Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

}
like image 496
Ali Ali Abdel Fatah Avatar asked Dec 08 '13 15:12

Ali Ali Abdel Fatah


2 Answers

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>();
....
like image 167
melc Avatar answered Oct 17 '22 18:10

melc


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.

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

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

like image 45
user1048931 Avatar answered Oct 17 '22 19:10

user1048931