【问题标题】:How to fetch liferay entity through custom-finder in custom plugin portlet?如何通过自定义插件 portlet 中的自定义查找器获取 liferay 实体?
【发布时间】:2014-07-28 15:59:13
【问题描述】:

我们如何使用自定义 SQL 通过 custom-finder 获取 liferay 实体?

  1. 以下是我用default.xml编写的sql查询(我已将查询缩减到最低限度,以便逻辑保持简单。由于它包含一些函数和连接,我们无法使用DynamicQuery API ):

    SELECT
        grp.*
    FROM
        Group_
    WHERE
        site = 1
        AND active_ = 1
        AND type_ <> 3
    
  2. MyCustomGroupFinderImpl.java中的相关代码:

    Session session = null;
    
    try {
        session = openSession();
    
        // fetches the query string from the default.xml
        String sql = CustomSQLUtil.get(FIND_ONLY_ACTIVE_SITES);
    
        SQLQuery sqlQuery = session.createSQLQuery(sql);
    
        sqlQuery.addEntity("Group_", GroupImpl.class);
        // sqlQuery.addEntity("Group_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.GroupImpl"));
    
        return (List<Group>) QueryUtil.list(sqlQuery, getDialect(), 0, QueryUtil.ALL_POS);
    }
    catch (Exception e) {
        throw new SystemException(e);
    }
    finally {
        closeSession(session);
    }
    

上面的代码无法工作,因为GroupImpl 类存在于portal-impl.jar 中,并且这个jar 不能在自定义portlet 中使用。

我也尝试过使用sqlQuery.addEntity("Group_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.GroupImpl"))
但是上面的代码抛出异常:

com.liferay.portal.kernel.exception.SystemException:
    com.liferay.portal.kernel.dao.orm.ORMException:
        org.hibernate.MappingException:
            Unknown entity: com.liferay.portal.model.impl.GroupImpl

但是如果我们写sqlQuery.addEntity("MyCustomGroup", MyCustomGroupImpl.class);,同样的代码也适用于我们的自定义实体。

谢谢

【问题讨论】:

    标签: sql service liferay liferay-6 builder


    【解决方案1】:

    我从liferay forum thread 发现不是session = openSession(); 我们需要从liferaySessionFactory 获取会话以使其工作:

    // fetch liferay's session factory
    SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
    
    Session session = null;
    
    try {
        // open session using liferay's session factory
        session = sessionFactory.openSession();
    
        // fetches the query string from the default.xml
        String sql = CustomSQLUtil.get(FIND_ONLY_ACTIVE_SITES);
    
        SQLQuery sqlQuery = session.createSQLQuery(sql);
    
        // use portal class loader, since this is portal entity
        sqlQuery.addEntity("Group_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.GroupImpl"));
    
        return (List<Group>) QueryUtil.list(sqlQuery, getDialect(), 0, QueryUtil.ALL_POS);
    }
    catch (Exception e) {
        throw new SystemException(e);
    }
    finally {
        sessionFactory.closeSession(session); // edited as per the comment on this answer
        // closeSession(session);
    }
    

    希望这对 stackoverflow 上的某个人有所帮助,我还发现了一个不错的 tutorial 关于 custom-sql,它也使用相同的方法。

    【讨论】:

    • 对 finally 块的 closeSession 调用会关闭该会话吗?我查看了 liferay 的代码,发现它使用 finder 的 sessionFactory 来关闭会话,因此是我的问题。
    • @user1316487 我认为在这种情况下应该是sessionFactory.closeSession(session)。感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2013-06-17
    • 2021-02-06
    • 2017-08-19
    • 2016-03-04
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多