oneToMany双向不工作

时间:2014-06-17 10:16:58

标签: hibernate jpa

我有两个类,并且想要实现双向manyToOne关系。我得到了父班:

@Entity
@Table(name = "SECTIONS")
public class Section_beta implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SECTION_ID")
public Long getId() {
    return id;
}

private List<Question_beta> questions = new LinkedList<>();


@OneToMany(mappedBy = "section", cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @OrderColumn(name = "SECTION_QUESTION_ORDER")
    public List<Question_beta> getQuestions() {
        return questions;
    }

    public void setQuestions(List<Question_beta> questions) {
        this.questions = questions;
    }

儿童班:

@Entity
@Table(name = "QUESTIONS")
public class Question_beta implements Serializable, Cloneable {


@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "QUESTION_ID")
public Long getQuestionID() {
    return questionID;
}
    private Section_beta section;
    @ManyToOne(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name = "SECTION_ID", referencedColumnName = "SECTION_ID")
    public Section_beta getSection() {
        return section;
    }

    public void setSection(Section_beta section) {
        this.section = section;
    }

问题在于hibernate会破坏消息:

Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: Question_beta.SECTION_ID in Section_beta.questions.

我不知道什么是错的,有人吗?

1 个答案:

答案 0 :(得分:0)

试试这个:

@ManyToOne(cascade = {...})
@JoinColumn(name = "SECTION_ID")
public Section_beta getSection() {
    return section;
}

@JoinColumn中的“name”字段必须是Section_beta类中的id字段,而不是表格中的。