hibernate中一对多关系映射出错

时间:2014-04-17 10:02:09

标签: java hibernate nhibernate

我有两张表:opinionPollresult

拥有One(opinionPoll) to Many(result) relation

当我尝试使用Alias从其他表中获取记录时,会出现映射错误。 当我删除父子(结果)时,不删除但父删除。我认为我的问题在于映射所以我的表格的下面代码。

对于opinionPoll:

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER)
            @Fetch(value = FetchMode.SUBSELECT)
            @JoinColumn(name = "pid", nullable = false, updatable = true)

            private List<PollResult> pollResults = new ArrayList<PollResult>() ;

结果:

    @ManyToOne(cascade = { CascadeType.ALL })
        @JoinColumn(name = "pid", insertable = false, updatable = false)
        private OpinionPoll opinionPoll;

1 个答案:

答案 0 :(得分:2)

试试这个:

For OpinionPoll:

// mappedBy "opinionPoll" --> the OpinionPoll object in Result class
@OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="opinionPoll")
private List<PollResult> pollResults = new ArrayList<PollResult>() ;


For Result :

@ManyToOne
@JoinColumn(name = "pid")
private OpinionPoll opinionPoll;