-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria Query and typesafe Java Result Object
PostPosted: Thu Jan 10, 2008 5:00 am 
Newbie

Joined: Sun Dec 30, 2007 1:08 pm
Posts: 14
Hi,
I know that you can return a typesafe Java object from a HQL Query e.g:

select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr


Is it possible to return a typesafe Java object from a Criteria Query?
Somthing similar like: "Criteria crit = sess.createCriteria(DomesticCat .class).returnAs(Family.class);"

Best regards,

Christian


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 9:51 am 
Newbie

Joined: Wed Nov 02, 2005 4:03 am
Posts: 7
I found this message googling around for exactly the same question.
Unfortunately, no answers so far.
Has anybody any idea?
Can I tell a criteria query projection to construct Java objects just like HQL "select new ..." would do?


Marcus.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 10:01 am 
Newbie

Joined: Wed Nov 02, 2005 4:03 am
Posts: 7
I think I posted too fast ...
Was too exciting finding the problem that I too early stopped to look for the solution.

Class AliasToBeanResultTransformer seems to do the job, or AliasToBeanConstructorResultTransformer.

The former actually has a JavaDoc comment with an example.


Marcus.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 26, 2008 6:41 pm 
Newbie

Joined: Sun Dec 30, 2007 1:08 pm
Posts: 14
Thank you Marcus that solved the problem.
Here is a short example for anyone who might encounter the same problem:


The Result Object:

import java.util.Date;

public class ResultObject {

java.util.Date von;
java.util.Date bis;

public ResultObject() {

}

public java.util.Date getVon() {
return von;
}

public void setVon(java.util.Date von) {
this.von = von;
}

public java.util.Date getBis() {
return bis;
}

public void setBis(java.util.Date bis) {
this.bis = bis;
}

}

The Criteria Query:

@SuppressWarnings("unchecked")
public void testResultObjectWithCriteria() {

Session session = null;
org.hibernate.Transaction tx = null;

try {
session = getSessionFactory().getCurrentSession();

tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Author.class)
.setProjection(
Projections.projectionList().add(
Projections.property("von"),"von").add(
Projections.property("bis"),"bis"));

criteria.setResultTransformer( Transformers.aliasToBean(
ResultObject.class));

List<ResultObject> results = criteria.list();

for (ResultObject r : results) {
System.out.println(" von: " + r.getVon() + " bis: "
+ r.getBis());
}
tx.commit();
} catch (Exception e) {
e.printStackTrace();

if (tx != null) {
tx.rollback();
}

fail(e.getMessage());
} finally {
if (session.isOpen()) {
session.close();
}
}
}


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