-->
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.  [ 7 posts ] 
Author Message
 Post subject: Hibernate does not return the persistent class type object?
PostPosted: Sat Jun 30, 2007 8:05 am 
Newbie

Joined: Sat Jun 30, 2007 7:45 am
Posts: 7
Location: India
Hibernate version:3
Code between sessionFactory.openSession() and session.close():

I am using the following code :

List results = session.createCriteria(MyClass.class)
.setProjection( Projections.projectionList()
.add(Property.forName("Property1"))
.add(Property.forName("Property2"))
.add(Property.forName("Property3"))
.addOrder(Order.desc("Property3"))
.addOrder(Order.asc("Property1"))
.list();

It does not return me Objects of type MyClass but returns object of type
Object class of java.Can anyone help me in getting the object of type MYClass only with these particular properties only.Thanks in Advance.:-)

Name and version of the database you are using: Oracle 10G


Top
 Profile  
 
 Post subject: Re: Hibernate does not return the persistent class type obje
PostPosted: Sat Jun 30, 2007 10:47 am 
Beginner
Beginner

Joined: Thu Mar 18, 2004 8:11 am
Posts: 38
Location: Italy
You probably meant somethings like this:

Code:
List<MyClass> lst = (List<MyClass>)
session.createCriteria(MyClass.class)
  .add(Property.forName("prop1").eq(theProp1ValueIWant))
  .add(Property.forName("prop2").eq(theProp2ValueIWant))
  .add(Property.forName("prop3").eq(theProp3ValueIWant))
  .addOrder(Property.forName("prop3").desc())
  .addOrder(Property.forName("prop1").asc())
.list()
;


setProjection() is meant exactly to specify the fields you want to be returned by a query, thereby a Criteria query using it want not return a specific class, but an array (o a list, can't remeber) of the specified fields for each row.

All this, of course, if I'm not wrong: I quit using Criteria because of some problems with border cases. This happened some months ago, so forgive me if my reply can't be more exact.

Giampaolo

_________________
Giampaolo Tomassoni
Italy


Top
 Profile  
 
 Post subject: Re: Hibernate does not return the persistent class type obje
PostPosted: Sat Jun 30, 2007 11:04 am 
Beginner
Beginner

Joined: Thu Mar 18, 2004 8:11 am
Posts: 38
Location: Italy
If, instead, you meant you want MyClass instances with only that properties loaded, well, you can't get that by Criteria or HQL queries. Even better, you can't get that at all in Hibernate.

You may, however, define some of the properties in your MyClass mappings as "lazy loaded", which means they are effectively loaded only when your code references it. If they will not be referenced, then they will never be loaded.

This may or may not be helpful in order to increse performaces, however. As a rule of thumb, it may help appling lazy loading to "blogs", "clogs" and referred entities, while lazy-loading other field type (integers, floatings, short strings etc) is more prone to decrease performances.

In general, use lazy loading with fields which wouldn't be loaded by a single table SELECT anyway (referred entities on most db engines, as well as "blogs" and "clogs" on some of them).

_________________
Giampaolo Tomassoni
Italy


Top
 Profile  
 
 Post subject: Re: Hibernate does not return the persistent class type obje
PostPosted: Sat Jun 30, 2007 12:05 pm 
Newbie

Joined: Sat Jun 30, 2007 7:45 am
Posts: 7
Location: India
[quote="g.tomassoni"]If, instead, you meant you want MyClass instances with only that properties loaded, well, you can't get that by Criteria or HQL queries. Even better, you can't get that at all in Hibernate....[quote]


Thanks Giampaolo,

It feels good atlast someone answered my query.And with such details.
I really appreciate you.Thanks a lot...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 02, 2007 3:58 am 
Regular
Regular

Joined: Mon Mar 26, 2007 12:38 am
Posts: 119
Hi,
Use ResultTransformers.

List results = session.createCriteria(MyClass.class)
.setProjection( Projections.projectionList()
.add(Property.forName("Property1"))
.add(Property.forName("Property2"))
.add(Property.forName("Property3"))
.addOrder(Order.desc("Property3"))
.addOrder(Order.asc("Property1"))
.setResultTransformer(new AliasToBeanResultTransformer(MyClass.class))

.list();



-----------------------------------------------
Rate the reply if you find it helpful


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 4:44 am 
Newbie

Joined: Sat Jun 30, 2007 7:45 am
Posts: 7
Location: India
[quote="pramodkp"]Hi,
Use ResultTransformers.

[i][color=green]List results = session.createCriteria(MyClass.class)
.setProjection( Projections.projectionList()
.add(Property.forName("Property1"))
.add(Property.forName("Property2"))
.add(Property.forName("Property3"))
.addOrder(Order.desc("Property3"))
.addOrder(Order.asc("Property1"))[b]
.setResultTransformer(new AliasToBeanResultTransformer(MyClass.class))[/b]
.list(); [/color][/i]



Thanks a lot pramod you have solved the problem of my programming carrier.I was very irritated with this problem ,every time i got stuck at this problem only.Thanks thanks thanks.


Last edited by HibernatePassion on Fri Jul 06, 2007 4:46 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 06, 2007 4:44 am 
Newbie

Joined: Sat Jun 30, 2007 7:45 am
Posts: 7
Location: India
[quote="pramodkp"]Hi,
Use ResultTransformers.

[i][color=green]List results = session.createCriteria(MyClass.class)
.setProjection( Projections.projectionList()
.add(Property.forName("Property1"))
.add(Property.forName("Property2"))
.add(Property.forName("Property3"))
.addOrder(Order.desc("Property3"))
.addOrder(Order.asc("Property1"))[b]
.setResultTransformer(new AliasToBeanResultTransformer(MyClass.class))[/b]
.list(); [/color][/i]


Thanks a lot pramod you have solved the problem of my programming carrier.I was very irritated with this problem ,every time i got stuck at this problem only.Thanks thanks thanks.


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