-->
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.  [ 13 posts ] 
Author Message
 Post subject: select from createCriteria
PostPosted: Wed Sep 07, 2005 11:31 am 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.0.5

Name and version of the database you are using:
Oracle 9i

I want to do a simple select of children from a parent using the createCriteria.

This is how I do it in createQuery:
session.createQuery("select p.children from Parent p").list(); // get a list of Child objects

How would you do this with createCriteria? I cant get the returnMaps to work :(


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 11:32 am 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
Mapping docs please?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 11:39 am 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
Mapping:

<class name="Parent" table="tbl_parent">
<id name="parentID" type="java.lang.Long" column="parent_id">
<generator class="sequence">
<param name="sequence">seq_parent_id</param>
</generator>
</id>

<property name="name" type="string">
<column name="name" />
</property>

<set name="children" inverse="true" cascade="none">
<key column="parent_id"/>
<one-to-many class="Child" />
</set>

</class>

<class name="Child" table="tbl_child">
<id name="childID" type="java.lang.Long" column="child_id">
<generator class="sequence">
<param name="sequence">seq_child_id</param>
</generator>
</id>

<property name="name" type="string">
<column name="name" />
</property>

<many-to-one name="parent" column="parent_id" cascade="none"/>

</class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 1:11 pm 
Beginner
Beginner

Joined: Mon Oct 11, 2004 12:30 pm
Posts: 21
I may be mistaken, but i believe you can do this (may be a simpler way):

Code:
Criteria criteria = session.createCriteria(Child.class)
        .add(Expression.eq("parent", parent));

List results = criteria.list();
session.close();


HTH,
Jason


Top
 Profile  
 
 Post subject: i also have the same problem
PostPosted: Wed Sep 07, 2005 1:11 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
See this post. http://forum.hibernate.org/viewtopic.ph ... highlight=


Top
 Profile  
 
 Post subject: Re: i also have the same problem
PostPosted: Wed Sep 07, 2005 2:37 pm 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
chbvsprasad wrote:

So does that mean we cant get a collection from an object using createCriteria? Thats all I want to do.
From the looks of the link, seems like this is a feature request, and currently cant be done. Maybe i am misinterpretting something here?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:40 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
I do think it is can't be done now (atleast as of 3.0.5) and I don't see it in the road map either (this exact feature). So I am waiting for someone from Hibernate team to confirm this so that I can file for a feature request or as an issue.

Hopefull some one will respond.

Thanks,
Prasad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:43 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
jvinson wrote:
I may be mistaken, but i believe you can do this (may be a simpler way):

Code:
Criteria criteria = session.createCriteria(Child.class)
        .add(Expression.eq("parent", parent));

List results = criteria.list();
session.close();


HTH,
Jason


But that is from child to parent and not the other way round which is what the original poster wanted.

Thanks,
Prasad


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:51 pm 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
jvinson wrote:
I may be mistaken, but i believe you can do this (may be a simpler way):

Code:
Criteria criteria = session.createCriteria(Child.class)
        .add(Expression.eq("parent", parent));

List results = criteria.list();
session.close();


HTH,
Jason


Yes thats fine. But the direction is reversed. I want to do add other restrictions on the parent later on, which if done in the direction from child to parent, will not only become complicated, but also result into performance degradation in the long term.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 2:54 pm 
Newbie

Joined: Fri Jun 10, 2005 1:52 pm
Posts: 9
If this feature is not in the current version, then howcome in the manual there is a returnMaps() example? I dont mind using the depricated method as i just want my code to work right now. I can change it once the Hibernate team adds another way of doing this.
Has anyone got the returnMaps example to work? I cant.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:08 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
puks7 wrote:
If this feature is not in the current version, then howcome in the manual there is a returnMaps() example? I dont mind using the depricated method as i just want my code to work right now. I can change it once the Hibernate team adds another way of doing this.
Has anyone got the returnMaps example to work? I cant.

I am unable to find any reference to returnMaps() in the code base that I have. May be returnMaps() doesn't work any more so it got removed?
I looked into the code for Criteria queries and Projections and I can't find any code that does similar things that an HQL query with elements() does.

A work around which is not entirely unsatisfactory (performance would be same but you need to do more work) is to do the following.

step1:
select child.id from parent where parent.id = ...

step2:
process each id from the previous result and load them.

This is what HQL query with elements() tag does and Criteria Query is not yet set up to do this. I might end up coding it this way if I think it will not be done in Hibernate in the near future.

The performance is may be bad relatively because you can load all the children properties in the first query itself and build the objects that way like below

step1:
select child.id, child.prop1, child.prop2, ... from parent join child where where child.parent_id = parent.id and ...
step2:
for each result row, construct the child object.

This would reduce the number of queries but the code could be cumbersome and it is better if hibernate can do this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
puks7 wrote:
If this feature is not in the current version, then howcome in the manual there is a returnMaps() example? I dont mind using the depricated method as i just want my code to work right now. I can change it once the Hibernate team adds another way of doing this.
Has anyone got the returnMaps example to work? I cant.


setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 07, 2005 3:41 pm 
Beginner
Beginner

Joined: Fri Oct 15, 2004 9:58 pm
Posts: 25
It works for me too then.


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