-->
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: hibernate mapping without using Set object
PostPosted: Fri Aug 14, 2009 3:49 am 
Newbie

Joined: Tue Aug 04, 2009 6:46 am
Posts: 17
Hi all
i have articles and comments tables
article comments will be maintained in coments table with thread_code of article(the commments may be from 0 to 1000)
my hibernate mapping file contains

Article.hbm.xml

<set name="comments" fetch="select" inverse="false">
<key column="thread_code" property-ref="threadCode"/>
<one-to-many class="pkg.Comments" not-found="ignore"/>
</set>

related getters and setters i have provided in the bean follows:

public Set getComments() {
return comments;
}

public void setComments(Set<CommentsVO> comments) {
this.comments = comments;
}
private Set<CommentsVO> comments;

now my use case is i have to get the list of article objects from data base with only comments count

Criteria criteria = session.createCriteria(Articles.class);
criteria.createAlias("comments", "c", CriteriaSpecification.LEFT_JOIN);
ProjectionList list = Projections.projectionList();
list.add(Projections.groupProperty("id"));
list.add(Projections.count("c.threadCode"));
criteria.setProjection(list);
return criteria.list();

but my query is getting the articles each with a set of comment objects and the comments count
here i dont want the hibernate to load set of comments
my requirement is to load only the article object with comments count from comments table
i want to remove the Set Comments from my mapping file and bean
then how can i join the 2 tables to get article and related comments count (not all the comments objects related to the article)

thanks a lot..


Top
 Profile  
 
 Post subject: Re: hibernate mapping without using Set object
PostPosted: Fri Aug 14, 2009 1:19 pm 
Newbie

Joined: Tue Feb 24, 2009 8:39 pm
Posts: 19
You must use a magic spell called "Column and formula elements" http://docs.jboss.org/hibernate/stable/ ... ing-column

Something like this:
<property name="totalSpells" type="integer" formula="(SELECT COUNT(c.thread_code) FROM THREADS c WHERE c.thread_code = spell_id_from_container)"/>

But be careful, you must understand how it works, look carefully at the where condition, and try to guess the "spell_id_from_container" is.


Good luck!


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.