掲示板

Using a ResultTransformer

thumbnail
7年前 に Pete Helgren によって更新されました。

Using a ResultTransformer

Regular Member 投稿: 225 参加年月日: 11/04/07 最新の投稿
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
7年前 に David H Nebinger によって更新されました。

RE: Using a ResultTransformer

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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
7年前 に Pete Helgren によって更新されました。

RE: Using a ResultTransformer

Regular Member 投稿: 225 参加年月日: 11/04/07 最新の投稿
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
7年前 に David H Nebinger によって更新されました。

RE: Using a ResultTransformer

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
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.