我需要建立关系,但会出现此错误
mappedBy引用未知的目标实体属性
这是父Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property
package com.dating.model;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="question")
public class PsyQuestions {
@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="questionid")
private long psyQuestionId;
@Column(name="questiontext")
private String question;
@OneToMany(fetch = FetchType.LAZY,mappedBy="question")
private List<PsyOptions> productlist=new ArrayList<PsyOptions>();
public PsyQuestions() {
super();
}
public List<PsyOptions> getProductlist() {
return productlist;
}
public void setProductlist(List<PsyOptions> productlist) {
this.productlist = productlist;
}
public long getPsyQuestionId() {
return psyQuestionId;
}
public void setPsyQuestionId(long psyQuestionId) {
this.psyQuestionId = psyQuestionId;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
而这个孩子班
package com.dating.model;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="option")
public class PsyOptions {
@Id
@GenericGenerator(name="autoGen" ,strategy="increment")
@GeneratedValue(generator="autoGen")
@Column(name="optionid")
private long psyOptionId;
@Column(name="optiontext")
private String optionText;
@JoinColumn(name = "questionid")
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
PsyQuestions psyQuestions;
public PsyOptions() {
super();
}
public PsyQuestions getPsyQuestions() {
return psyQuestions;
}
public void setPsyQuestions(PsyQuestions psyQuestions) {
this.psyQuestions = psyQuestions;
}
public long getPsyOptionId() {
return psyOptionId;
}
public void setPsyOptionId(long psyOptionId) {
this.psyOptionId = psyOptionId;
}
public String getOptionText() {
return optionText;
}
public void setOptionText(String optionText) {
this.optionText = optionText;
}
}
答案 0 :(得分:34)
您需要将mappedBy
注释的@OneToMany
属性设置为 psyQuestions ,而不是问题。 mappedBy
属性的值是关系另一侧的类字段的名称,在本例中为PsyOptions类的ManyToOne
侧的 psyQuestions 。
public class PsyQuestions {
....
@OneToMany(fetch = FetchType.LAZY,mappedBy="psyQuestions")
private List<PsyOptions> productlist=new ArrayList<PsyOptions>();
....
答案 1 :(得分:3)
我有同样的问题,因为源实体中的mappedBy被定义为“注册”(用@OneToMany注释),但目标实体中的相应属性是“bankEnrollment”;这是用@ManyToOne注释的属性。
在源实体中从注册更新到bankEnrollment之后,异常消失了(正如预期的那样。
获得的经验:mappedBy值(例如psyQuestions)应作为目标实体中的属性名存在。
答案 2 :(得分:0)
不确定这是否会对任何人有所帮助,这是一个简单的错误导致我的配置出现此错误。没有意识到我有两个包含域类文件的不同包。映射成员位于另一个包中,而应用程序仅扫描单个包路径。
很难弄明白,因为它们只有一个字符不同,例如org.abc.core.domains和org.abcs.core.domains。
向数据库添加了其他软件包@configuration扫描程序为我解决了这个问题。
答案 3 :(得分:0)
我只是更正了映射,它解决了问题。
...
@OneToMany(fetch = FetchType.LAZY,mappedBy="question")
...