我有一个问题,一个Entity
包含一个unique_key
所以我可能需要的是创建Entity
与两个PrimaryKey
甚至可能吗?
这是我的Entity
@Entity
public class UserAnswerQuestion extends DateAudit {
@Id
@Column(name = "useranswerquestion_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "useranswerquestion_seq")
@SequenceGenerator(name = "useranswerquestion_seq", allocationSize = 1)
private Long id;
@ManyToOne
private User user;
@ElementCollection
private List<Answer> answerList;
@ManyToOne
private Question question;
private Boolean passed;
private Boolean shown;
public UserAnswerQuestion(){
}
....
当我尝试用另一个Entity
创建这个User
的问题时,它说:
org.postgresql.util.PSQLException:错误:重复键值违反唯一约束“uk_6v3tlg1gflua8r8d4wlqxo7v5”详细信息:键(answer_list_answer_id)=(11)已存在。
所以我想做的就是尽可能将User
作为@Id
,也许它解决了我的问题......
我做的是,像这样创建一个类UserAnswerQuestionId
:
public class UserAnswerQuestionId implements Serializable {
@Column(name = "useranswerquestion_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "useranswerquestion_seq")
@SequenceGenerator(name = "useranswerquestion_seq", allocationSize = 1)
private Long id;
@Column(name ="user_id")
private Long user_id;
public UserAnswerQuestionId(){
}
public UserAnswerQuestionId(Long user_id) {
this.user_id = user_id;
}
然后在UserAnswerQuestion
实体中我将其更改为:
@EmbeddedId
private UserAnswerQuestionId userAnswerQuestionId;
但错误现在说:
org.hibernate.id.IdentifierGenerationException: null id generated for:class com.pew.model.useranswers.UserAnswerQuestion
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:192) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:62) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:800) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:785) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
我正在仔细阅读错误,看起来问题就在于
@ElementCollection
private List<Answer> answerList;
我如何解决这个问题,以便在这个Entity
上重复这个元素?
也许我可以定义那些@ElementCollection不是唯一的?那么可以在这个实体上重复一次吗?
这是我的Answer
实体
@Entity(name = "answer")
public class Answer extends DateAudit {
@Id
@Column(name = "answer_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answer_seq")
@SequenceGenerator(name = "answer_seq", allocationSize = 1)
private Long id;
@Column(name = "answerToQuestion")
private String answerToQuestion;
也许您可能需要更好地解释您的用例。从上下文信息看起来,您想要检索用户和他们为各种问题提供的答案。在这种情况下,您可能只需要一个如下所示的关联表。
public class User extends DateAudit { //This assumes you are intested in retrieving User and their answer(s) in the domain model.
@Id
private Long id; //userid.
@ManyToMany
@JoinTable(name = “ANSWERS_FOR_QUESTIONS”, //More appropriate name may be : ANSWERS_BY_USERS
joinColumns = { @JoinColumn(name = “user_id”) }, //fk
inverseJoinColumns = { @JoinColumn(name = “question_id”) }) //fk
private Set<Answers> answers = new HashSet<Answers>();
主键始终只有1且唯一。它可以是由多列元组组成的复合键。
使用UserAnswerQuestionId
包括你的@IdClass
应该工作:
@Entity
@IdClass(UserAnswerQuestionId.class)
public class UserAnswerQuestion extends DateAudit {
@Id
private Long id;
@Id
private Long user_id;
@ElementCollection
private List<Answer> answerList;
@ManyToOne
private Question question;
private Boolean passed;
private Boolean shown;
public UserAnswerQuestion(){
}