Drew1208 Drew1208 - 6 months ago 24
Java Question

Configuring JPA to handle HIBERNATE composite-id and key-property

I am trying to find the correct way to convert the below (Hibernate) XML to JPA annotations:

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<class name="TesTEntry" table="RAW_SCORE">
<composite-id mapped="false" unsaved-value="undefined">
<key-property column="SSN" name="ssn" type="string"/>
<key-property column="SUB_TEST_CD" name="subTestCd" type="string"/>
</composite-id>
<property column="TEST_QY" generated="never" lazy="false"
name="testQy" type="java.lang.Short"/>
<property column="SYS_REC" generated="never" lazy="false"
name="sysRec" type="java.util.Date"/>
<property column="SYS_ID" generated="never" lazy="false"
name="sysId" type="java.lang.String"/>
</class>


Since I have two
<key-property>
I am not sure if I use
@Id
, or
@EmbeddedId

Answer

Since I have two I am not sure if I use @Id, or @EmbeddedId

You mean probably IdClass. And it doesn't matter which one you use.

Let us say, you want to use @IdClass:

  • Define a class for your ID.
  • Implement equals and hashCode methods
  • Implement public default constructor
  • Implement the Serializable interface

Here is an example implementation (equals & hashCode are generated from IDE):

    public class TestEntityPK implements Serializable {

       private static final long serialVersionUID = -3424067518791080014L;

       private String ssn;
       private String subTestCd;

       public TestEntityPK() { // }

       public TestEntityPK(String ssn, String subTestCd) {
           this.ssn = ssn;
           this.subTestCd;
       }

       public String getSsn() {
          return ssn;
       }

       public String getSubTestCd() {
          return subTestCd;
       }

      @Override
      public int hashCode() {
          final int prime = 31;
          int result = 1;
          result = prime * result + ((ssn == null) ? 0 : ssn.hashCode());
          result = prime * result
            + ((subTestCd == null) ? 0 : subTestCd.hashCode());
          return result;
      }

      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          TestEntityPK other = (TestEntityPK) obj;
          if (ssn == null) {
             if (other.ssn != null)
                 return false;
          } else if (!ssn.equals(other.ssn))
              return false;
          if (subTestCd == null) {
              if (other.subTestCd != null)
                  return false;
           } else if (!subTestCd.equals(other.subTestCd))
              return false;
           return true;
      }

}

And use it in the entity as follows:

@Entity
@Table(name="RAW_SCORE")
@IdClass(TestEntityPK.class)
public class TestEntity {
    @Id private String ssn;

    @Id 
    @Column(name="SUB_TEST_CD")
    private String subTestCd;

    @Column(name="TEST_QY")
    private short testQy;

    @Column(name="SYS_REC")
    @Temporal(TemporalType.DATE)
    private Date sysRec;

    @Column(name="SYS_ID")
    private String sysId;

    // getters and setters
}
Comments