Hibernate 复合键是另一个表的外键

发布于 2024-09-15 17:17:33 字数 870 浏览 9 评论 0原文

我有两个表 Part 和 SubPart。零件表具有 id、名称、desc 等通用字段。子零件表具有 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;
}

I have two table Part and SubPart. Part table has general fields like id, name, desc etc. The SubPart table has part_id, sub_part_id as composite key. Both of these columns are referring to Part table and has a one to many mapping for each of them, like for each part_id in the Part table there can be multiple entries in SubPart table for both the columns. I'm having problem defining the composite key for the SubPart table. I tried the Embedded tag but its not working. How can I address this problem. Thanks a lot.

Part table like this.

@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;
}

Sub Part Table

@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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-09-22 17:17:33

你说

我在为 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

You said

I'm having problem defining the composite key for the SubPart table

When you have a compound primary key, you must define a class (Usually a static inner class) which defines your compound primery key (Just an advice: because Hibernate makes use of proxies, prefer to put your annotated mapping on the getter's instead of field members)

/**
  * 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();  
        }

    }

}

Notice Part and SubPart mapping has been marked as insertable=false, updateable=false Because the mapping has been defined in the compound primary key. Hibernate does not allow you mapping two properties WITH THE SAME COLUMN unless you mark insertable=false, updateable=false. Otherwise you will see this nice exception

Should be marked with insertable=false, updateable=false

猫烠⑼条掵仅有一顆心 2024-09-22 17:17:33

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

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

I think I would declare a field of type Map<Part,SubPart> in class Part and declare it as @OneToMany.

Actually, in this particular case, it could event be a Map<Part,Integer> because the only other field is the quantity. The SubPart class is not needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文