In a very distilled version of the sample code available from the tutorial, Built-in composite key: main key "JPA" Java Tutorial ", I 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 pretty general, but Hibernate provides some good log debugging information (I replaced the actual package name <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 is the distilled code. (The last background is that I tried, but to no avail, to create an object with a built-in identifier. After some time, trying to debug it, I started again with this tutorial code, deleting everything 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); }
}