【发布时间】:2013-11-23 17:22:20
【问题描述】:
我正在使用自定义查询来访问 liferay 中的现有实体。由于我没有新实体,所以我创建了一个类,如下所示:
public class TestServiceBuilderPortlet {
public static List<JournalArticle> getList() {
List<JournalArticle> jaList = null;
// fetch liferay's session factory
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
SQLQuery query = null;
try {
session = sessionFactory.openSession(); // Line A
query = session.createSQLQuery("select {ja.*} from journalarticle as ja");
query.setCacheable(false);
query.addEntity("JournalArticle", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portlet.journal.model.impl.JournalArticleImpl"));
QueryPos qPos = QueryPos.getInstance(query);
jaList = query.list();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (session != null) sessionFactory.closeSession(session);
}
return jaList;
}
}
当我调用上述函数时,出现异常:“没有 Hibernate Session 绑定到线程,并且配置不允许在此处创建非事务性会话”
我根据一些来源得到了上面的代码:
http://www.liferay.com/community/forums/-/message_boards/view_message/7158040#_19_message_7166735
How to fetch liferay entity through custom-finder in custom plugin portlet?
我还搜索了上述异常,有一种解决方法:将 hibernate.current_session_context_class=thread 添加到 portal-ext.properties 但它确实对我有用。
我是不是做错了什么?
【问题讨论】:
标签: liferay-6