我收到“无标识符错误”实体类“QuestionResponse”有两个@Id字段。 Question和QuestionResponse之间存在OneToMany关系,Response和QuestionResponse之间存在OnetoMany关系,所有这些类都是基于https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations-many-to-many-bidirectional-with-link-entity设计的
我正在使用Postgres 9.5,Spring Boot 2.0.1并在WildFly 11上部署它
谢谢!
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
答案 0 :(得分:3)
在JPA中,您不能在实体中的多个字段上使用@Id
注释,除非您将其定义为复合主键。因此,您需要将@IdClass
添加到QuestionResponse
实体,以便它包含多个主键字段。
这可能与您的问题无关,但也值得查看此article,其中显示了使用JPA @ManyToMany
注释的最佳方式。