[英]Can't create composite primary key: Unable to build Hibernate SessionFactory
I try to create a composite primary key but can't.我尝试创建复合主键但不能。
My attempt (there is the tutorial I used):我的尝试(有我使用的教程):
@Entity
@Table(name = "user")
public class User {
@Id
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
//getters, setters, equals, hashcode
}
@Entity
public class PersonalScore {
@EmbeddedId
private PersonalScoreId personalScoreId;
//getters, setters, equals, hashcode, constructors
}
@Embeddable
public class PersonalScoreId implements Serializable {
private User user;
private Date date;
//getters, setters, equals, hashcode, constructors (noargs included)
That's all.就这样。 In the result I got the error:
结果我得到了错误:
> Caused by: javax.persistence.PersistenceException: [PersistenceUnit:
> default] Unable to build Hibernate SessionFactory; nested exception is
> org.hibernate.MappingException: Could not determine type for:
> com...model.dto.User, at table: personal_score, for columns:
> [org.hibernate.mapping.Column(user)]
From EmbeddedId :从EmbeddedId :
Relationship mappings defined within an embedded id class are not supported.
不支持在嵌入式 ID class 中定义的关系映射。
What you need to do is replace the User
field with a field for the user's id ( private long userId
), then use MapsId in your PersonalScore
class to add a relation with User
using the userId
field from the embedded id.您需要做的是将
User
字段替换为用户 id 的字段( private long userId
),然后在PersonalScore
class 中使用MapsId使用嵌入 id 中的userId
字段添加与User
的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.