-->
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.  [ 3 posts ] 
Author Message
 Post subject: newbie: simple hql query...
PostPosted: Wed Dec 14, 2005 2:57 pm 
Newbie

Joined: Tue Dec 13, 2005 5:43 pm
Posts: 5
hello,
I was wondering how to do a simple query with hql. I have the following two POJOs, with a one-to-many association:

Code:
public class Component  implements java.io.Serializable {
     private Set componentPowerLimits;
}

public class ComponentPowerLimit  implements java.io.Serializable {
     private Date terminated;
     private Component component;
}


in my database table, the COMPONENT_POWER_LIMITS table contains the id (cmp_irn_nr) of the COMPONENT as a foreign key. I would like to achieve an SQL looking like this:

Code:
select id from component_power_limits where cmp_irn_nr = 779511201 and terminated is not null


How can I achieve this with hql? I tried
Code:
session.createQuery("from ComponentPowerLimit cpl where cpl.cmpIrnNr = :cmpIrn1 and terminated is null");


which of course did not work as the cmpIrnNr is not a part of the ComponentPowerLimit class.

Hmmm.... seems to be something about Hibernate I do not quite grasp. Any help or pointers in the right direction would be highly appreciated!

cheers,
Kenny


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 3:36 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Always think of HQL in terms of the Objects, not the table/columns. You want to do:

Code:
from ComponentPowerLimit cpl where cpl.component = :component and terminated is null


Then you would bind the Component object to the HQL query, something like this:

Code:
Component comp = session.get(Component.class,new Long(779511201));
List cpls = session.createQuery(hql).setEntity("component",comp).list();


Of course, this is just sample code. I assume that you would already have the Component loaded previously, and wouldn't really load it by id just for this query. If that were the case, then you could do it in one shot with a query like:

Code:
from ComponentPowerLimit cpl where cpl.component.componentId = :componentid and terminated is null


where componentId is the name of the field in your Component object for the primary key (you didn't show one in your posted code, so I don't know what yours is really named).

Hope this helps.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 14, 2005 4:12 pm 
Newbie

Joined: Tue Dec 13, 2005 5:43 pm
Posts: 5
thank you for your very helpful answer!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.