views:

293

answers:

1

I have a complex object graph with JPA connected entities. When I delete the parent the deletions cascade correctly to the children.

Then I instrument the parent class (as to not load eagerly one-to-one relationships) and upon deletion I get referential integrity violation exceptions. Looking at the queries hibernate issues upon flush, I can see that hibernate indeed tries to delete records in a wrong order, thus the db complains for referential integrity and an exception is thrown.

Why this only manifests when the entities are instrumented? Is there a way to alter the delete cascade order?

+1  A: 

I don't have an answer to your question but... why are you messing with "instrumentation" to make your one-to-one association lazy-loaded? I have tested the following for a one-to-one association between a class Foo and its FooDetail :

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

And lazy-loading just works. This is the statement performed when retrieving a Foo instance:

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, the FooDetail is fetched:

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

And deleting a given Foo instance also works fine, the statements are executed in the right order:

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

Below, the DDL generated by Hibernate for the corresponding tables for reference:

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

Tested with Hibernate Annotations 3.4.0.GA.

Update: If this is not what you're looking for, then use a one-to-many association, there are limitations with a one-to-one association (and please clarify your question, readers can't guess what you don't write).

Pascal Thivent
Just try it the other way. First load a FooDetail, and see if Foo is fetched lazily. OneToOne relationships are never genuine lazy relationships no matter what. Look here: https://www.hibernate.org/162.htmlIt does not use annotations but you will get the point.
yannisf
@yannisf First, you mentioned explicitly parent to child cascading, not child to parent (which doesn't make sense IMO). Second, you didn't mention anything about the association being bidirectional. Are the readers supposed to guess that?
Pascal Thivent
Ok, to clear the things out:1. The cascading IS from parent to child2. The relationships are bidirectional and the owner of the relationship is the child.3. Study the link in comment 1.Does this help?
yannisf
@yannisf I'm not going to "study" anything because 1. I don't need to (I'm perfectly aware of the problems with one-to-one associations and lazy loading) 2. I don't like your tone 3. I spent enough time on your problem without having all the details. 4. I gave you a way to solve this (use a fake one-to-many assocation).
Pascal Thivent
@Pascal Thivent my question explicitly asked about the hibernate behaviour in conjuction with instrumention. You provided a solution that I have already thought of, but it is not the answer to my question. For example, an acceptable answer could be: "I noticed the same problem, maybe we should open a jira issue on hibernate as this seems to be a bug".
yannisf
@yannisf If you think you provided all the details, everything required to get help, fine, but I don't. Too often, people ask about a specific solution without explaining what problem they are they are trying to solve, which is exactly your case (maybe study http://meta.stackoverflow.com/questions/21366/asking-questions-clearly). That's why *I* am not going to help more. Now, if you think this is a bug, open an issue (and please provide the link so that I can see how they will treat it if you formulate you problem like here, without any test case), you don't need any authorization for that.
Pascal Thivent
@yannisf And BTW, reread the first sentence of my answer.
Pascal Thivent
@Pascal Thivent Do you have any experience with hibernate instrumentation? If I provided a test case that this manifests will it interest you?
yannisf
@yannisf Yes I do but I'm not interested. Maybe I'll look at the Jira issue though.
Pascal Thivent