-->
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: Handling Specific Columns using Projections.
PostPosted: Fri Dec 28, 2007 10:29 am 
Newbie

Joined: Fri Dec 28, 2007 10:12 am
Posts: 1
How do I select lets say 2 columns from a select statement instead of all columns?

When I specify the 2 columns in a Projection list, the iterator is giving me an object that is not Castable to the Entity class (Trade), and therefore, I am unable to invoke a getXXX for these columns.



Code:

      Criteria criteria = session.createCriteria(Trade.class);

      // SELECT specific columns.
      ProjectionList projectionList = Projections.projectionList();
      projectionList.add(Projections.property("order_num")).add(
            Projections.property("del_source"));

      iter = criteria.setProjection(projectionList).list().iterator();

      // FIXME - How to handle the specific columms from the ResultSet?
      while (iter.hasNext())
         System.out.println(((Trade)iter.next()).getOrder_Num()); <--ERROR


Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;
   at hibernate.client.TradeClient.main(TradeClient.java:91)



Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 28, 2007 10:39 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
When using projections Hibernate is NOT returning an entity (it can't because it has incomplete data) but a List<Object[]>, that is a List with each element being an array of Objects. See also the Hibernate online documentation (http://www.hibernate.org/hib_docs/v3/re ... ing-tuples)


Top
 Profile  
 
 Post subject: can work around that
PostPosted: Fri Dec 28, 2007 6:01 pm 
Newbie

Joined: Tue Mar 08, 2005 12:24 pm
Posts: 9
Location: Boston
However it is not rocket science to use the property names in the projection to build new objects with only the desired data filled in.

Just don't try to save these objects later or they'll erase most of your data ;)

There are examples on the web and perhaps also HQL syntax to do this automatically.


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.