我遇到一个包含Entity
的{{1}}的问题,所以也许我需要创建带有两个unique_key
的{{1}}甚至有可能吗?
这是我的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(){
}
....
时出现的问题是:
org.postgresql.util.PSQLException:错误:重复的键值违反了唯一约束“ uk_6v3tlg1gflua8r8d4wlqxo7v5” 详细信息:键(answer_list_answer_id)=(11)已存在。
所以我想做的就是尽可能将Entity
设置为User
,也许可以解决我的问题...
我所做的是,像这样创建一个类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定义为不唯一?可以在这个实体上重复吗?
这是我的@ElementCollection
private List<Answer> answerList;
实体
Entity
答案 0 :(得分:1)
也许您可能需要更好地解释用例。从上下文信息中,您似乎想 检索用户及其提供的答案 以解决各种问题。在这种情况下,您可能只需要如下所示的关联表即可。
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 :(得分:0)
主键始终只有1个且唯一。它可以是由多列元组组成的复合键。
答案 2 :(得分:0)
使用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(){
}