1

2 つのテーブル パーツとサブパーツがあります。part テーブルには、id、name、desc などの一般的なフィールドがあります。SubPart テーブルには、複合キーとして part_id、sub_part_id があります。これらの列はどちらも Part テーブルを参照しており、それぞれに 1 対多のマッピングがあります。たとえば、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 はプロキシを使用するため、注釈付きマッピングをフィールドではなくゲッターに配置することをお勧めします)メンバー)

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

    }

}

パーツとサブパーツのマッピングが、insertable =false、updateable=false としてマークされていることに注意してください。これは、マッピングが複合主キーで定義されているためです。Hibernateでは、insertable=false、updateable=false とマークしない限り、2 つのプロパティを同じ列にマッピングすることはできません。それ以外の場合は、この素晴らしい例外が表示されます

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 に答える