【问题标题】:Hibernate exceptions when accessing existing tables in liferay 6.1?访问liferay 6.1中的现有表时出现休眠异常?
【发布时间】: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


    【解决方案1】:

    您可以在 service.xml 和 XXXLocalServiceImpl.java 中创建空白实体,您可以使用您的代码添加方法(从 db 获取记录),然后您将不会收到有关休眠会话的错误。基本上这个类提供了必要的休眠会话。

    【讨论】:

      猜你喜欢
      • 2018-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多