The EntitySpaces Community

Share and learn about the EntitySpaces Architecture.
Welcome to The EntitySpaces Community Sign in | Join | Help
in
Home Forums Photos

Hierarchical Save - Deleting Records

Last post 01-14-2008, 12:29 PM by warrenra8. 2 replies.
Sort Posts: Previous Next
  •  01-11-2008, 6:12 PM 7592

    Hierarchical Save - Deleting Records

    EntitySpaces Trial Version -  not sure how to tell the version, but the GettingStarted PDF is dated Nov 19, 2007

    MyGeneration Version - 1.2.0.7

    Database - Oracle 10g 

     
    I've been evaluating the EntitySpaces trial version.  So far, the experience has been great - I really love how easy this is to get up and running!  I was involved at my company getting a persistence layer working in Java (pre-Hibernate), and I can attest to how much work it is to get right!

    That said, I'm having some difficulty with the hierarchy data model in regards to deleting records. Your online documentation says that the model is smart enough to delete records in the correct order, so as to not raise any constraint violations.  I want to make sure I'm doing this correctly.  I have a table PrimerGroup that is related one-to-many to a table PrimerGroupPair.  It is also related one-to-many to a table AssayPrimerGroup.  To delete a PrimerGroup, I am employing the following code:

     

    Code:
    1    PrimerGroup primerGroup = primerGroupCollection.FindByPrimaryKey(4);
    2 primerGroup.PrimerGroupPairCollectionByPrimerGroupId.MarkAllAsDeleted();
    3 primerGroup.AssayPrimerGroupCollectionByPrimerGroupId.MarkAllAsDeleted();
    4 primerGroup.MarkAsDeleted();
    5 primerGroupCollection.Save();

     When the save happens, I'm running into a child record found constraint violation in my PrimerGroupPair table.  If the Save() call on line 5 is trying to delete the primerGroup record BEFORE all the primerGroupPair records, then I can see where this would happen.  Am I doing something wrong?
  •  01-11-2008, 10:03 PM 7593 in reply to 7592

    Re: Hierarchical Save - Deleting Records

    My first thought was that we had a test for multiple deletes that was passing.

    Code:
    Employee testEmp = new Employee();
    testEmp.LoadByPrimaryKey(empKey);
    testEmp.OrderCollectionByEmployeeID.MarkAllAsDeleted();
    testEmp.CustomerCollectionByStaffAssigned.MarkAllAsDeleted();
    testEmp.MarkAsDeleted();
    testEmp.Save();

    But, comparing to your code, I realized the test was on a single entity, while your code is saving a collection. I added a test more like your code.

    Code:
    empColl = new EmployeeCollection();
    empColl.LoadAll();
    
    Employee testEmp = empColl.FindByPrimaryKey(empKey);
    testEmp.OrderCollectionByEmployeeID.MarkAllAsDeleted();
    testEmp.CustomerCollectionByStaffAssigned.MarkAllAsDeleted();
    testEmp.MarkAsDeleted();
    empColl.Save();

    And it failed with a foreign key violation.

    Code:
    System.Data.SqlClient.SqlException: 
    The DELETE statement conflicted with the 
    REFERENCE constraint "FK_Customer_StaffAssigned". 
    The conflict occurred in database "ForeignKeyTest", 
    table "dbo.Customer", 
    column 'StaffAssigned'.
    The statement has been terminated.

    While still not quite the same, this work-around detaches the entity from the collection, before doing the deletes, and saving the single entity. Hopefully, it will suffice until we can track down the problem and get a fix out.

    Code:
    empColl = new EmployeeCollection();
    empColl.LoadAll();
    
    Employee empToDelete = empColl.FindByPrimaryKey(empKey);
    Employee testEmp = empColl.DetachEntity(empToDelete);
    testEmp.OrderCollectionByEmployeeID.MarkAllAsDeleted();
    testEmp.CustomerCollectionByStaffAssigned.MarkAllAsDeleted();
    testEmp.MarkAsDeleted();
    testEmp.Save();

    David Neal Parsons
    www.entityspaces.net
  •  01-14-2008, 12:29 PM 7618 in reply to 7593

    Re: Hierarchical Save - Deleting Records

    Thank you for looking into this so quickly!  Yes, the workaround will be sufficient - thanks again!!
View as RSS news feed in XML