在教程Embedded Compound Primary Key : Primary Key « JPA « Java Tutorial中提供的示例代码的非常精简版中,我得到:
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 [
这是蒸馏代码. (背景故事是我一直试图创建一个具有嵌入式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),问题似乎消失了:
原文链接:https://www.f2er.com/java/437203.html@Embeddable
class StudentId implements Serializable {
private static final long serialVersionUID = -7415410969017941320L;
// ...
}
一旦我发现了这一点,我就可以做更多的研究,但它有不同程度的帮助.例如,Do Hibernate table classes need to be Serializable?讨论了实体类是否需要可序列化.一般来说,他们没有.另一个问题,Why composite-id class must implement Serializable?,更相关,但使id类不可序列化产生不同于我得到的错误消息.
用户OndrejM在a comment中指出了an answer到How to map a composite key with Hibernate?,它定位了JPA 1.0规范的一部分,该部分规定复合密钥类必须是可序列化的.为了完整起见,后来的规范JSR-000338 JavaTM Persistence 2.1 Final Release for Evaluation中的相关部分是:
2.4 Primary Keys and Entity Identity
… The following rules apply for composite primary keys: …
- The primary key class must be serializable.