0

First of all, sorry for my English.

So, I'm working with MS SQL Server with hibernate and i faced with a problem. I have next mapping of one of the tables in my DB:

    @Entity(name = " ... ")
    public class Entity extends BaseEntity implements Comparable {
        @Id
        @Column(name = "...")
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;

        @Column(name = "parent_entity_id", insertable = false, updatable = false)
        private Integer parentId;

        @ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST})
        @JoinColumn(name = "parent_entity_id")
        private Entity parent;

        @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade = {CascadeType.REMOVE}, orphanRemoval = true)
        private Set<Entity> children;
    //other fields, setters, getters
}

That means, that my Entity objects can have children, which are also an Entity objects.

So, my problems is that I can't correctly delete parent with all his children. When I try to remove parent, i get an SQL error:

The DELETE statement conflicted with the SAME TABLE REFERENCE

So, any ideas, how to solve this problem?

a.kozlenkov
  • 63
  • 1
  • 9
  • Did you check http://stackoverflow.com/questions/16463773/the-delete-statement-conflicted-with-the-same-table-reference-constraint-with-en ? – Abecee Dec 20 '14 at 18:26
  • @Abecee Tnaks for reply. If I understand correctly, this article tells me to "manually" delete all children from the lowest level to upper right up to entity, that I want to delete. I thought about this way, but I'm looking for the way when this will do hibernate or SQL Server:) – a.kozlenkov Dec 22 '14 at 07:07
  • The way I read James' response to above question, it explicitely takes care of all descendants. (Can't test, though, whether he succeeds. - But you seem to have the right environment… ;-) ) – Abecee Dec 22 '14 at 21:11

1 Answers1

0

You have a foreign key defined between parent_entity_id and id. Set it to allow cascading deletes: deleting a parent will delete all it's children, and all their children et cetera.

Be sure you actually want this to happen!

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • I'm sure that i want this to happen, and I'd tried to do this, but SQL Server tells me that i can't, because of Introducing FOREIGN KEY constraint ... on table .... may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. [Here](http://stackoverflow.com/a/852047/3812345) i read, that this situation is MS SQL Server "feature". – a.kozlenkov Dec 22 '14 at 06:55
  • I think you need to tell us what other constraints you have on this set of tables. – simon at rcl Dec 22 '14 at 20:59
  • I have at least another 2 constraints on this table, but i get exception exactly on this foreighn key (and if i not use this contraint, everething works fine), so, do you really want to know more?) – a.kozlenkov Dec 23 '14 at 19:29