-->
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.  [ 6 posts ] 
Author Message
 Post subject: Query on some attributes
PostPosted: Fri Jan 13, 2006 5:15 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
In an example, I have a class that has the attributes: id, name and listCodes. I just want to perform a query that my objects cames with the properties id and name populated, assuming that my list are huge lists and I don´t want to use them.

There is any way of doing this? My reference to the list to each object points to null or to an empty list.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 5:27 pm 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
Shame on me... I didn´t know that select id, name is ok in hql...

But if I try to perform a cast just like this:

Code:
Query q = aSession.createQuery("select oid, nome from DivisaoTerritorial as xyz where xyz.codigoIBGE like '__'");
         List lista = q.list();
         DivisaoTerritorial current = (DivisaoTerritorial)lista.get(0);


I get a ClassCastException (naturally). What can I do?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 5:30 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I think it returns an array of objects for each row in the database matching your criteria, the objects in this case being two String objects

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 5:31 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
to be more clear, it returns a list where each object in the list is an array of objects for each row

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 14, 2006 3:41 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
To make it work, create a constructor in DivisaoTerritorial that takes id and name as arguments (you might have one if u used hbm2java). Once you do that , change your HQL like
Code:
Query q = aSession.createQuery("select new DivisaoTerritorial(oid, nome) from DivisaoTerritorial as xyz where xyz.codigoIBGE like '__'");


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 16, 2006 11:55 am 
Beginner
Beginner

Joined: Mon Oct 03, 2005 5:13 pm
Posts: 30
rajasaur, thanks a lot, it works!

But if I perform a save, having the list null, this will overwrite the corresponding field in the database (update), and I´ll have my list always null. Is there any way of not perform this update of this list?

Example:

Code:
SomeClass object = new SomeClass();
someClass.setDivisaoTerritorial(myDivTerr); //without the lists
session.save(object);


I can do the cascade none for the list, but I would like other way, without messing with the mapping :-)


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