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: Using HQL's SELECT NEW() with collection in the constructor
PostPosted: Tue May 24, 2005 8:35 am 
Newbie

Joined: Thu Jul 15, 2004 5:06 am
Posts: 2
Hibernate version: 2.1.8

Hi all

Does anyone know if it's possible to use the SELECT NEW() syntax in HQL to retrieve an object if the constructor takes a collection...

e.g. something like:

SELECT NEW MyCat(cat.age, cat.kittens)
FROM Cat cat
WHERE cat.age = 2

i.e. Cat has a number of properties including:
int age;
Set kittens;
String colour;
...etc.

with MyCat having the constructor:
void MyCat(int age, Set kittens) {
this.age = age;
this.kittens=kittens;
}


...I'm struggling with the HQL syntax, and the best I can get is a single kitten object (not a collection) being passed to the MyCat constructor.

Many thanks,
ive


Top
 Profile  
 
 Post subject: Re: Using HQL's SELECT NEW() with collection in the construc
PostPosted: Tue May 24, 2005 9:48 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Ive wrote:
Hibernate version: 2.1.8

Hi all

Does anyone know if it's possible to use the SELECT NEW() syntax in HQL to retrieve an object if the constructor takes a collection...

e.g. something like:

SELECT NEW MyCat(cat.age, cat.kittens)
FROM Cat cat
WHERE cat.age = 2

i.e. Cat has a number of properties including:
int age;
Set kittens;
String colour;
...etc.

with MyCat having the constructor:
void MyCat(int age, Set kittens) {
this.age = age;
this.kittens=kittens;
}


...I'm struggling with the HQL syntax, and the best I can get is a single kitten object (not a collection) being passed to the MyCat constructor.

Many thanks,
ive


Not sure why your doing this but you could pass the whole Cat into the constructor and assign the attributes that way.

Code:
void MyCat(Cat cat) {
   this.age = cat.age;
   this.kittens=cat.kittens;
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 10:24 am 
Newbie

Joined: Thu Jul 15, 2004 5:06 am
Posts: 2
Thanks for the reply, pksiv...

This Cat/MyCat obviously doesn't make any sense, but I need to know if the concept can work...

Basically (due to not wanting to break an existing system), I want to only fetch some of the attributes (mostly of associated objects (to Cat in this case)), without implementing proxies on any objects...

Is this a viable option? ... or am I on the wrong track!?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 24, 2005 2:26 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Ive wrote:
Thanks for the reply, pksiv...

This Cat/MyCat obviously doesn't make any sense, but I need to know if the concept can work...

Basically (due to not wanting to break an existing system), I want to only fetch some of the attributes (mostly of associated objects (to Cat in this case)), without implementing proxies on any objects...

Is this a viable option? ... or am I on the wrong track!?



I built a simple test for this and tried several different query strings and it appears that in order to properly retrieve the collection, you have to do a join. And by doing a join, your results has more than one row per new MyCat(), each constructed with a different kitten. Seems like this functionality is


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.