Hibernate version: 3.2.5
Hi, colleagues!
I have a hard time creating the correct Criteria with Examples for the following setup:
Code:
@Entity
class Branch {
@Id
BranchPK getId()
}
-----------------------
@Embeddable
class BranchPK {
@Column(name="BRANCH_NUM")
Long getBranchNumber()
@ManyToOne
Bank getBank()
}
----------------
@Entity
class Bank{
@Id
Long getId()
}
Now I have an example object:
Code:
Bank bank = new Bank();
bank.setId(12);
BranchPK id = new BranchPK();
id.setBank(bank);
id.setBranchNumber(345);
Branch branch = new Branch();
branch.setId(id);
Since Hibernate doesn't include the associations in example object by default, but requires subcriteria for it, I have to pass via the embedded object to add the bank subcritera.
I have tried 4 options:
1. Ignoring the embeddness of the id fields:
Code:
Criteria criteria = session.createCriteria(Branch.class).add(Example.create(branch)).createCriteria("bank").add(Example.create(bank));
2. Add the id to the bank subcriteria path:
Code:
Criteria criteria = session.createCriteria(Branch.class).add(Example.create(branch)).createCriteria("id.bank").add(Example.create(bank));
3. Add the id as separate subcriteria:
Code:
Criteria criteria = session.createCriteria(Branch.class).add(Example.create(branch)).createCriteria("id").createCriteria("bank").add(Example.create(bank));
4. Add the id object as another example (as it was assocciation):
Code:
Criteria criteria = session.createCriteria(Branch.class).add(Example.create(branch)).createCriteria("id").add(Example.create(id)).createCriteria("bank").add(Example.create(bank));
Non of the options works.
Could you please point me on what I am doing wrong?
Thanks in advance,
Baruch.