1

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.
Premraj
  • 7,802
  • 8
  • 45
  • 66

0 Answers0