-->
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.  [ 2 posts ] 
Author Message
 Post subject: Criteria for oneToMany relation, fetch SubTopic from Topic
PostPosted: Wed Feb 22, 2012 3:36 pm 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
Hello,

im having some problems creating a criteria for fetching a specific element. im using hibernate 3.6.6.Final

my classes:
Code:
class Topic {
@id
long id;

@oneToMany
@JoinColumn "topic_id"
list<SubTopic> subTopics;
}

Code:
class SubTopic {
@id
long id;

string content;
}


now im trying to load the subtopic with code="xyz" that belongs to the topic with id="123";
in plain sql i would just write a query like:
Code:
select * from subtopic join topic on topic.id = subtopic.topic_id where subtopic.code like 'xyz' and topic.id = 123;

but since we absolutely dont want a reference to the topic in our subtopic-class, this is not possible.
one solution would be to load the topic and then iterate through the subtopic-list, but this would be a problem if there would be several thousands subtopics for a topic.. any advices would be appreciated!


Top
 Profile  
 
 Post subject: Re: Criteria for oneToMany relation, fetch SubTopic from Topic
PostPosted: Wed Feb 22, 2012 7:40 pm 
Beginner
Beginner

Joined: Thu Jun 14, 2007 4:33 am
Posts: 39
i found a working HQL solution:
Code:
"select subtopic from Topic topic inner join topic.subTopic subtopic where topic.id = :topicId and subtopic.code = :code"


however i wonder: could this be accomplished using hibernate criteria?


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