开发者

How to map this OnetoOne in hibernate using JPA

Merged with Referential integrity with One to One using hibernate.

I have following Entity -

@Entity
public class Foo {
    @Id
    private Long id;

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "foo")
    private Bar bar;

    // getters/setters omitted
}

@Entity
public class Bar{
    @id
    private Long id;

    @OneToOne
    @JoinColumn(name = "foo_id",  nullable = false)
    private Foo foo;

    // getters/setters omitted
}

I kept the relation like this because I want to keep the Id of Foo in Bar table so I can have delete cascade constraints through SQL at DB end

Now this causes another issues -

  1. If I change the reference of Bar in Foo then hibernate doesn't delete the existing Bar but adds the another entry.
  2. I need to delete existing Bar explicitly before assigning new one for update.

What I would like to know is - can I achieve the same DB layout with Foo as a owning side, so I开发者_Go百科f I assign the new Bar I'll just assign it and Hibernate will internally delete the existing not required entry.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜