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:
@Entity
public class PersonalScore {
@EmbeddedId
private PersonalScoreId personalScoreId;
//getters, setters, equals, hashcode, constructors
}
- PersonalScoreId class (Primary key):
@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)]
CodePudding user response:
From EmbeddedId:
Relationship mappings defined within an embedded id class are not supported.
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.