gpt4 book ai didi

java - 由 : org. hibernate.AnnotationException: mappedBy 引用未知目标实体属性引起

转载 作者:IT老高 更新时间:2023-10-28 20:45:15 25 4
gpt4 key购买 nike

我需要建立一对多关系,但出现此错误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;
}
}

和这个 child 类

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

}

最佳答案

您需要将 @OneToMany 注释的 mappedBy 属性设置为 psyQuestions 而不是 questionmappedBy 属性的值是关系另一端的类字段的名称,在本例中 ManyToOne 端的 psyQuestions类 PsyOptions。

public class PsyQuestions {
....
@OneToMany(fetch = FetchType.LAZY,mappedBy="psyQuestions")
private List<PsyOptions> productlist=new ArrayList<PsyOptions>();
....

关于java - 由 : org. hibernate.AnnotationException: mappedBy 引用未知目标实体属性引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com