RE: org.hibernate.MappingException: Unknown entity: <namespace>.FooImpl

Christopher Virtucio, modified 7 Years ago. Junior Member Posts: 33 Join Date: 9/26/17 Recent Posts

Hi,

 

I created a finder class with a finder method, that gets called by a LocalService, following this documentation: https://dev.liferay.com/es/develop/tutorials/-/knowledge_base/7-1/defining-service-entity-finder-methods. However, calling the methods results in the exception stated in this thread's title. I've gone through several archived threads like this one, but the solutions mentioned don't solve the problem. Specifically, I've tried leveraging the LocalServiceUtil's dynamicQuery API instead of the DynamicQueryFactory, and it still results in the MappingException.

 

Any ideas on a good path I can take?

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Christopher Virtucio:
org.hibernate.MappingException: Unknown entity: <namespace>.FooImpl

The only things I can think of that would result in that exception are either a missing mapping file (so the mapping file does not get included in the service module), or the wrong classloader is used when calling the service (since the classloader governs the mapping file that gets used).

Assuming you're not doing any classloader switching, does the compiled .jar have a module-hbm.xml file in it?

Christopher Virtucio, modified 7 Years ago. Junior Member Posts: 33 Join Date: 9/26/17 Recent Posts
Minhchau Dang:
Christopher Virtucio:
org.hibernate.MappingException: Unknown entity: <namespace>.FooImpl

The only things I can think of that would result in that exception are either a missing mapping file (so the mapping file does not get included in the service module), or the wrong classloader is used when calling the service (since the classloader governs the mapping file that gets used).

Assuming you're not doing any classloader switching, does the compiled .jar have a module-hbm.xml file in it?

 

The hbm file is in the jar.

As for the classLoader, I tried the following:

1) passing just Foo.class to the DynamicQueryFactoryUtil.forClass() method

2) passing Foo.class and the FinderImpl's classLoader to the DynamicQueryFactoryUtil.forClass() method

3) using the FooLocalServiceUtil.dynamicQuery() method (which uses its own classLoader under the hood) to create the query, instead of DynamicQueryFactoryUtil.forClass()

None of these worked. I even stepped through the code in the debugger to verify that the right classLoader was being used, and that the FooImpl class was being retrieved from that classLoader. It's during the call to FooLocalServiceUtiil.dynamicQuery( DynamicQuery dynamicQuery ) that the exception is thrown.

 

EDIT:

Tried using the FooPersistence.findByUserId( long userId ) method (probably created by the service builder in my last run?) instead of using my Finder class. I got a different exception this time:

com.liferay.portal.kernel.exception.SystemException: com.liferay.portal.kernel.dao.orm.ORMException: org.hibernate.QueryException: in expected: foo [SELECT foo FROM Foo foo WHERE foo.userId = ?]

thumbnail
Minhchau Dang, modified 7 Years ago. Liferay Master Posts: 598 Join Date: 10/22/07 Recent Posts
Christopher Virtucio:

Tried using the FooPersistence.findByUserId( long userId ) method (probably created by the service builder in my last run?) instead of using my Finder class. I got a different exception this time:

Strange. If even the Persistence isn't able to figure out what to do with the Hibernate mapping file, that says something is wrong with the thread context classloader, so something somewhere is classloader hopping. Does it work if you swap the thread context classloader to the FinderImpl classloader?

Christopher Virtucio, modified 7 Years ago. Junior Member Posts: 33 Join Date: 9/26/17 Recent Posts
Minhchau Dang:
Christopher Virtucio:

Tried using the FooPersistence.findByUserId( long userId ) method (probably created by the service builder in my last run?) instead of using my Finder class. I got a different exception this time:

Strange. If even the Persistence isn't able to figure out what to do with the Hibernate mapping file, that says something is wrong with the thread context classloader, so something somewhere is classloader hopping. Does it work if you swap the thread context classloader to the FinderImpl classloader?

 

I set the thread context classLoader in the LocalService's findByUserId method to that of the FinderImpl, but it still threw the same exception. The same was true when I set it to the Persistence's classLoader.

 

However, when the classLoader is set to the FinderImpl, calling the FinderImpl's find method no longer throws the Unknown Entity exception. And it also results in an empty list, which is not supposed to be the case since records in the table exist for the given userId that I'm passing (my dynamicQuery has the restriction of Restrictions.eq( "userId", userId )). Calling the method without the restriction also results in an empty list.

 

 

John O'Flaherty, modified 6 Years ago. New Member Posts: 14 Join Date: 1/25/14 Recent Posts

Hi, did you ever manage to track this error down ? 

Christopher Virtucio, modified 6 Years ago. Junior Member Posts: 33 Join Date: 9/26/17 Recent Posts
John O'Flaherty:

Hi, did you ever manage to track this error down ? 

We had <hbmFileName/> configured to portlet-hbm.xml (the pre-DXP naming convention). We filed a LESA ticket and basically, we needed to change that value to module-hbm.xml. They don't mention this in the documentation; they expect you to infer this from their samples. And apparently they've changed the naming convention again in 7.1: it's now portal-hbm.xml (hooray.. more obscure changes).

So the lesson here is, Liferay's documentation just isn't enough.. go through their source code, and make sure your implementations match their samples 1:1.