简体   繁体   English

无法创建复合主键:无法构建 Hibernate SessionFactory

[英]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):我的尝试(有我使用的教程):

  • User entity:用户实体:
@Entity
@Table(name = "user")
public class User {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column
    private String name;

    //getters, setters, equals, hashcode
}
  • PersonalScore class:个人分数 class:
@Entity
public class PersonalScore {

    @EmbeddedId
    private PersonalScoreId personalScoreId;
    //getters, setters, equals, hashcode, constructors
}
  • PersonalScoreId class (Primary key): PersonalScoreId class(主键):
@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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM