dayse rocha

Greenhorn
+ Follow
since Sep 21, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by dayse rocha

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();
}
}
It won't be necessary, my dear. I've already solved this problem.

Thanks.
14 years ago
Did anyone solve this problem???
Tell me why, please.
14 years ago