-->
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.  [ 3 posts ] 
Author Message
 Post subject: Populating only part of an Object
PostPosted: Mon Oct 12, 2009 10:52 am 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
Is it possible to dynamically specify (via the query) which parts of the object are populated .


eg. consider a class
Code:
public class Person {
long id;
String name;
String age;
List<Address> addresses;
List<Long> phoneNumbers;
Company company
}



Is there a way I can chose to populate ONLY one or more (as desired) fields in the query (sort of like dynamic lazy loading)? If I use projections or select Person.a, Person.b ... type syntax, I get a list of Object arrays (which is technically what I want), but I'd really like to be able to cast to a Person object in the query

I looked at projection lists and they are close but still end up with a list of Object arrays.
Thanks


Top
 Profile  
 
 Post subject: Re: Populating only part of an Object
PostPosted: Tue Oct 13, 2009 4:17 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you can use constructors in HQL, something like
Code:
new Person(person.name, person.age) from Person person where ...

provided you add an appropriate constructor to Person class (and don't forget to explicitly declare the no-arguments constructor too).

Keep in mind that the constructed entity is not managed: you can't lazy load non initialized fields after this kind of loading.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Populating only part of an Object
PostPosted: Mon Dec 14, 2009 11:56 am 
Beginner
Beginner

Joined: Thu May 28, 2009 10:25 am
Posts: 21
Thanks, that worked.

Though I think it may introduce a new issue. See viewtopic.php?f=1&t=1001487


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