-->
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.  [ 12 posts ] 
Author Message
 Post subject: Hibernate selects all columns in all tables in FROM clause
PostPosted: Tue May 09, 2006 5:37 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
Hi,

I am using Hibernate 3.1. I got a criteria query that joins multiple tables. When I execute the query, the SQL generated has all columns from all tables in FROM clause. Is there a way, I can restrict hibernate to select cloumns from main criteria only?

Below is the example.

Criteria query:
criteria = getSession().createCriteria(JournalLine.class);
criteria.createAlias("journal", "jou").createAlias("jou.journalType", "jty").add(Restrictions.ne("jty.txnNature", JournalType.TxnNature.CASH));
criteria.list();

JournalLine has many-to-one relationship with Journal and Journal has one-to-one relationship with Journal Type.
Above code generates SQL that selectes columns from JournalLine, Journal and JournalType while I want it to select only JournalLine.

I have gone through the projection topic. Adding projection is one option but then I need to add projection for all columns in JournalLine table and also have to construct JournalLine object manually which is very ugly.

Any help will be very much appreciated.

Many Thanks
Shantanu


Top
 Profile  
 
 Post subject: Re: Hibernate selects all columns in all tables in FROM clau
PostPosted: Tue May 09, 2006 9:29 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Code:
criteria = getSession().createCriteria(JournalLine.class);
criteria.createAlias("journal", "jou").createAlias("jou.journalType", "jty").add(Restrictions.ne("jty.txnNature", JournalType.TxnNature.CASH));
criteria.list();
criteria.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY );


Have you tried setting distinct root entity option on result transformer as shown above?


Top
 Profile  
 
 Post subject: Re: Hibernate selects all columns in all tables in FROM clau
PostPosted: Wed May 10, 2006 4:07 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
Thanks for helping but this does not work. As far as I understand, the DISTINCT_ROOT_ENTITY is used for selecting distinct rows of root entity and does not affect columns in SELECT clause in the query.

Thanks
Shantanu


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 8:09 am 
Senior
Senior

Joined: Mon Apr 04, 2005 8:04 am
Posts: 128
Location: Manchester, NH USA
I haven't tried this out myself, but isn't that the intention of setting lazy loading at the class level?

I'm not sure what your goal is - in your example would you want Hibernate to only select the columns in the JournalLine class, and not in the JournalType class?


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 9:06 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
No lazy loading will happen in case of any collections that are set for it but when I join multiple tables in WHERE clause, hibernate selects ALL columns from ALL tables i.e. in my case it is selecting all columns from JournaLine, JournalType and Journal while I wanted to select only JournalLine columns.

I was wondering whether there is any way to tell hibernate to select columns from Root entity only and not other entities.

Thanks
Shantanu


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 10, 2006 10:08 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
To give you hint you can use DetachedCriteria which will atleast result in columns of only JournalLine.class but with subqueries.

Something like

Code:
DetachedCriteria jtDC = DetachedCriteria.forClass( JournalType.class, "jty" );
jtDC.add( Restrictions.ne( "jty.txnNature", JournalType.TxnNature.CASH ) );
jtDC.setProjection( Property.forName( "jty.join_column_name" ) );

DetachedCriteria jDC = DetachedCriteria.forClass( Journal.class, "j" );
jDC.setProjection( Property.forName( "j.join_column_name" ) );

DetachedCriteria jLineCrit = DetachedCriteria.forClass( JournalLine.class, "jLine" );
jLineCrit.add( Property.forName( "jLine.join_column_name" ).eq( jtDC ) );
jLineCrit.add( Property.forName( "jLine.join_column_name" ).eq( jDC ) );   
List results = jLineCrit.getExecutableCriteria(session).list();


The snippet may not be correct, atleast it will give you idea how to extract only columns from one persistent class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 5:38 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
Thanks for help. The technique seems to be working. There is a issue though. I wonder whether this is hibernate bug. Here is working code.


DetachedCriteria jtyCriteria = DetachedCriteria.forClass(JournalType.class, "jty");
jtyCriteria.add(Restrictions.ne("jty.txnNature", JournalType.TxnNature.CASH));
jtyCriteria.setProjection(Projections.property("jty.code"));

DetachedCriteria jouCriteria = DetachedCriteria.forClass(Journal.class, "jou");
jouCriteria.setProjection(Projections.property("jou.SID"));
jouCriteria.add(Property.forName("jou.journalType").in(jtyCriteria));
jouCriteria.getExecutableCriteria(getSession()).list();

DetachedCriteria criteria = DetachedCriteria.forClass(JournalLine.class, "jli");

criteria.add(Property.forName("jli.journal").in(jouCriteria));
criteria.getExecutableCriteria(getSession()).list();


This code is creating subquery in subquery.

If I do not put the line in bold then I get following exception. Any idea?

java.lang.NullPointerException
at org.hibernate.criterion.SubqueryExpression.getTypedValues(SubqueryExpression.java:80)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getQueryParameters(CriteriaQueryTranslator.java:251)
at org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:55)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:333)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1514)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy24.list(Unknown Source)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
at ......


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 9:30 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
[quote="shantanu"]jouCriteria.getExecutableCriteria(getSession()).list();[quote]

Thanks for posting the solution. It helps.

Similar to you I wasnt using the above quoted line and getting the same exception. Having ran out of ideas, I thought maybe Hibernate Criteria doesnt allow subqueries in a subquery.
I still dont know why that exception is coming.


Top
 Profile  
 
 Post subject: Can Someone in hibernate team look at it?
PostPosted: Thu May 11, 2006 10:45 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
Is this a bug in hibernate? How can we do subquery in subquery?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 10:56 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
I wouldnt call it as bug just because I dont know how to work with it. Its true there is very less documentation about it.


Top
 Profile  
 
 Post subject: Subquery within subquery
PostPosted: Fri May 12, 2006 4:51 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
Agreed. It may not be a bug but can someone from Hibernate team let me know how can I do subquery within a subquery?

Thanks
Shantanu


Top
 Profile  
 
 Post subject: Use Projections
PostPosted: Fri Aug 11, 2006 6:24 am 
Newbie

Joined: Tue May 09, 2006 4:54 am
Posts: 13
One solution to the original problem of Hibernate selecting all columns is to use projections.

Please look at

http://forum.hibernate.org/viewtopic.ph ... projection

This shows how to populate objects directly from projections. Ony disadvantage is that the objects created are transient objects.


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