Java 如何为Spring Boot中只有外键的表映射/创建实体类

Java 如何为Spring Boot中只有外键的表映射/创建实体类,java,spring,hibernate,spring-boot,jpa,Java,Spring,Hibernate,Spring Boot,Jpa,我的数据库(Postgres)中有以下表格:问题、回答和问题与回答。 问题和回答表之间存在多对多关系,我已经为这两种关系创建了实体类。我现在必须为question\u respone表创建一个实体映射,该表没有任何主键 我读过关于使用@IdClass或@EmbeddedId的内容,但是,我不确定如何使用这些注释映射两个不同类中的主键 注: 在实施评论中提到的变更后更新实体 谢谢 questions.sql respones.sql 问题_respone.sql# Question.java Re

我的数据库(Postgres)中有以下表格:
问题
回答
问题与回答
。 问题和回答表之间存在多对多关系,我已经为这两种关系创建了实体类。我现在必须为
question\u respone
表创建一个实体映射,该表没有任何主键

我读过关于使用
@IdClass
@EmbeddedId
的内容,但是,我不确定如何使用这些注释映射两个不同类中的主键

注: 在实施评论中提到的变更后更新实体

谢谢

questions.sql respones.sql 问题_respone.sql# Question.java Response.java
您可以将多对多关系分解为一对多对一构造,如下所述:


我真的不明白你的问题是什么。您的@Entitiy类在我看来很不错。@larsgrefer我想将问题\u响应表映射到实体类,但我还没有实体类,因为问题\u响应表中没有主键,它包含两个外键,它们映射到两个不同表的主键。那么,我如何创建一个没有Id的实体类,如何使用EmbeddedId或IdClass来创建映射到相应表的question_response.java实体呢。为什么要这样做?@larsgrefer我需要查询问号_响应表,如果没有一个实体类映射到JPAI中的数据库中的关系,我就无法这样做。我尝试了这种方法,得到的结果是:org.hibernate.AnnotationException:没有为实体问号响应指定标识符不确定下一步要做什么下面是我的QuestionResponse.java。我也相应地更新了Question.java和Response.java,正如您发布的jboss用户指南中提到的//由于comments
Entity Table(name=“Question\u Response”)公共类QuestionResponse中的字符限制,省略了getter和setter{Id ManyToOne private Question;Id ManyToOne private Response Response;
您使用的是哪个版本的hibernate?我使用的是Spring Boot 2.0.1,它自动管理JPA和hibernate版本。当我在WildFly 11上部署war时,它会显示org.hibernate.validator.internal.util.version](后台preinit)HV000001:Hibernate Validator 5.3.6.Final,我想这意味着Hibernate版本5.3.6请用您当前的实体更新您的问题完成。我在实体QuestionResponse中有两个ID。但是,我仍然得到实体QuestionResponse错误指定的No标识符。不确定需要做什么。我遗漏了什么吗?
CREATE TABLE questions( 
    id BIGSERIAL PRIMARY KEY,
    question VARCHAR(255)
);
CREATE TABLE responses( 
    id BIGSERIAL PRIMARY KEY,
    response VARCHAR(255)
);
CREATE TABLE question_response(
    question_id bigint REFERENCES questions ON DELETE CASCADE,
    response_id bigint REFERENCES responses ON DELETE CASCADE,
    PRIMARY KEY ( question_id, response_id)
);
@Entity
@Table(name = "questions")
public class Question{


    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="qid_seq")
    @SequenceGenerator(name = "qid_seq", sequenceName="questions_id_seq")
    @Column(name = "id")
    private Long id;


    @Column(name = "questionText")
    private String questionText;

    @OneToMany(mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<QuestionResponse> responses;

    public Question() {}

    public Question(String questionText) {
        super();
        this.questionText = questionText;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public List<QuestionResponse> getResponses() {
        return responses;
    }
}
@Entity
@Table(name = "question_response")
public class QuestionResponse {

    @Id
    @ManyToOne 
    private Question question;

    @Id
    @ManyToOne 
    private Response response;



    public QuestionResponse() {
        super();
    }

    public QuestionResponse(Question question, Response response) {
        super();
        this.question= question;
        this.response = response;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

}
@Entity
@Table(name = "responses")
public class Response {

    @Id
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="rid_seq")
    @SequenceGenerator(name = "rid_seq", sequenceName="questions_id_seq")
    @Column(name = "id")
    private Long id;

    @Column(name = "responseText")
    private String responseText;

    @OneToMany(mappedBy = "response", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<QuestionResponse> question;

    public Response() {}

    public Response(String responseText) {
        super();
        this.responseText = responseText;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getResponseText() {
        return responseText;
    }

    public void setResponseText(String responseText) {
        this.responseText = responseText;
    }

    public List<QuestionResponse> getQuestion() {
        return question;
    }

}
13:54:49,581 ERROR [org.springframework.boot.SpringApplication] (ServerService Thread Pool -- 86) Application run failed: org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]:
 Invocation of init method failed; nested exception is org.hibernate.AnnotationException:
 No identifier specified for entity: com.poc.questionnarie.QuestionResponse