Reputation: 4041
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.
Upvotes: 3
Views: 17333
Reputation: 1493
In my case I was using Java 17 and a newer version of hibernate-core and trying to import javax.persistence.*
. I started with this example and tried to upgrade hibernate-core to a newer version (6.2.7.Final) and got the error Unable to locate persister
.
Original pom.xml:
<dependencies>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
</dependencies>
Original imports:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
The hibernate-core
dependency pulls in persistence implicitly so explicitly pulling it in like that (and an older version) was my mistake. I just needed to change my pom dependencies to this:
<dependencies>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.2.7.Final</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
</dependencies>
And change my imports to this:
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
... and it all worked fine after that.
Upvotes: 0
Reputation: 41
Try changing the imports from "javax" to "jakarta". I had the same issue and mine worked when I made the change.
Upvotes: 4
Reputation: 4393
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
Upvotes: 0
Reputation: 19
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.
Upvotes: 0