掲示板

Getting a Hibernate error when calling service builder method from JS

6年前 に Pablo Yuste Soto によって更新されました。

Getting a Hibernate error when calling service builder method from JS

New Member 投稿: 14 参加年月日: 17/10/03 最新の投稿
Hi again!

I'm now getting a very strange Hibernate error when I try to consume some web service from Javascript. I will describe what I did for now:

1. I created a class "Persona" (which stands for "person" in spanish) and generated with Service Builder all its methods.
2. Modified its entry in service.xml and added support for Remote Service and set the uuid to true. Re-run service builder.
3. In my portlet, the same in which service.xml is located, I created in PersonaServiceImpl a method "findByAge" made with dynamic queries. This is the method:

// more imports
import com.services.model.Persona;

// class definition and so

	public List<persona> findByAge(int age){

		
		DynamicQuery eventQuery = DynamicQueryFactoryUtil.forClass(Persona.class)
			    .add(RestrictionsFactoryUtil.between("edad", 10, 14))
				.add(RestrictionsFactoryUtil.eq("nombre", "Persona de prueba (13añ)"));
		
		return PersonaLocalServiceUtil.dynamicQuery(eventQuery);
	}</persona>


This code works good when being called from the portlet controller's class. I can see the info in my JSP and nothing goes wrong. Except when I tried going to the jsonws admin page, copied the Javascript example code for calling this method, and tried it by myself:

ERROR [http-nio-8080-exec-6][JSONWebServiceServiceAction:97] org.hibernate.MappingException: Unknown entity: com.services.model.Persona


What do you think it's wrong here? I need to fix this :/
thumbnail
6年前 に David H Nebinger によって更新されました。

RE: Getting a Hibernate error when calling service builder method from JS

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Instead of trying to create the DQ yourself, just do the following:

public List<persona> findByAge(int age){
  dq = personaLocalService.dynamicQuery();

  dq..add(RestrictionsFactoryUtil.between("edad", 10, 14))
    .add(RestrictionsFactoryUtil.eq("nombre", "Persona de prueba (13añ)"));

  return personaLocalService.dynamicQuery(dq);
}</persona>


Two things here - first, you let the local service impl create the DQ instance so that solves your possible class loader issues. Second, you should have a personaLocalService injected into the class so you always want to avoid the static util classes when you can.

Additionally, I would recommend moving this method to the PersonaLocalServiceImpl class. You're not permission checking yet and that too removes possible class loader issues. Then in your PersonaServiceImpl's findByAge() method, you're just passing through to personaLocalService.findByAge(). Much cleaner IMHO.







Come meet me at 2017 LSNA!
6年前 に Pablo Yuste Soto によって更新されました。

RE: Getting a Hibernate error when calling service builder method from JS

New Member 投稿: 14 参加年月日: 17/10/03 最新の投稿
Thank you David! This worked perfectly. It seems that the big problem was located in how I was creating the dynamic query; now everything works great.

Thank you again!
thumbnail
6年前 に David H Nebinger によって更新されました。

RE: Getting a Hibernate error when calling service builder method from JS

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Yep, that can make all the difference.









Come meet me at 2017 LSNA!