OneToMany,ManyToOne为什么孩子的父级值为null? [英] OneToMany, ManyToOne why value of parent in children is null?

查看:93
本文介绍了OneToMany,ManyToOne为什么孩子的父级值为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过类似的问题,但仍然有问题.

I read similar questions, but still have problem.

配置信息: 休眠3.5.1

Configuration info: Hibernate 3.5.1

父类问题:

@Entity
public class Question implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="questionId")
    private long id;
    private String title;
    private String description;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="question")
    private Set<Vote> votes;

    public void addVote(Vote vote){
        if(votes==null)
            votes = new HashSet<Vote>();
        getVotes().add(vote);
    }

}

儿童班投票:

@Entity
public class Vote implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="voteId")
    private long id;

    private transient InetAddress address;

    @ManyToOne
    @JoinColumn(name="questionId")
    private Question question;

    @Enumerated(EnumType.STRING)
    private Mode mode;

// ...
}

最后是非常简单的测试:

And finally very simple test:

@Test
public void testSaveOrUpdate() {
Vote vote1 = new Vote();
    vote1.setAddress(InetAddress.getLocalHost());
    vote1.setMode(Mode.HIM);
    question = new Question();
    question.setTitle("test?");
    question.setDescription("test");
    question.addVote(vote1);

    question2 = questionDao.saveOrUpdate(question);
    assertNotNull(question2);
    Set<Vote> votes = question.getVotes();
    assertEquals(votes.size(), 1);
    for(Vote vote:votes)
        assertNotNull(vote.getQuestion());
}

测试失败,因为 vote.getQuestion()返回null.当我在数据库中签入时, questionId 列中为空.我的问题是我应该怎么做才能从 votes 中引用 vote 中的问题?映射有问题,但我不知道该怎么办.

test fail because vote.getQuestion() return null. When I check in DB there are null in questionId column. My question is what I should do to have reference to question in vote from votes? there is something wrong with mapping but i have no idea what.

推荐答案

在所有关系中,都有一个拥有的一面(在ORM中至少是一面).在多对一"案例中,您已将投票"作为拥有方.这意味着,将投票权与问题联系起来是依赖的.因此,question.addVote(vote1)将不起作用,但是vote.setQuestion(question)将使一切正常.投票是拥有者,因为您已在这边声明了JoinColumn并在Question边声明了mappingBy字段.

In all relationships there is an owning side (atleast in ORM). In your Many-to-One case you have made the Vote as the owning side. That means, it is the dependency of a vote to associate itself with the question. Hence question.addVote(vote1) will not work, but vote.setQuestion(question) will make everything work. The Vote is the owning side, as you have declared the JoinColumn on this side and the mappedBy field on the Question side.

您现在应该保留投票对象而不是问题对象才能生效.

And you should now persist the vote object and not the question object to have any effect.

这篇关于OneToMany,ManyToOne为什么孩子的父级值为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆