How to map this OnetoOne in hibernate using JPA
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 -- If I change the reference of Bar in Foo then hibernate doesn't delete the existing Bar but adds the another entry.
- I need to delete existing Bar explicitly before assigning new one for update.
精彩评论