In a very distilled version of the sample code available from a tutorial, Embedded Compound Primary Key : Primary Key « JPA « Java Tutorial, I'm getting:
javax.persistence.PersistenceException: [PersistenceUnit: unit] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at [my code that calls e.persist on a Student]
The exception is fairly generic, but Hibernate provides some good log debug info (I've replaced the actual package name with <package>):
[DEBUG] org.hibernate.boot.internal.ClassLoaderAccessImpl: Not known whether passed class name [<package>.Student] is safe
[DEBUG] org.hibernate.boot.internal.ClassLoaderAccessImpl: No temp ClassLoader provided; using live ClassLoader for loading potentially unsafe class : <package>.Student
Here's the distilled code. (The backstory is that I'd been trying, to no avail, to create an entity with an embedded id. After a while of trying to debug that, I re-started with this tutorial code, removing things until I got the same error.)
@Embeddable
class StudentId {
private int id;
public StudentId() {}
public StudentId(int id) { this.id = id; }
@Override
public boolean equals(Object o) {
return ((o instanceof StudentId) && id == ((StudentId) o).id);
}
@Override
public int hashCode() { return id; }
}
@Entity
public class Student {
@EmbeddedId
private StudentId id;
public Student() {}
public Student(int id) { this.id = new StudentId(id); }
}