-->
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: How to use Projection with two tables
PostPosted: Tue Apr 22, 2008 1:43 am 
Newbie

Joined: Tue Dec 04, 2007 7:25 am
Posts: 2
Location: Kolkata
Suppose there are two tables.
1. Person table(columns are personId[pk], personName);
2. Address table( addressId[pk], personId[fk], address1, address2 )

now suppode Person table has one record[11, bikash]

and Address table contains two records
[1(addressId),11(parentId as fk), kolkata, delhi]
[2(addressId),11(parentId as fk), b'lore, mumbai]



now i nedd only personName and latest address1 and address2 for each person record.


I am able to retrive only master record by projection. But how can i retrive recods from child table by projection

Means record should be-->>>
bikash, b'lore, mumbai.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:14 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
It is unclear what field of "address" you use to establish the max(). Suppose it is called "addnum"

Create a criteria query joining the two tables, add a projection "max" for that field, and list all the remaining fields you are interested in as "groupProperties" (equivalent to a "group by" in regular SQL).


Code:
      Criteria crit=session.createCriteria(Person.class);
      crit.createAlias("addresses", "b");
      crit.setProjection(Projections.projectionList()
      .add(Projections.max("b.addnum"))
                .add(Projections.groupProperty("name"))
                .add(Projections.groupProperty("address1"))
                .add(Projections.groupProperty("address2"))

                );
      crit.list();

_________________
Gonzalo Díaz


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.