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.
Related
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.
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)
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.
So im using EFBulkInsert https://efbulkinsert.codeplex.com/
The problem is I have child objects I need to set the ID of the inserted parent objects.
Previously after inserting the parents objects I've tried to rely on the context to return the id's and set them on the child objects - then use EFBulkInsert to insert the child objects - every now and then the context gets confused even after recreating the context and I get the wrong id on the child objects.
Does anyone have a good pattern / strategy for setting the parent id on the child object I should mention i'm doing this for a batch of 1000 parent objects. So I don't particularly want to get the id's from the database after SaveChanges for the parent object unless it's performant.
The best idea I have is to add two temporary columns, firtst to hold an original Id, and second to hold parentId. After bulk insert update proper columns. This method requires privilages to modify the table.
I had a similar problem when I couldn't modify database, so I set AutoDetectChangesEnabled and ValidateOnSaveEnabled to false, but results were not very satisfied.
I am somewhat new to LINQ and have a quick question regarding deleting.
Say, for example I have 2 tables, Orders and OrderItems. Using LINQ, I can easily create a new child record by using
order.Items.Add(new OrderItem());
and this will create the child record in the database and update its foreign key to the orderId. This is great, I like it! However when I want to remove a child record
order.Items.Remove(orderItem);
I get an error when I sumbit the changes (because its not actually deleting the child row (order item), just removing the foreign keyId). Is it possible to do this the way I would like to? I don't want to have to create a whole bunch of repositories and if ladders to delete all child rows for a large database.
Thanks in advance.
E
You can achieve that in the DB itself by configuring the Foreign key relationship to delete child records on deletion of the parent's key.
Note that this is transparent to Linq2SQL and it will not be aware of it, so it's best to make sure you do not keep the datacontexts around after that, since the OrderItem objects will still be present.
Set ON DELETE CASCADE for the table in question which will let the SQL Server handle this for you.