如何通过定制,搜索器自定义插件门户获取Liferay的实体?(How to fetch lifera

2019-08-20 15:29发布

我们如何使用自定义的SQL获取通过自定义的Liferay取景实体?

  1. 以下是我写的SQL查询default.xml这样的逻辑仍然很简单我已经下调查询到最低限度由于它包含的一些功能,并加入我们不能使用。 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和该罐不能在自定义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);

谢谢

Answer 1:

我从发现Liferay的论坛主题 ,与其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);
}

希望这有助于在计算器别人,我也找到了一个不错的教程关于定制SQL也采用了同样的方法。



文章来源: How to fetch liferay entity through custom-finder in custom plugin portlet?