Java 引起:org.hibernate.AnnotationException:mappedBy 引用了一个未知的目标实体属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20454945/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:03:52  来源:igfitidea点击:

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property

javahibernatejpa

提问by Ali Ali Abdel Fatah

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

我需要建立 onetomany 关系,但出现此错误 mappingBy 引用了一个未知的目标实体属性,这是父级 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;
    }

}

采纳答案by melc

You need to set the mappedByattribute of the @OneToManyannotation to psyQuestionsinstead of question. The value of mappedByattributes is the name of the class field on the other side of the relationship, in this case psyQuestionsof the ManyToOneside of class PsyOptions.

您需要mappedBy@OneToMany注释的属性设置为psyQuestions而不是question。的值mappedBy的属性是在关系的另一侧上的类字段的名称,在这种情况下psyQuestions所述的ManyToOne类PsyOptions的一侧。

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

回答by user1048931

I had the same issue because the mappedBy in the source entity was defined to "enrollment" (annotated with @OneToMany) but the corresponding property in the target entity was "bankEnrollment"; this is the property annotated with @ManyToOne.

我遇到了同样的问题,因为源实体中的mappedBy 被定义为“注册”(用@OneToMany 注释)但目标实体中的相应属性是“bankEnrollment”;这是用@ManyToOne 注释的属性。

After updating from enrollment to bankEnrollmentin the source entity, the exception went away (as expected_.

在源实体中从注册更新到 bankEnrollment 后,异常消失了(如预期的_.

Lesson learnt: the mappedBy value (e.g. psyQuestions) should exist as a property name in the target entity.

经验教训:mappedBy 值(例如 psyQuestions)应该作为目标实体中的属性名称存在。

回答by Charith De Silva

Not sure if this is going to help anyone, it was a simple mistake caused this error on my config. Without realising I have had two different packages containing domain class files. Mapped member was in the other package while the application was only scanning single package path.

不确定这是否会帮助任何人,这是一个简单的错误导致我的配置出现此错误。没有意识到我有两个不同的包包含域类文件。映射成员在另一个包中,而应用程序仅扫描单个包路径。

It was hard to figure out as they were different only by a single character e.g. org.abc.core.domains and org.abcs.core.domains.

很难弄清楚,因为它们只有一个字符不同,例如 org.abc.core.domains 和 org.abcs.core.domains。

Added other package to database @configuration scanners resolved the issue for me.

将其他包添加到数据库 @configuration 扫描仪为我解决了这个问题。

回答by menoktaokan

I just correct my mapping and it solved the problem.

我只是更正了我的映射,它解决了问题。

...
     @OneToMany(fetch = FetchType.LAZY,mappedBy="question")
...