-->
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: Joins wth Few Considerations
PostPosted: Wed Aug 28, 2013 6:34 am 
Newbie

Joined: Wed Aug 28, 2013 5:45 am
Posts: 4
Hi All,
My Table Structure Will be as shown below.
Image

I will have association( one - many ) in my A & B entity HBM's for C & D entities.
the column TABLE_ID in C & D will hold the values as 1 for A and 2 for B.
the column TABLE_PK in C & D will hold the Primary Key values of entities A & B .

So My Concern is I want to do fetch operation as described below
Quote:
from A
join C on A.id = C.table_pk AND C.table_type = 1
join D on A.id = D.table_pk AND D.table_type = 1
WHERE A.id = :id


My Concern
  • Only One query must be executed.
  • AS per Joins (one - many ) there will be duplicate in the result set. Want to use Criteria.DISTINCT_ROOT_ENTITY.
  • And I need to fetch only the fields I want to, not all. Some thing like PROJECTION.
  • After all the conditions should be placed as after the joins not after the where condition. like RESTRICTIONS.EQPROPERTY .

How can I achieve this .


Regards,
A.


Top
 Profile  
 
 Post subject: Re: Joins wth Few Considerations
PostPosted: Thu Aug 29, 2013 1:45 am 
Newbie

Joined: Wed Aug 28, 2013 5:45 am
Posts: 4
I end up with something like stated below.

Code:
   Criteria mainCriteria = session.createCriteria(EntityA.class, "a");
         mainCriteria.createCriteria(
               "a.bEntities",
               "b",
               JoinType.LEFT_OUTER_JOIN,
               Restrictions.and().add(Restrictions.eq("b.tableId", 1)));
                     
         mainCriteria.createCriteria(
               "a.cEntities",
               "c",
               JoinType.LEFT_OUTER_JOIN,
               Restrictions.and().add(Restrictions.eq("c.tableId", 2)));
         ProjectionList   mainCriteriaProjectionList = Projections.projectionList();
         mainCriteriaProjectionList.add(Projections.property("a.idA"), "id");
         mainCriteriaProjectionList.add(Projections.property("a.colA"), "name");
         [b]//*** below colB and colC are not shown in the above image , consider it is there[/b]
         mainCriteriaProjectionList.add(Projections.property("b.colB"), "bValue");
         mainCriteriaProjectionList.add(Projections.property("c.colC"), "cValue");
         mainCriteria.setProjection(memberProjectionList);
         memberCriteria.setResultTransformer(new MyResultTransformer());//*** My manual transformer to remove the duplicates.
         memberCriteria.list(); [b]// ** Finally the result[/b]



Suggest me the best approach to follow.


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.