造成者:org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性 [英] Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property

查看:1752
本文介绍了造成者:org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使onetomany关系,但这个错误出现
mappedBy引用一个未知的目标实体属性
这是父母引起:org.hibernate.AnnotationException:mappedBy引用未知目标实体属性

  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)
私人字符串问题;

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

}


解决方案

您需要将 @OneToMany 注释的 mappedBy 属性设置为心理问题而不是问题 mappedBy 属性的值是关系另一端的类字段的名称,在这种情况下 psyQuestions ManyToOne 类PsyOptions一侧。

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


I need to make onetomany relationship but this error appears mappedBy reference an unknown target entity property this is parent 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;
    }
}

and this child class

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

}

解决方案

You need to set the mappedBy attribute of the @OneToMany annotation to psyQuestions instead of question. The value of mappedBy attributes is the name of the class field on the other side of the relationship, in this case psyQuestions of the ManyToOne side of class PsyOptions.

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

这篇关于造成者:org.hibernate.AnnotationException:mappedBy引用一个未知的目标实体属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆