How to map this OnetoOne in hibernate using JPA

∥☆過路亽.° 提交于 2020-01-16 01:47:26

问题


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 If I assign the new Bar I'll just assign it and Hibernate will internally delete the existing not required entry.

来源:https://stackoverflow.com/questions/5356313/how-to-map-this-onetoone-in-hibernate-using-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!