1

我有两个表部分和子部分。Part 表具有 id、name、desc 等通用字段。SubPart 表具有 part_id、sub_part_id 作为复合键。这两个列都引用 Part 表,并且每个列都有一个一对多的映射,就像 Part 表中的每个 part_id 一样,SubPart 表中的两个列都可以有多个条目。我在为 SubPart 表定义复合键时遇到问题。我尝试了嵌入式标签,但它不起作用。我该如何解决这个问题。非常感谢。

像这样的零件表。

@Entity
@Table(name="Part")
public class Part {

    @Id
    @GeneratedValue
    @Column(name="Part_Id")
    private int id;
    @Column(name="Part_Number")
    private String partNumber;
    @Column(name="Part_Name")
    private String partName;
}

子零件表

@Entity
@Table(name="SubPart")
public class SubPart {
    // part and subPart combination is the compound key here.
    @ManyToOne
    @JoinColumn(name="Part_Id")
    private Part part;

    @ManyToOne
    @JoinColumn(name="Sub_Part_Id")
    private Part subPart;

    @Column(name="Quantity")
    private Integer quantity;
}
4

2 回答 2

6

你说

我在为 SubPart 表定义复合键时遇到问题

当你有一个复合主键时,你必须定义一个类(通常是一个静态内部类)来定义你的复合主键(只是一个建议:因为 Hibernate 使用代理,所以更喜欢将你的带注释的映射放在 getter 而不是字段上成员)

/**
  * When both entity class and target table SHARE the same name
  * You do not need @Table annotation
  */
@Entity
public class SubPart implements Serializable {

    @EmbeddedId
    private SubPartId subPartId;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="PART_ID", insertable=false, updateable=false)
    private Part part;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="SUP_PART_ID", insertable=false, updateable=false)
    private SubPart subPart;

    /**
      * required no-arg constructor
      */
    public SubPart() {}
    public SubPart(SubPartId subPartId) {
        this.subPartId = subPartId;
    }

    // getter's and setter's

    /**
      * It MUST implements Serializable
      * It MUST overrides equals and hashCode method
      * It MUST has a no-arg constructor
      *
      * Hibernate/JPA 1.0 does not support automatic generation of compound primary key
      * You SHOULD set up manually
      */
    @Embeddable
    public static class SubPartId implements Serializable {

        @Column(name="PART_ID", updateable=false, nullable=false)
        private Integer partId;
        @Column(name="SUB_PART_ID", updateable=false, nullable=false)
        private Integer subPartId;

        /**
          * required no-arg constructor
          */
        public SubPartId() {}
        public SubPartId(Integer partId, Integer subPartId) {
            this.partId = partId;
            this.subPartId = subPartId;
        }

        // getter's and setter's

        @Override
        public boolean equals(Object o) {
            if(!(o instanceof SubPartId))
                return null;

            final SubPartId other = (SubPartId) o;
            return new EqualsBuilder().append(getPartId(), other.getPartId())
                                      .append(getSubPartId(), other.getSubPartId())
                                      .isEquals();
        }

        @Override
        public int hashCode() {
            return new HashCodeBuilder().append(getPartId())
                                        .append(getSubPartId())
                                        .toHashCode();  
        }

    }

}

注意 Part 和 SubPart 映射已被标记为 insertable=false, updateable=false因为映射已在复合主键中定义。Hibernate不允许您使用相同的列映射两个属性,除非您标记 insertable=false、updateable=false。否则你会看到这个很好的例外

应标记为 insertable=false, updateable=false

于 2010-08-20T05:44:13.787 回答
0

我想我会在 Part 类中声明一个 Map<Part,SubPart> 类型的字段并将其声明为@OneToMany。

实际上,在这种特殊情况下,它可能是 Map<Part,Integer> 事件,因为唯一的其他字段是数量。不需要 SubPart 类。

于 2010-08-20T05:19:10.910 回答