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!