问题描述
我们如何使用自定义SQL通过自定义查找器来获取liferay实体?
How can we fetch liferay entities through custom-finder using custom SQL?
-
以下是我用
default.xml
():
Following is my sql query written in
default.xml
():
SELECT
grp.*
FROM
Group_
WHERE
site = 1
AND active_ = 1
AND type_ <> 3
MyCustomGroupFinderImpl.java
中的相关代码:
Relevant code in 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);
}
上面的代码将不起作用,因为portal-impl.jar
中存在GroupImpl
类,并且该jar无法在自定义portlet中使用.
This above code won't work as the GroupImpl
class is present in portal-impl.jar
and this jar cannot be used in custom portlet.
我也尝试使用sqlQuery.addEntity("Group_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.GroupImpl"))
但这上面的代码引发了异常:
I also tried using sqlQuery.addEntity("Group_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.GroupImpl"))
But this above code throws exception:
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);
,则相同的代码也适用于我们的自定义实体.
But the same code works for our custom-entity, if we write sqlQuery.addEntity("MyCustomGroup", MyCustomGroupImpl.class);
.
谢谢
推荐答案
我从 liferay论坛线程中获取liferay门户实体,而不是session = openSession();
我们需要按照以下步骤从liferaySessionFactory
获取会话以使其正常工作:
I found out from the liferay forum thread that instead of session = openSession();
we would need to fetch the session from liferaySessionFactory
as follows to make it work:
// 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的教程,该方法也使用相同的方法.
Hope this helps somebody on stackoverflow, also I found a nice tutorial regarding custom-sql which also uses the same approach.
这篇关于如何通过自定义插件portlet中的自定义查找器获取liferay实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!