Foren

Using a ResultTransformer

thumbnail
Pete Helgren, geändert vor 7 Jahren.

Using a ResultTransformer

Regular Member Beiträge: 225 Beitrittsdatum: 07.04.11 Neueste Beiträge
Back in 2013 I had a post that David Nebinger replied to my post: https://web.liferay.com/community/forums/-/message_boards/message/23877058

Although I eventually fixed the issue, his additional suggestion was "you should be passing in a ResultTransformer instance to handle the marshalling"

I'd like to now do that, but I am finding very few examples of this in LR 6.0.6 (yeah, I know, it's old. we ARE moving but I still have to keep up this older version)

So the question is: Is there a working example of using a ResultTransformer for LR 6.0.6?

Here is the current code

Query query = session.createSQLQuery(sbsql.toString());
QueryPos qpos = QueryPos.getInstance(query);
if(classType.length()>0)
qpos.add(classType);

locations = query.list();

Based on looking at several examples unrelated to LR I thought this would work:

Query query = session.createSQLQuery(sbsql.toString()).setResultTransformer(Transformers.aliasToBean(ClassGeolocation.class));

But I get the error "The method setResultTransformer(ResultTransformer) is undefined for the type SQLQuery"
Looking for the correct syntax to make this work.

Thanks
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Using a ResultTransformer

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
ResultTransformer is an interface from Hibernate, not from Liferay.

Your previous post mentioned using Hibernate directly, hence the opportunity to leverage the ResultTransformer. Liferay doesn't have a similar facility to do something like that.

Sorry!
thumbnail
Pete Helgren, geändert vor 7 Jahren.

RE: Using a ResultTransformer

Regular Member Beiträge: 225 Beitrittsdatum: 07.04.11 Neueste Beiträge
Thanks! No reason to apologize. You made it sound so simple I figured something must be missing.

I did go the "pure" hibernate route:

try {

SQLQuery query = (SQLQuery) session.createSQLQuery(sbsql.toString()).setResultTransformer(Transformers.aliasToBean(ClassGeolocation.class));

QueryPos qpos = QueryPos.getInstance((Query) query);
if(classType.length()>0)
qpos.add(classType);

locations = query.list();

}
finally{
persistence.closeSession((Session) session);
}

The cast to SQLQuery is the hibernate reference. So the code compiles but I am getting a runtime error of java.lang.NoClassDefFoundError: org/hibernate/Session Caused by: java.lang.ClassNotFoundException: org.hibernate.Session So there is some class loading jujitsu that is going on that prevents the class from being visible. So, it was worth a shot but I'll go back to manually marshaling the columns to the pojo. It's ugly but it works!

Thanks again.
thumbnail
David H Nebinger, geändert vor 7 Jahren.

RE: Using a ResultTransformer

Liferay Legend Beiträge: 14919 Beitrittsdatum: 02.09.06 Neueste Beiträge
Since ROOT opens the hibernate session, it's class loader is where some of those classes are going to come from, not your own. You need the classes, of course, for the compile, but at runtime you're going to be left to choosing the correct class loader all the time.