Java Spring Boot JPA Hibernate:即使实体类中存在@Id,也没有为实体错误指定标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50401164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot JPA Hibernate: No identifier specified for entity error even when @Id is present in the entity class
提问by Sambhav
I am getting a "No Identifier error" the entity class "QuestionResponse" has an two @Id fields . There is a OneToMany relationship between Question and QuestionResponse and OnetoMany relationship between Response and QuestionResponse and all these classes are designed based on https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations-many-to-many-bidirectional-with-link-entity
我收到“无标识符错误”实体类“QuestionResponse”有两个 @Id 字段。Question 和 QuestionResponse 之间存在一对多关系,Response 和 QuestionResponse 之间存在一对多关系,所有这些类都是基于https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#associations-设计的多对多双向链接实体
I am using Postgres 9.5, Spring Boot 2.0.1 and deploying it on WildFly 11
我正在使用 Postgres 9.5、Spring Boot 2.0.1 并将其部署在 WildFly 11 上
Thanks!
谢谢!
questions.sql
问题.sql
CREATE TABLE questions(
id BIGSERIAL PRIMARY KEY,
question VARCHAR(255)
);
respones.sql
响应文件
CREATE TABLE responses(
id BIGSERIAL PRIMARY KEY,
response VARCHAR(255)
);
question_respone.sql #
question_respone.sql #
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)
);
Question.java
问题.java
@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;
}
}
QuestionResponse.java
问答.java
@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;
}
}
Response.java
响应.java
@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 console
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
回答by Luay Abdulraheem
In JPA, you cannot use the @Id
annotation on more than one field in an entity, unlessyou are defining it as a composite primary key. So you need to add @IdClass
to your QuestionResponse
entity so that it consists multiple primary key fields.
在 JPA 中,您不能@Id
在实体中的多个字段上使用注释,除非您将其定义为复合主键。因此,您需要添加@IdClass
到您的QuestionResponse
实体中,使其包含多个主键字段。
This might not relate to your issue, but it would also be worth having a look at this articlethat shows the best way to use the @ManyToMany
annotation with JPA.