3

I am trying to execute some Java with the Maven exec plugin, and am getting an error Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: my.biz.CoolEntity. I tried a number of failed solutions, before ultimately finding a hacky fix. Based on the hacky fix, I believe the issue to be a runtime classpath error.

I am looking for an answer that will tell me how to address the problem without resorting to copying the needed files into place, as I do in the hacky fix.

Background

I have a Maven project "Project A" which has dependencies upon Hibernate 5.0.1.Final as well as another project "Project B". Project B contains my JPA entity classes, including my.biz.CoolEntity.

Normally, I build everything into a shaded JAR and run/deploy that, without any issues.

Hibernate issue

Recently, I began using Maven's exec:java goal, but am running into what on the surface is a Hibernate issue. when I run mvn exec:java -DmainClass=my.biz.CoolEntity, I get this error:

Caused by: org.hibernate.UnknownEntityTypeException: Unable to locate persister: my.biz.CoolEntity
        at org.hibernate.internal.SessionFactoryImpl.locateEntityPersister(SessionFactoryImpl.java:792) ~[hibernate-core-5.0.1.Fi
nal.jar:5.0.1.Final]
        at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2641) ~[hibernate-core-5.0.1.Final.jar:5.0.1
.Final]
        at org.hibernate.internal.SessionImpl.access$2500(SessionImpl.java:164) ~[hibernate-core-5.0.1.Final.jar:5.0.1.Final]
        at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2579) ~[hibernate-core-5.0.1.Final
.jar:5.0.1.Final]
        at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2566) ~[hibernate-core-5.0.1.Final
.jar:5.0.1.Final]
        at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1044) ~[hibernate-core-5.0.1.Final.jar:5.0.1.Final]
        at org.hibernate.internal.SessionImpl.get(SessionImpl.java:955) ~[hibernate-core-5.0.1.Final.jar:5.0.1.Final]
        at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1075) ~[hibernate-entitymanager-5.
0.1.Final.jar:5.0.1.Final] 

Failed attempts

I tried making use of the exec plugin's <additionalClasspathElements> and <includePluginDependencies>. The first solution didn't result in any different, and the second resulted in a compile-time error.

Hacky fix

What ended up working for me was copying the compiled classes from Project B into Project A.

Community
  • 1
  • 1
maxenglander
  • 3,991
  • 4
  • 30
  • 40

4 Answers4

2

Try changing the imports from "javax" to "jakarta". I had the same issue and mine worked when I made the change.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 08 '22 at 14:33
  • 1
    This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32199617) – Japhei Jul 10 '22 at 11:05
1

When the entities are residing in a different jar, you have to include it in persistence.xml. <jar-file>entities-packaged.jar</jar-file>. Different options are explained here

Community
  • 1
  • 1
ravthiru
  • 8,878
  • 2
  • 43
  • 52
0

When we use Hibernate 5 jars, then we get

org.hibernate.UnknownEntityTypeException: Unable to locate persister".

I just changed the hibernate jar versions from 5 to 4 and then everything was fine. Hibernate 5 does not locate the entity classes even after mapping.

Alexander
  • 4,420
  • 7
  • 27
  • 42
Lovish
  • 19
  • 3
0

I too got the same exception and it was due to missing the entity annotation @Entity for the class. Please check if the @Entity annotation is available in your class

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58