[英]org.hibernate.MappingException - composite id
在我的 Spring boot - JPA 应用程序中,我试图实现复合键:
@Entity
public class User
{
@Id
private String timeStamp;
@Id
private String firstName;
@Id
private String lastName;
}
这给了我错误,说:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Composite-id class must implement Serializable: com.mua.testkeys.model.User
即使我实现了Serializable
它也会给我错误。
我该如何解决这个问题?
使用:弹簧 + JPA + H2
复合键可以使用@IdClass
创建,如下所示。
用户类
@IdClass(UserPK.class)
@Table(name = "user")
@Entity
public class User {
@Id
private String timeStamp;
@Id
private String firstName;
@Id
private String lastName;
//remaining fields
// getters and setters
}
用户PK.class
public class UserPK {
private String timeStamp;
private String firstName;
private String lastName;
// constructors
// getters and setters
//implement euquels() and hashcode()
}
equals()
和hashcode()
方法。@IdClass(UserPK.class)
注释用户类@Id
注释声明 Id 字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.