Hi
This is a sample code for you
Code:
package lk.wts.test;
import java.util.Set;
public class Accounts {
String accountNum;
String name;
Set details;
public String getAccountNum() {
return accountNum;
}
public void setAccountNum(String accountNum) {
this.accountNum = accountNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getDetails() {
return details;
}
public void setDetails(Set details) {
this.details = details;
}
}
Accounts.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="lk.wts.test.Accounts"
table="Accounts"
>
<id
name="accountNum"
column="AccountNum"
type="java.lang.String"
>
<generator class="assigned">
</generator>
</id>
<property
name="name"
type="java.lang.String"
update="true"
insert="true"
column="Name"
/>
<set
name="details"
lazy="false"
inverse="true"
cascade="all"
sort="unsorted"
>
<key
column="AccountNum"
>
</key>
<one-to-many
class="lk.wts.test.AccountsDetails"
/>
</set>
</class>
</hibernate-mapping>
AccountsDetails.class
Code:
package lk.wts.test;
import java.io.Serializable;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class AccountsDetails implements Serializable{
String accountNum;
int observationMonth;
int observationYear;
double balance;
public String getAccountNum() {
return accountNum;
}
public void setAccountNum(String accountNum) {
this.accountNum = accountNum;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public int getObservationMonth() {
return observationMonth;
}
public void setObservationMonth(int observationMonth) {
this.observationMonth = observationMonth;
}
public int getObservationYear() {
return observationYear;
}
public void setObservationYear(int observationYear) {
this.observationYear = observationYear;
}
public boolean equals(Object arg0) {
if(arg0 instanceof AccountsDetails){
AccountsDetails obj = (AccountsDetails)arg0;
String key = this.accountNum+"_"+ this.observationMonth+"_"+ this.observationYear;
return key.equals(obj.getAccountNum()+"_"+obj.getObservationMonth()+"_"+obj.getObservationYear());
}
return false;
}
public int hashCode() {
HashCodeBuilder hashBuilder=new HashCodeBuilder();
return hashBuilder.append(this.accountNum+"_"+ this.observationMonth+"_"+ this.observationYear).hashCode();
}
}
AccountsDetail.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
>
<class
name="lk.wts.test.AccountsDetails"
table="AccountsDetails"
>
<composite-id
>
<key-property
name="accountNum"
type="java.lang.String"
column="AccountNum"
/>
<key-property
name="observationMonth"
type="int"
column="ObservationMonth"
/>
<key-property
name="observationYear"
type="int"
column="ObservationYear"
/>
</composite-id>
<property
name="balance"
type="double"
update="true"
insert="true"
column="Balance"
/>
</class>
</hibernate-mapping>
try this query in your application
Code:
Query q=sess.createQuery("select a from Accounts a where a.accountNum='123abc'");
List list=q.list();
You didn't rate my answer
Amila
(Don't forget to rate if helps)