-->
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: Composite Identifier class and createQuery
PostPosted: Tue Nov 22, 2005 5:51 pm 
Newbie

Joined: Tue Nov 22, 2005 5:44 pm
Posts: 3
I have create a Composite Identifier class called PatKey, which I use to retrieve a Pat object (which includes a PatKey id; field).

The session.get() works fine with the PatKey as a parameter. However, if I try to use createQuery() with the following HQL:

session.createQuery("from Pat as p where p.hosp_id = :hospId"
+ " and p.pat_num = :patNum and p.ver_seq = :verSeq");

(where :hospId, :patNum, :verSeq are the elementsa of PatKey), I get the following error:

org.hibernate.QueryException: could not resolve property: hosp_id of: org.ed4becky.hibernate.hibernate.Pat

Makes sense to me, since there is no hosp_id in the Pat class (it's in thye id filed, which contaoins the PatKey reference).

What is the best way if I want to retrieve multiple Pat objects based on part of the primary key, and (possibly) some other field(s)? IE how do I use createQuery when a Composite Identier clkass is involved?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 5:59 pm 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
try using:
Code:
session.createQuery("from Pat as p where p.id.hosp_id = :hospId"
+ " and p.id.pat_num = :patNum and p.ver_seq = :verSeq");


insert id. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 22, 2005 9:19 pm 
Newbie

Joined: Tue Nov 22, 2005 5:44 pm
Posts: 3
momania wrote:

insert id. :)


Thanx! That almost did it! I also had to change from using table column names to using object field names! (makes sense).

Appreciate the quick response!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 2:58 am 
Beginner
Beginner

Joined: Tue Nov 22, 2005 4:53 pm
Posts: 41
Location: Netherlands
ed4becky wrote:
momania wrote:

insert id. :)


Thanx! That almost did it! I also had to change from using table column names to using object field names! (makes sense).

Appreciate the quick response!

In HQL you can use the object's attribute names in stead of the table column names indeed. :)

so. "from Object o where o.id.xValue = :x and o.otherAttribute = :other"

You can use that as far as your references go. :)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 5:21 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
You've got some other alternatives:

Code:
session.get( Pat.class, patKey );  // returns null if not found
session.load( Pat.class, patKey );  // undefined result if not found
session.createCriteria( Pat.class ).add( Restrictions.idEq( patKey ) ).uniqueResult();  // should return null if not found; other conditions possible, too (what for though)


And I think there should be some possibility to formulate the condition like " p.id = :id ". It just couldn't work it out yet ;-)

_________________
hth,
Heinz
Don't forget to rate if this helped


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.