我正在尝试找到将下面(Hibernate)XML转换为JPA注释的正确方法:
<!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>
由于我有两个<key-property>
我不确定是否使用@Id
或@EmbeddedId
答案 0 :(得分:0)
由于我有两个,我不确定我是否使用@Id或@EmbeddedId
你的意思可能是IdClass
。你使用哪一个并不重要。
我们说,您想使用@IdClass
:
equals
和hashCode
方法Serializable
界面这是一个示例实现(等于&amp; hashCode是从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;
}
}
并在实体中使用它如下:
@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
}