-->
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.  [ 8 posts ] 
Author Message
 Post subject: Type safe collections (java 1.5)
PostPosted: Mon Feb 06, 2006 5:41 am 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
Hi,

I'd like to know if there is a way, to use type safe collections for queries. Before posting my question, I've searched the forum for answers, but the best I could find is "yes, hibernate 3 has it, but we do not have a user friendly api for it yet...".

I'd be glad for any answers/pointers on how to do something like:

Code:
   Query query = session.createQuery("select p from Person as p where p.name=:name);
   query.setString("name", "Tom");
   List<Person> persons = query.list();

... without(!) getting warnings about type safety.

Regards,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 6:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
no, unfortunately not.

Maybe ejb3 entitymanger gives you that possibility...i actually dont remember.

I would love it for hibernate3 too, but pretty hard (impossible) to do without loosing compability....

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 7:31 am 
Newbie

Joined: Wed Nov 30, 2005 11:13 am
Posts: 10
Well thanks anyway - at least now I know that I don't have to keep wondering & googling.

So this basically means that we're stuck with non-type safe collections, until Hibernate moves to JDK 1.5, right?

Regards,
Tom


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 7:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes, or someone comes up with an ingenius way of making a backward compatible solution ;)

You could actually make your own wrapper of Query that made this possible if it bothers you too much ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 5:26 pm 
Beginner
Beginner

Joined: Wed Nov 05, 2003 4:38 pm
Posts: 29
Code:
Query query = session.createQuery("select p from Person as p where p.name=:name);
query.setString("name", "Tom");
List<? extends Person> persons = (List<? extends Amount>)query.list();
List<Person> results = new LinkedList<Person>();
results.addAll(persons)

return results;


This is how I do it, while it may be a slight performance hit, it does provide type safety without warnings and also when no records are found it returns an empty list instead of null which helps keep you from writing special checks for null.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 07, 2006 3:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibernate never returns null from a .list() operation, so i would say it is a pretty high price to have a addAll operation instead of a weak warning.

my idea of encapsulation would be:

Code:
Query5 query = new Query5(Person.class, session.createQuery("select p from Person as p where p.name=:name));
   query.setString("name", "Tom");
   List<Person> persons = query.list();


where Query5 then could encapsulate the warnings....i haven't done it, but should be able to work.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 08, 2006 4:42 pm 
Regular
Regular

Joined: Wed Feb 08, 2006 3:59 pm
Posts: 75
I think this is a little bit overenginered and @SuppressWarnings("unchecked") is just fine :)

Anyway you can still get unsafe collections (and therefore runtime errors) by messing with HQL ("from Business ..." in a wrapper expecting "from Person ...")


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 4:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
sure - i didn't say it were perfect; only that the previous one would definitly be inefficient with no added value.

_________________
Max
Don't forget to rate


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