TargetPerEntity Delete

Posts   
 
    
nottinbe
User
Posts: 46
Joined: 15-Mar-2007
# Posted on: 06-Dec-2007 22:58:18   

Using adapter, 2.5.7.1119

I have an Order table, and several derived tables such as OrderTypeA, OrderTypeB, etc.
They are setup with a 1:1 relationship between OrderID, which is defined of course on Order.

I have an OrderID that I want to delete. I expected to be able to do: DeleteEntitiesDirectly(typeof(OrderEntity), new RelationPredicateBucket(OrderFields.OrderID == orderID))

It doesn't work though. It only issues the DELETE against the Order table. There are so many other parts of LLBLGen that detects the relationship and does the appropriate joins. I really expected this to work that way as well.

I understand you can't make deletes on hierarchies work generically if your deleting using some type of filter. But in this case, with the PK, shouldn't you just be able to issue a DELETE on all derived tables on that PK value?

I also tried: adapter.Delete(new OrderEntity(id)), but that had the same problem as DeleteEntitiesDirectly.

Thanks, Brian

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Dec-2007 10:24:56   

DeleteEntitiesDirectly is limited to the table it's used on, and it shouldn't be used in a TargetPerEntity hierarchy. Explanation is found here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=4589

I also tried: adapter.Delete(new OrderEntity(id)), but that had the same problem as DeleteEntitiesDirectly.

You should delete the Child Entity, this will also delete it's parent Entity.

nottinbe
User
Posts: 46
Joined: 15-Mar-2007
# Posted on: 07-Dec-2007 14:56:25   

But what if all I know is the key value, and the fact that its in some hierarchy with Order as the root. I don't know what specific leaf in the hiearchy the ID represents. So, just like with polymorphic fetches where LLBLGen tries to pull from all leaf tables, I thought there would be a way to have it delete and try the delete on all leaf tables.

Thanks, Brian

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39866
Joined: 17-Aug-2003
# Posted on: 07-Dec-2007 16:41:00   

Delete entities directly of inheritance entities using TargetPerEntity isn't possible in a lot of cases, for the following reason: when you delete a subtype directly based on a filter on that subtype's fields, you can't delete the SUPERtype's row anymore, because you need a FILTER on the SUBtype, however for FK constraints, the SUBtype has to be deleted first.

To only 100% working solution is to store the IDs of the deleted subtype entities in a temptable, and delete the supertype entities based on a filter on that temptable. We didn't add this as not all databases support temptables.

The only situation where it could go right is with a PK single entity delete. For that situation, use the workaround to load the entity in memory and delete it then using the normal ways. You can fetch the entity polymorphically using the Order factory and FetchNewEntity where you specify a filter on the ID. THen pass the fetched entity to DeleteEntity and you're done.

Frans Bouma | Lead developer LLBLGen Pro
nottinbe
User
Posts: 46
Joined: 15-Mar-2007
# Posted on: 07-Dec-2007 16:47:57   

Yeah, I figured that would be the solution. But I really wanted to avoid loading the entity, since that was the point of DeleteEntitiesDirectly! I may have a lot of PK's to delete, and it would be quite wasteful to have to load each one just to delete it.

So, instead I'm using a combination of IInheritanceInfo.EntityNamesOfPathsToLeafs and IRelationFactory.GetSubTypeRelation to dynamically determine the relations, given a root entity, that I need to try to delete first.

Thanks, Brian

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 07-Dec-2007 16:57:25   

Also you may use database Cascade Deletes, so when you delete the root entity all its children are deleted accordingly.