Usage of Composite primary
key in hibernate
AbcEntity.java
============================================
package com.xyz.entity;
import java.io.Serializable;
public class AbcEntity implements Serializable {
private static final long serialVersionUID = 1L;
private AbcEntityInputPK id;
private String answer;
public AbcEntity() {
}
public AbcEntityInputPK getId() {
return this.id;
}
public void setId(AbcEntityInputPK id) {
this.id = id;
}
public String getAnswer() {
return this.answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
}
AbcEntityInputPK.java - composite primary class
===============================================
package com.xyz.entity;
import java.io.Serializable;
public class AbcEntityInputPK implements Serializable {
private static final long serialVersionUID = 1L;
private Integer qId;
private Short cId;
public AbcEntityInputPK() {
}
public Integer getQId() {
return this.qId;
}
public void setQId(Integer qId) {
this.qId = qId;
}
public Short getCId() {
return this.cId;
}
public void setCId(Short cId) {
this.cId = cId;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof AbcEntityInputPK)) {
return false;
}
AbcEntityInputPK cOther = (AbcEntityInputPK)other;
return
(this.qId == cOther.qId)
&& (this.cId == cOther.cId);
}
public int hashCode() {
final int prime =7;
int hash = 17;
hash = hash * prime + ((int) (this.qId ^ (this.qId >>> 32)));
hash = hash * prime + ((int) (this.cId ^ (this.cId >>> 32)));
return hash;
}
}
hbm file
========================================
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.xyz.entity.AbcEntity" table="ABC_TABLE_INPUT">
<composite-id name="id" class="com.xyz.entity.AbcEntityInputPK">
<key-property name="qId" type="int">
<column name="Q_ID" precision="10" scale="0" />
</key-property>
<key-property name="cId" type="short">
<column name="C_ID" precision="10" scale="0" />
</key-property>
</composite-id>
<property name="answer" type="string">
<column name="ANSWER" length="500" />
</property>
</class>
</hibernate-mapping>
No comments:
Post a Comment