Issue
I try to create a composite primary key but can't.
My attempt (there is the href="https://Caused%20by:%20javax.persistence.PersistenceException:%20%5BPersistenceUnit:%20default%5D%20Unable%20to%20build%20Hibernate%20SessionFactory;%20nested%20exception%20is%20org.hibernate.MappingException:%20Could%20not%20determine%20type%20for:%20com.hltr.deardiary.model.dto.User,%20at%20table:%20personal_score,%20for%20columns:%20%5Borg.hibernate.mapping.Column(user)%5D" rel="nofollow noreferrer">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)]
Solution
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.
Answered By - Rob Spoor
Answer Checked By - Senaida (JavaFixing Volunteer)