-->
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.  [ 4 posts ] 
Author Message
 Post subject: Child entities also getting loaded for many-to-one relations
PostPosted: Mon Jul 03, 2006 12:11 pm 
Newbie

Joined: Sun Oct 02, 2005 1:18 pm
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
3.0.5

I have defined a lazy many-to one relationship between two entities.
(Street and Municipality). Multiple streets can point to a single municipality.
however when I search for the root entity i.e Street, it also loads the child entity i.e Municipality!

My hibernate configuration for Street has following mapping for Municipality:
<many-to-one name="municipality" class="Municipality" column="ID_MUNICIPALITY_TB" not-found="ignore" lazy="true" cascade="evict" insert="false" update="false"/>

The java code to build the criteria is as follows:
Code:
Criteria ct1 = session.createCriteria(Street.class);
ct1.createCriteria("municipality")
.add(Restrictions.eq("id","123"));
ct1.addOrder(Order.asc("id"));
ct1.setFirstResult(page);
ct1.setMaxResults(100);


The SQL query that gets executed is
select * from ( select this_.ID as ID1_, this_.VERSION as VERSION15_1_, this_.UPDATED_TS as UPDATED3_15_1_, this_.CREATED_TS as CREATED4_15_1_, this_.CREATED_BY as CREATED5_15_1_, this_.UPDATED_BY as UPDATED6_15_1_, this_.DELETED as DELETED15_1_, this_.FICTIONAL as FICTIONAL15_1_, this_.ID_MUNICIPALITY_TB as ID9_15_1_, municipali1_.ID as ID0_, municipali1_.VERSION as VERSION9_0_, municipali1_.UPDATED_TS as UPDATED3_9_0_, municipali1_.CREATED_TS as CREATED4_9_0_, municipali1_.CREATED_BY as CREATED5_9_0_, municipali1_.UPDATED_BY as UPDATED6_9_0_, municipali1_.FICTIONAL_FLAG as FICTIONAL7_9_0_, municipali1_.DELETED as DELETED9_0_, municipali1_.POST_CODE as POST9_9_0_, municipali1_.ID_COUNTRY_TB as ID10_9_0_ from LOC_STREET_TB this_ inner join LOC_MUNICIPALITY_TB municipali1_ on this_.ID_MUNICIPALITY_TB=municipali1_.ID where municipali1_.ID=? order by this_.ID asc ) where rownum <= ?

Can anyone suggest if there is something wrong with the configuration mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 12:31 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
try this code.

Code:
Criteria ct1 = session.createCriteria( Street.class );
ct1.add( Restrictions.eq( "id", "123" ) );
ct1.addOrder( Order.asc( "id" ) );
ct1.setFirstResult( page );
ct1.setMaxResults( 100 );


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 1:36 pm 
Newbie

Joined: Sun Oct 02, 2005 1:18 pm
Posts: 3
The code you are suggesting would fetch the street with id = "123". I need to fetch all the streets which are linked to the municipality that has id = "123". Please note both street as well as municipality have the "id" field.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 03, 2006 2:07 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Maybe this one.

Code:
DetachedCriteria mpltyCriteria = DetachedCriteria.forClass( Municipality.class, "mplty" );
mpltyCriteria.add( Restrictions.eq( "mplty.id", new Long( 1 ) ) );
mpltyCriteria.setProjection( Projections.property( "mplty.id" ) );

DetachedCriteria streetCriteria = DetachedCriteria.forClass( Street.class, "mystreet" );
streetCriteria.add( Property.forName( "mystreet.MuncipalityIDColumnINStreet" ).in( mc ) );
streetCriteria.addOrder( Order.asc( "mystreet.MuncipalityIDColumnINStreet" ) );
streetCriteria.setFirstResult( page );
streetCriteria.setMaxResults( 100 );
streetCriteria.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY );

List results = streetCriteria.getExecutableCriteria( session ).list();


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