Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 是否可以创建具有两个主键值的实体?_Spring_Hibernate_Jpa_Spring Data Jpa - Fatal编程技术网

Spring 是否可以创建具有两个主键值的实体?

Spring 是否可以创建具有两个主键值的实体?,spring,hibernate,jpa,spring-data-jpa,Spring,Hibernate,Jpa,Spring Data Jpa,我在一个实体中遇到了一个问题,它包含一个唯一的\u键,所以我可能需要用两个主键创建该实体,这可能吗 这是我的实体 @Entity public class UserAnswerQuestion extends DateAudit { @Id @Column(name = "useranswerquestion_id") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "useranswerq

我在一个
实体
中遇到了一个问题,它包含一个
唯一的\u键
,所以我可能需要用两个
主键创建该
实体
,这可能吗

这是我的
实体

@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(){
    }
....
然后在
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]
编辑2 我正在仔细阅读错误,看起来问题出在

@ElementCollection
private List<Answer> answerList;

主键始终仅为1且唯一。它可以是由多列元组组成的复合键。

使用
@IdClass
包括您的
UserAnswerQuestionId
应该可以:

@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(){
    }
@实体
@IdClass(UserAnswerQuestionId.class)
公共类UserAnswerQuestion扩展了DateAudit{
@身份证
私人长id;
@身份证
专用长用户标识;
@元素集合
私人名单;
@许多酮
私人问题;
私有布尔传递;
显示私有布尔值;
公共用户回答问题(){
}

也许您需要更好地解释您的用例。从上下文信息来看,您似乎希望检索用户以及他们为各种问题提供的答案。在这种情况下,您可能只需要如下所示的关联表

public class User extensed DateAudit{//这假设您在域模型中检索用户及其答案时有经验。
@身份证
私有长id;//用户id。
@许多
@JoinTable(name=“ANSWERS\u FOR\u QUESTIONS”,//更合适的名称可能是:ANSWERS\u BY\u USERS
joinColumns={@JoinColumn(name=“user_id”)},//fk
inverseJoinColumns={@JoinColumn(name=“question_id”)})//fk
私有集answers=newhashset();
@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;
@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(){
    }
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>();