Ali*_*tah 24 java hibernate jpa
我需要进行onetomany关系,但是这个错误出现了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;
}
}
Run Code Online (Sandbox Code Playgroud)
而这个孩子班
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;
}
}
Run Code Online (Sandbox Code Playgroud)
mel*_*elc 40
您需要mappedBy
将@OneToMany
注释的属性设置为psyQuestions而不是问题.的值mappedBy
的属性是在关系的另一侧上的类字段的名称,在这种情况下psyQuestions所述的ManyToOne
类PsyOptions的一侧.
public class PsyQuestions {
....
@OneToMany(fetch = FetchType.LAZY,mappedBy="psyQuestions")
private List<PsyOptions> productlist=new ArrayList<PsyOptions>();
....
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,因为源实体中的mappedBy被定义为“注册”(用@OneToMany注释),但是目标实体中的对应属性是“ bankEnrollment”;这是@ManyToOne注释的属性。
从源实体中的注册更新为bankEnrollment后,异常消失(按预期_。
经验教训:mappedBy值(例如psyQuestions)应作为属性名称存在于目标实体中。
归档时间: |
|
查看次数: |
45634 次 |
最近记录: |