-->
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.  [ 1 post ] 
Author Message
 Post subject: Problem with Hibernate 3 Criteria API: How to Project result
PostPosted: Thu Nov 09, 2006 10:29 am 
Newbie

Joined: Sun Oct 16, 2005 11:38 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1.3

Mapping documents:
Company:
<class name="test.Company" table="company">
<id name="id" type="int" access="field">
<column name="company_id" />
<generator class="native">
<param name="sequence">company_id_seq</param>
</generator>
</id>
<property name="name" type="string" access="field">
<column name="name" />
</property>
</class>

PostNet:
<class name="test.PostNet" table="post_net">
<id name="id" type="int" access="field">
<column name="post_net_id" />
<generator class="native">
<param name="sequence">post_net_id_seq</param>
</generator>
</id>
<property name="name" type="string" access="field">
<column name="name" not-null="true" />
</property>
<many-to-one name="managedCompany" class="test.Company"
fetch="select" access="field" insert="false" update="false" lazy="proxy">
<column name="company_id" not-null="true" />
</many-to-one>
</class>


Code between sessionFactory.openSession() and session.close():

Criteria postNetCrit = session.createCriteria(PostNet.class);
//some restriction....
//search Company
Criteria comCrit = postNetCrit.createCriteria("managedCompany");
comCrit.add(Restrictions.ilike("name", "ABC", MatchMode.ANYWHERE));

List<PostNet> find = postNetCrit.list();//<--- exception


java.lang.ClassCastException: [Ljava.lang.Object;

Name and version of the database you are using: PostgreSQL 7.4.3

The generated SQL (show_sql=true):
select this_.name, this_.id, company_.id, company_.name
from post_net p inner join company c on p.company_id = c.company_id
where c.name ilike '%ABC%';


I can resolve this exception by implement ResultTransfomer for getting only PostNet
Code:
postNetCit.setResultTransformer(new ResultTransformer() {
               
      public List transformList(List collection) {
           return collection;
      }
               
      public Object transformTuple(Object[] tuple, String[] aliases) {
                    //don't get tuple[0] because it is Company
                    return tuple[tuple.length  - 1];
      }
               
});
)


Is there a way to Criteria only get PostNet?

Thanks for advices!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.