Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: SessionFactory Throwing Exception in Custom SQL
Hello Friends,
I am using Liferay 7.2 .
I have created Custom SQL and inside FinderImpl, trying to access SessionFactory but it's showing error as,
com.liferay.portal.kernel.bean.BeanLocatorException with No bean named 'liferaySessionFactory' available.
Following is my code,
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
List<User> lrUsers = new LinkedList<User>();
List<Long> userOrgIds = getUsersAllOrgIds(userSoldToOrgId);
try {
session = sessionFactory.openSession();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(" SELECT DISTINCT u.* FROM User_ u, Users_Orgs uo, Organization_ org ")
.append(" WHERE u.userId = uo.userId ")
.append(" and org.organizationId = uo.organizationId ")
.append(" and org.organizationId in ( " + userOrgIdsCommaSeparated + " ) ")
.append(" ORDER BY u.firstName, u.lastName ");
SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
sqlQuery.setCacheable(true);
sqlQuery.addEntity("User_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
lrUsers = (List<User>) QueryUtil.list(sqlQuery, getDialect(), start, end);
Please help me to solve it.
Thanks in advance.
I am using Liferay 7.2 .
I have created Custom SQL and inside FinderImpl, trying to access SessionFactory but it's showing error as,
com.liferay.portal.kernel.bean.BeanLocatorException with No bean named 'liferaySessionFactory' available.
Following is my code,
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
List<User> lrUsers = new LinkedList<User>();
List<Long> userOrgIds = getUsersAllOrgIds(userSoldToOrgId);
try {
session = sessionFactory.openSession();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(" SELECT DISTINCT u.* FROM User_ u, Users_Orgs uo, Organization_ org ")
.append(" WHERE u.userId = uo.userId ")
.append(" and org.organizationId = uo.organizationId ")
.append(" and org.organizationId in ( " + userOrgIdsCommaSeparated + " ) ")
.append(" ORDER BY u.firstName, u.lastName ");
SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
sqlQuery.setCacheable(true);
sqlQuery.addEntity("User_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
lrUsers = (List<User>) QueryUtil.list(sqlQuery, getDialect(), start, end);
Please help me to solve it.
Thanks in advance.
Try openSession() like in this finder:https://github.com/liferay/liferay-portal/blob/master/modules/apps/blogs/blogs-service/src/main/java/com/liferay/blogs/service/persistence/impl/BlogsEntryFinderImpl.java
Assuming you're in your own module, I think this won't work. An option you can do is get the userIds in your query and then retrieve the User object from the userId from the user local service.
sqlQuery.addEntity("User_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
Assuming you're in your own module, I think this won't work. An option you can do is get the userIds in your query and then retrieve the User object from the userId from the user local service.
Hi Amos Fong,
I have tried based on your suggestion and below is my code,
I have tried based on your suggestion and below is my code,
User userObj = UserLocalServiceUtil.getUser(userId);
sqlQuery.addEntity("User_", userObj.getClass());
lrUsers = (List<User>) QueryUtil.list(sqlQuery, getDialect(), start, end);
Afterward, got error message like, "com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.MappingException: Unknown entity: com.liferay.portal.model.impl.UserImpl"Let me know if you need any more details.
Oh I think I wasn't clear. Even if you get the User object in addEntity, the hibernate mappings are still in the portal classloader (see the error you get).I meant to change your SQL query to (select userId from User_....), then with that userId get the actual users back.
SessionFactory sessionFactory = (SessionFactory) PortalBeanLocatorUtil.locate("liferaySessionFactory");
Session session = null;
List<User> lrUsers = new LinkedList<User>();
List<Long> userOrgIds = getUsersAllOrgIds(userSoldToOrgId);
try {
session = sessionFactory.openSession();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(" SELECT DISTINCT u.* FROM User_ u, Users_Orgs uo, Organization_ org ")
.append(" WHERE u.userId = uo.userId ")
.append(" and org.organizationId = uo.organizationId ")
.append(" and org.organizationId in ( " + userOrgIdsCommaSeparated + " ) ")
.append(" ORDER BY u.firstName, u.lastName ");
SQLQuery sqlQuery = session.createSQLQuery(sqlBuilder.toString());
sqlQuery.setCacheable(true);
sqlQuery.addEntity("User_", PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
lrUsers = (List<User>) QueryUtil.list(sqlQuery, getDialect(), start, end);
Hi ,
Above code is correct and is working fine till 7.1 , its throwing " com.liferay.portal.kernel.bean.BeanLocatorException with No bean named 'liferaySessionFactory'" in 7.3 also , Below is a workaround which is working in 7.3 also might work in 7.2.
public List<user> searchAllUser() {
Session session = _basePersistence.openSession();
String sql = "SELECT * FROM USER_ WHERE companyId = " + PortalUtil.getDefaultCompanyId();
SQLQuery q = session.createSQLQuery(sql);
q.setCacheable(Boolean.TRUE);</user>
try {
q.addEntity("User_",
PortalClassLoaderUtil.getClassLoader().loadClass("com.liferay.portal.model.impl.UserImpl"));
} catch (ClassNotFoundException e) {
_log.error(e);
}
List<user> users = (List<user>) QueryUtil.list(q, getDialect(), -1, -1);
session.close();
return users;
}
@Reference
private BasePersistence _basePersistence;</user></user>
Thank You Mohammed Yasin,
It's working fine for me.
It's working fine for me.
This also works:
Session session = UserLocalServiceUtil.getService().getBasePersistence().openSession();
Session session = UserLocalServiceUtil.getService().getBasePersistence().openSession();
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™