Alguém pode me explicar o que está errado no código abaixo?
public static void main(
String[] args) throws Exception {
SessionFactory session = HibernateUtil.getSessionFactory();
Session sess = session.getCurrentSession();
Transaction tx = sess.beginTransaction();
CompoundKey key1 = new CompoundKey(500, 1001);
Accounts saving = new Accounts();
saving.setCompoundKey(key1);
saving.setAccountBalance(8500);
CompoundKey key2 = new CompoundKey(500, 2001);
Accounts checking = new Accounts();
checking.setCompoundKey(key2);
checking.setAccountBalance(8500);
sess.saveOrUpdate(saving);
sess.saveOrUpdate(checking);
Criteria crit = sess.createCriteria(Accounts.class).add(
Restrictions.eq("compoundKey.userId", new Integer(500)));
List accList = crit.list();
for(int i = 0; i < accList.size(); i++){
Accounts acc = (Accounts) accList.get(i);
System.out.println(acc.getAccountBalance());
}
tx.commit();
session.close();
}
-----------------------------------------------------------------------------------------------
package hibernate;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Accounts {
private CompoundKey compoundKey;
private int accountBalance;
@Id
public CompoundKey getCompoundKey() {
return compoundKey;
}
public void setCompoundKey(CompoundKey compoundKey) {
this.compoundKey = compoundKey;
}
public int getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}
}
-----------------------------------------------------------------------------------------------
package hibernate;
import java.io.Serializable;
import javax.persistence.Embeddable;
@Embeddable
public class CompoundKey implements Serializable{
private int userId;
private int accountId;
public CompoundKey(int userId, int accountId) {
super();
this.userId = userId;
this.accountId = accountId;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}
public CompoundKey() {
super();
}
}