wood burning stoves
The moose likes Object Relational Mapping and the fly likes composite key and Criteria API [Hibernate] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "composite key and Criteria API [Hibernate] " Watch "composite key and Criteria API [Hibernate] " New topic

composite key and Criteria API [Hibernate]

prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
Hi All,

I have a composite key with three columns (a,b,c) that I need to map,
I created a @Embeddable PK class

and provided it as @EmbeddedId in the mapping class

I've tried few things but they are not working

(I also tried setting value of C but it didn't help)

Now I want something like SELECT * FROM TABLE_COMPOSITE WHERE A = ? AND B = ? using Criteria API

Please help.

this message brought to you by .... PIE! .... it's yummy! ;)
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
I tried something like

but I am getting
could not resolve property: a of: TableComposite

am I missing something here ?
prateek urmaliya
Ranch Hand

Joined: Sep 15, 2006
Posts: 87
it's workin

I tried

final Criteria crit = session.createCriteria(TableComposite.class)
.add(Restrictions.eq("id.A", 123))
.add(Restrictions.eq("id.B", 456));

I was missing that though my field was a the getter was getA
Also I don't need alias as I am not joining anything
Thanks to my colleague DT
Gouri SenSharma
Ranch Hand

Joined: Apr 16, 2008
Posts: 57
I have a Class Address with the composite key (personId, departmentId). I created a class PersonDepartmentId as below

To search an address I used criteria

Where personDepartmentId is a reference to an object of type PersonDepartmentId.
Also, please note that the equals method of PersonDepartmentId has been overridden.

And this works....

SCJP 5.0, SCBCD 5.0
Working towards OCMJEA, cleared part 1,2 and 3. Yet to attend training
dayse rocha

Joined: Sep 21, 2009
Posts: 3
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();

CompoundKey key2 = new CompoundKey(500, 2001);
Accounts checking = new Accounts();


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);

package hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;

public class Accounts {
private CompoundKey compoundKey;
private int accountBalance;

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;
public class CompoundKey implements Serializable{
private int userId;
private int accountId;

public CompoundKey(int userId, int accountId) {
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() {
I agree. Here's the link: http://aspose.com/file-tools
subject: composite key and Criteria API [Hibernate]
It's not a secret anymore!