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?