当前位置: 首页 > 知识库问答 >
问题:

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

卜弘文
2023-03-14

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

我已经阅读了有关使用IdClass或EmbeddedId的内容,但是,我不确定如何使用这些注释映射两个不同类中的主键。

在实施评论中提到的更改后更新实体

谢谢

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;
    }

}

#WildFly控制台#

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

共有1个答案

季凡
2023-03-14

您可以将多对多关系分解为一对多对一构造,如下所述:

https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations-多对多双向链接实体

 类似资料:
  • Hi编写Spring应用程序,使用Spring Security。这是我的用户和帐户角色数据库: 我的实体类: 当我尝试登录我的系统我有错误: Hibernate:选择userrolese0_. username作为username3_1_0_,userrolese0_. id作为id1_0_0_,userrolese0_. id作为id1_0_1_,userrolese0_. name作为nam

  • b)对Employee类中的ReferencedColumnName='department id'使用@ManyToOne和@JoinColumn。 建议采用哪种方法?还是这两种方法用于完全不同的问题?

  • 使用Java8 lambdas,在给定可能键的和的情况下,有效创建新的的“最佳”方法是什么?在这种情况下,您将得到一个包含可能的键的并希望生成一个其中是基于的某个方面构造的某种类型,即映射值类型。 我已经研究了几种方法,但认为一种方法比另一种方法更好(可能有一个例外--参见代码)。我将把“最佳”解释为代码清晰度和运行时效率的结合。这些是我想出来的。我肯定有人能做得更好,这是这个问题的一个方面。我不

  • 假设我有一组字符串和一个散列函数(或任何单边函数)和一个测试函数。我想用Java8流创建一个从输入字符串到通过测试函数的哈希值的映射。我的问题是如何在中编写? 看来老的for循环是最简洁的解决方案。

  • 但是当我运行应用程序时,我会得到以下异常:

  • null 帮我处理一下这种情况吧。谢谢你。