Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate Restrictions

 
Patrick McDonogh
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I am wondering about the best way to get values out of the Objects stored in a list when i do a projection.
i.e.

List matches = session.createCriteria(Cat.class)
.setProjection(Projections.projectionList()
.add(Projections.property("name"))
.add(Projections.property("colour")))
.add(Restrictions.ilike("name", "Mic%"))
.list();

After running this the "matches" list containes Objects which contain two strings. I was expecting the list to contain Cats with only the name and colour properties set. So if there are 10 matches then the "matches" list will contain 10 Objects and each object will contain two strings relating to the name and colour of the cat.

So how do i get the two strings out of the Objects in the list, or set the list to contain a different type of object.
Basically i am confused about what you do with the results in the list and how you get them out or if you can set a different object rather thanthe standard java.lang.Object to be stored in the list.

Thanks a lot.

Patrick
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, in a Projection query you will get a List of Object arrays, two dimensional.

If you want actual objects out of them, you can create an object that has a constructor that takes the projection fields in order that they appear, and just set the class to that object. The object doesn't even have to be mapped to a database table.

In HQL, for example you can do the following

"Select new MyObject(t.a, t.b, t.c) FROM MyTableMappedObject t"

And you will get a List<MyObject> returned instead of the two dimensional Object array.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic