Jatin
Jatin

Reputation: 31754

Access Role data from db

In liferay, I am trying to access the role data available in the database using the API.

I am using the com.liferay.portal.service.RoleLocalServiceUtil class to access Role's. Below is my code:

Role role = RoleLocalServiceUtil.getRole(10138);
ans = ans+role.getName()+","+role.getRoleId();

Role by Id 10138 exists in the database.

But the above throws the below exception: com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set com.liferay.portal.kernel.bean.PortalBeanLocatorUtil.locate(PortalBeanLocatorUtil.java:33) com.liferay.portal.service.RoleLocalServiceUtil.getService(RoleLocalServiceUtil.java:446) com.liferay.portal.service.RoleLocalServiceUtil.getRole(RoleLocalServiceUtil.java:166) com.bl.retail.controller.KPIController.changeAccess(KPIController.java:70) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) ..........

How do I set the BeanLocator? I have spent so much stupid time on it, please help!

PS: The above code is from a seperate servlet running on the same liferay server.

Upvotes: 0

Views: 396

Answers (2)

Jatin
Jatin

Reputation: 31754

Answering my own question:

This was happening because the role data access was done from another Servlet.

When I used the same code from within the portlet it worked all fine.

Upvotes: 0

Tator
Tator

Reputation: 556

try to add
ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.SERVLET_CONTEXT_NAME,"portletClassLoader");
before you call the getter. See http://www.liferay.com/zh/community/forums/-/message_boards/message/13302979 and http://liferay-blogging.blogspot.de/2011/03/dynamicquery-api-and-classloaders-in.html for ref.

Hope it helps you, regards

Upvotes: 1

Related Questions