Msgstr "It is not known whether the class name passed ... safely" using JPA EmbeddedId with Hibernate

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); }
}
+4
1

. StudentId Serializable ( serialVersionUID), , , :

@Embeddable
class StudentId implements Serializable {

    private static final long serialVersionUID = -7415410969017941320L;

    // ...
}

, , . , , Hibernate Serializable?, , Serializable. , . , Serializable?, , id , , ,

OndrejM, , Hibernate?, JPA 1.0, , . , , JSR-000338 Java TM Persistence 2.1 Final Release for Evaluation, :

2.4

& hellip; : & hellip;

  • .
+2

Source: https://habr.com/ru/post/1609529/


All Articles