简体   繁体   English

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

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

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我需要建立一个单一的关系,但这个错误出现了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;
    }
}

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 .您需要设定mappedBy的属性@OneToMany注释psyQuestions,而不是问题 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.的值mappedBy属性是在关系的另一侧上的类字段的名称,在的这种情况下psyQuestions ManyToOne类PsyOptions的一侧。

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

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";我遇到了同样的问题,因为源实体中的mappedBy 被定义为“注册”(用@OneToMany 注释),但目标实体中的相应属性是“bankEnrollment”; this is the property annotated with @ManyToOne.这是用@ManyToOne 注释的属性。

After updating from enrollment to bankEnrollmentin the source entity, the exception went away (as expected_.在源实体中从注册更新到 bankEnrollment 后,异常消失了(如预期的_.

Lesson learnt: the mappedBy value (eg psyQuestions) should exist as a property name in the target entity.经验教训:mappedBy 值(例如 psyQuestions)应该作为目标实体中的属性名称存在。

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 eg 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 扫描仪为我解决了这个问题。

I just correct my mapping and it solved the problem.我只是更正了我的映射,它解决了问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.hibernate.AnnotationException:mappedBy通过引用未知目标实体属性 - org.hibernate.AnnotationException: mappedBy reference an unknown target entity property java.lang.ExceptionInInitializerError和org.hibernate.AnnotationException:MapdBy引用了未知的目标实体属性: - java.lang.ExceptionInInitializerError and org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: 调用init方法失败; 嵌套的异常是org.hibernate.AnnotationException:mappedBy引用了未知的目标实体属性: - Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性。 发生错误:java.lang.NullPointerException - org.hibernate.AnnotationException: mappedBy reference an unknown target entity property. There is an error: java.lang.NullPointerException OneToMany - org.hibernate.AnnotationException:mappedBy引用未知的目标实体属性 - OneToMany - org.hibernate.AnnotationException: mappedBy reference an unknown target entity property org.hibernate.AnnotationException:未知,由...引用的属性未知 - org.hibernate.AnnotationException: Unknown mappedBy in … referenced property unknown AnnotationException:mapped通过引用未知目标实体属性 - AnnotationException:mappedBy reference an unknown target entity property 通过(一对一)暂停休眠:org.hibernate.AnnotationException:未知mappingBy在:引用的属性未知: - Casued Hibernate by (One-toOne) : org.hibernate.AnnotationException: Unknown mappedBy in: referenced property unknown: 双向休眠-org.hibernate.AnnotationException:未知的mappingBy in:错误 - bidirectional hibernate - org.hibernate.AnnotationException: Unknown mappedBy in: error org.hibernate.AnnotationException:未知的由普通类映射 - org.hibernate.AnnotationException: Unknown mappedBy with common class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM