Liferay Serivce Builder: Not able to run dynamic query
Asked Answered
C

3

5

I have two plugin portlets. First has service builder with all entities. Second portlet is using service's jar file to execute Dynamic query.

I am using first's service jar in my second plugin portlet to interact with database. But in this jar file there is not any Impl class. Thats why i am getting error Impl Class not found. Below is for reference:

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(XXX.class,
PortletClassLoaderUtil.getClassLoader());
try {
    XXXLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}

Error: [DynamicQueryFactoryImpl:96] Unable find model com.compass.model.impl.XXXImpl java.lang.ClassNotFoundException: com.compass.model.impl.XXXImpl

Nomal Functions are working fine of service builder

Cartan answered 28/3, 2017 at 10:30 Comment(1)
Closing the thread.Cartan
E
6

just don't use the DynamicQueryFactoryUtil but the XXXLocalServiceUtil this way

DynamicQuery dynamicQuery = XXXLocalServiceUtil.dynamicQuery() 
try {
    XXXLocalServiceUtil.dynamicQuery(dynamicQuery);
} catch (SystemException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}

If you want to use the factory you have to use the interface model not the impl of the entity so if you have an entity FooImpl sue the Foo.class and use the classloder of you service plugin portlet

Classloader cl =(ClassLoader) PortletBeanLocatorUtil.locate("services-portlet", "portletClassLoader");
DynamicQueryFactoryUtil.forClass(XXX.class, cl);
Eatables answered 28/3, 2017 at 11:33 Comment(5)
Hi @romeo, In my XXX-service.jar there is not any XXXImpl class. That's why i am getting the error.Cartan
By your example it is giving 'java.lang.RuntimeException: java.lang.ClassNotFoundException is not a valid exception' error.Cartan
you don't need the impl class just the interface of that class. you don't have access to impl the services work this wayEatables
I have added first's XXX-service.jar in lib folder and also tried with adding it into tomcat lib/ext folder. But while getting the data with dynamic query. it is giving error '[DynamicQueryFactoryImpl:96] Unable find model com.compass.model.impl.XXXImpl'.Cartan
sorry i've misunderstand the problem you have to user the service-portlet classloader and not the portalclassloader. the portalclassloader is for liferay entities. i've update the answer. But if you use the method XXXLocalServiceUtil.dynamicQuery() to get the dynamic query you will not have problem like this.Eatables
C
1

Both works for me..

ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(), "portletClassLoader");

OR

ClassLoader classLoader = PortletBeanLocatorUtil.getBeanLocator(ClpSerializer.getServletContextName()).getClassLoader();

DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(charges.class,classLoader);

Thanks @Romeo.

Cartan answered 29/3, 2017 at 4:14 Comment(0)
I
0
public static void getuserList(){
        DynamicQuery dynamicQuery = XXLocalServiceUtil.dynamicQuery();
        System.out.println("dynamicQuery");
        dynamicQuery.add(PropertyFactoryUtil.forName("field1").eq("field_name"));
        List<XX> userList = XXLocalServiceUtil.dynamicQuery(dynamicQuery);
        System.out.println(userList);
}

This is working for me !!!!

Implication answered 22/11, 2020 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.