![](/img/trans.png)
[英]Hibernate JPA IdentifierGenerationException: null id generated for class with @embeddedid
[英]“Not known whether passed class name … is safe” using JPA EmbeddedId with Hibernate
在教程中提供的示例代码的非常精简版本中, 嵌入式复合主键:主键«JPA«Java教程 ,我得到:
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]
异常是相当通用的,但Hibernate提供了一些很好的日志调试信息(我用<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
这是蒸馏代码。 (背景故事是我一直试图创建一个具有嵌入式id的实体无效。经过一段时间尝试调试之后,我重新开始使用这个教程代码,删除内容直到我得到相同的错误。)
@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); }
}
可嵌入类需要可序列化。 更新StudentId的定义以实现Serializable(并添加serialVersionUID),问题似乎消失了:
@Embeddable
class StudentId implements Serializable {
private static final long serialVersionUID = -7415410969017941320L;
// ...
}
一旦我发现了这一点,我就可以做更多的研究,但它有不同程度的帮助。 例如, Do Hibernate表类需要是Serializable吗? ,讨论实体类是否需要可序列化。 一般来说,他们没有。 另一个问题, 为什么composite-id类必须实现Serializable? ,更相关,但使id类不可序列化产生不同于我得到的错误消息。
用户OndrejM在评论中指出了如何使用Hibernate映射复合键 的答案 ? 找到JPA 1.0规范的一部分,该部分规定复合键类必须是可序列化的。 为了完整起见 ,后来的规范JSR-000338 Java TM Persistence 2.1 Final Release for Evaluation的相关部分是:
2.4主键和实体标识
...以下规则适用于复合主键:...
- 主键类必须是可序列化的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.