-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Query by Criteria with Composite Key Class?
PostPosted: Thu Oct 07, 2004 9:37 am 
Beginner
Beginner

Joined: Mon Jun 21, 2004 2:41 pm
Posts: 40
Hibernate version:
2.1.6


My question is can I use the Criteria API to query on a class/table where the class uses a composite key identifier (seperate class)?

Ex:
Cat table uses a composite key made up of:
name
sex
hair

The composite key is in a seperate class.

I know the cat's name so I want to get all cats where Cat.comp_id.name = 'Fluffy'

How can I do this with Criteria API?

Something I have tried is this but it did not return the correct results:
Quote:
Criteria crit = session.createCriteria(Cat.class);

Cat cat = new Cat();
cat.setComp_id(new CatPK());

cat.getComp_id().setName('Fluffy');
crit.add(Example.create(cat));

crit.setMaxResults(300);

results = crit.list();

Thank you for any suggestions,
David


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 9:39 am 
Beginner
Beginner

Joined: Mon Jun 21, 2004 2:41 pm
Posts: 40
One last try... Anyone?

bump


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 3:56 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I don't think the QBE looks at the PK at all.

Have you tried:

Code:
Criteria crit = session.createCriteria(Cat.class);
crit.add(Expression.eq("id.name", "Fluffy"));
crit.setMaxResults(300);
List cats = crit.list();


Essentially, anytime you want to get at a property on your key you can use id.someProperty. Alternatively you can use nameOfKey.someProperty.

HTH


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 4:05 pm 
Beginner
Beginner

Joined: Mon Jun 21, 2004 2:41 pm
Posts: 40
VampBoy wrote:
I don't think the QBE looks at the PK at all.

Have you tried:

Code:
Criteria crit = session.createCriteria(Cat.class);
crit.add(Expression.eq("id.name", "Fluffy"));
crit.setMaxResults(300);
List cats = crit.list();


Essentially, anytime you want to get at a property on your key you can use id.someProperty. Alternatively you can use nameOfKey.someProperty.

HTH


Yep I did try that. If you try that on a class where the composite key is kep as an object variable then you will get an error. :( Been there done that.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 12, 2004 4:22 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
I don't follow. From my experience you can do id.name but you aren't able to do id.someObject.someProperty. We do the id.name syntax quite frequently in our code (until we eliminate all the comp PKs).

For the id.someObject.someProperty a workaround is to specify a many-to-one on someObject with insert="false", update="false" and then walk the chain from there.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.