Hibernate one to many association deletion - oracle

In a one to many bidirectional association with cascade enabled as all-delete-orphan in hibernate, is there any possibility ever hibernate try to delete child entity using foreign key column?
Its firing one extra query delete from child where foreign_key_col = parent_primary_key.

For bidirectional relation,if you delete casacade in the parent entity.
You can remove the child from parent directly.
//set parent as null
child.setParent(null)
//parent.children.iterater and remove it from the iterator.
//then save the parent.
save(parent)

Related

Is it allowed to change entities' relationship state in Hibernate Interceptor?

I have an entity that has a Set of child entities. Can I add/remove some of the children in the collection in Hibernate's org.hibernate.Interceptor.onFlushDirty()?
The documentation (javadoc, JBoss userguide) doesn't explicitly mention changing relationship state, so I just want to make sure it's legitimate.
Update
Decided to describe by problem.
There's a Parent entity that has a Set of Child entities. The Child entities are created based purely on fields of the Parent entity, no additional information needed. The children need to be updated each time before persisting the Parent.
I wanted to do it in Interceptor.onFlushDirty() by clearing existing children and adding new reclaculated (i.e. Transient) children, but I'm getting the TransientObjectException. As I understand, it's because I add transient Child entities during flush, but I'm not sure.

spring-data-jpa Avoid to delete Parent entity if there are child entities

I have a relationship Invoice - InvoiceLine, and I want to avoid to delete an Invoice is there are InvoiceLines...
I would like to know which relationship is the best:
ALL ,
DETACH,
MERGE,
PERSIST,
REFRESH,
REMOVE
It almost doesn't matter.
As long as you don't use REMOVE and have an actual foreign key relationship between Invoice and InvoiceLine.
Without the REMOVE cascade deleting an Invoice won't touch the InvoiceLines. Which in turn will trigger the foreign key to prevent the operation.

JPA: Remove Child Entities

I have an entity(ex: Document) that is used as child in 4 other entities(using #OneToMany with #JoinTable in parents). I am not using Bidirectional Mapping. My requirement is to remove the Child(i.e Document), and I have two ways to do that, one way is, get the 4 parents, remove child from them and update them. Second, using native query(using jdbcTemplate) to remove entry from 4 join tables and remove the child.
Is there any other way it can be done in much simpler manner?
Create an abstract base class containing the Document as member and user JPA inhertiance --> http://en.wikibooks.org/wiki/Java_Persistence/Inheritance
Than it should be possible to get all users of a document with just one query.
Than it should be relatively easy to remove all references.
Don't do magic behind automatic deletion stuff. Thats for the cost of documentation.
Add orphan deletion (ie. delete child object when it's removed from collection in the parent). To enable it, you need to add
#OneToMany(orphanRemoval=true)
in owning entity.

JPA unidirectional #OneToMany performance

I have two entities with unidirectional #OneToMany Lazy relationship. When I try to add a child, it seems like Hibernate 4 (my JPA provider) actually performs
Select query
Delete all children with that parent id on join table
Reinsert back all and the new child on join table
How to make Hibernate to just insert the child I wish, without changing my relationship?
By default, a unidirectional #OneToMany relationship will use a join table, it will perform operation as my question. If you are using JPA2 and do not use polymorphic on parent, you may add #JoinColumn, which will create foreign key on children table instead of another join table. JPA provider then will not perform delete and reinsert again.
making relationship bidirectional will solve your problem. you can read some information here

Magento: How to delete parent object when child reference changes?

Given:
Two custom classes in Magento with a Many-to-One relationship between them.
The child holds a foreign key to the parent.
The database is set to cascade deletes.
There are cases when a child's reference changes to a different parent. In some of those cases, I want to delete the parent in the afterSave method of the child. When I do this, the child itself disappears, since the change of FK to the new parent hasn't been written to the database yet, and the database level cascade kicks in.
How can I arrange for the deletion of the parent object after the write of the new foreign key in the child object?
afterSave triggers before the query has been written to DB, as you've noticed yourself. You need to use *_save_commit_after event. Where asterisk is your Models event_prefix. Create an Observer and listen for this event, that way you can be sure that info in DB has been already updated, and you won't suffer the foreign key effect.

Resources