Hibernate, manage, and cascade delete

I have a complex graphic with JPA related objects. When I delete the parent element, they are cascaded correctly for children.

Then I process the parent class (so as not to load the one-to-one relationships), and when I delete, I get referential integrity violations. Looking at the hibernation requests for a flash, I see that hibernate is really trying to delete entries in the wrong order, so db complains about referential integrity and an exception is thrown.

Why does this only occur when subjects have tools? Is there a way to change the cascading delete order?

+3
source share
1 answer

I have no answer to your question, but ... why are you messing around with β€œtools” to make your individual association lazy? I checked the following for a one-to-one relationship between a class Fooand its FooDetail:

@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
public FooDetail getFooDetail() {
    return detail;
}

And lazy loading just works . This is the statement executed when the instance is retrieved Foo:

Hibernate: select foo0_.id as id45_0_, foo0_.detail_id as detail3_45_0_, foo0_.shortName as shortName45_0_ from Foo foo0_ where foo0_.id =?

And, later, when calling the getter, it selects FooDetail:

Hibernate: select foodetail0_.id as id46_0_, foodetail0_.fullName as fullName46_0_ from FooDetail foodetail0_ where foodetail0_.id =?

Foo , :

Hibernate: delete from Foo where id=?
Hibernate: delete from FooDetail where id=?

, DDL, Hibernate :

create table Foo (id bigint not null, shortName varchar(255), detail_id bigint, primary key (id))
create table FooDetail (id bigint not null, fullName varchar(255), primary key (id))
alter table Foo add constraint FK212C3F68B31178 foreign key (detail_id) references FooDetail

Hibernate 3.4.0.GA.

: , , "--", - (, , , , ).

+1

Source: https://habr.com/ru/post/1734393/


All Articles