Print Page | Close Window

Persistence of entities

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=482
Printed Date: 11-Jun-2026 at 6:22am


Topic: Persistence of entities
Posted By: Yaron
Subject: Persistence of entities
Date Posted: 09-Oct-2007 at 9:56am

I observed what appears strange behaviour.

I have a simple invoice & stock inventory system, Generate invoice and when saving invoice details - adjust the Qty In stock for the store,
what I noticed is that the Invoice Data (rowstate = 'Added') saves fine,but the ProductStore entity (where I store the qty is stock) which has RowState = 'Modified' looses the changes before saving.
I used the PM.SaveChanges method.
 
however is I 'split' the save operation i.e. save the StockLevels (modified) rows first, and on a second save operation save the Invoice Details. all works well, here is what I mean
  
Scenario 1 : SaveData(AllChanges) don't work
Scenario 2 : SaveData(InvoiceData)
                    SaveData(StockLevels) don't work
Scenario 3:  SaveData(StockLevels)
                    SaveData(InvoiceData) works well
 
Can anybody help?



Replies:
Posted By: davidklitzke
Date Posted: 10-Oct-2007 at 11:30am

The most likely cause is that the Persistence Order is incorrect.  The Persistence Order is by default set automatically by the Persistence Manager through the use of a "reverse topological sort".

 

If the Persistence Order is incorrect, you will need to override it with your own order using the SaveOptions of SaveChanges.

 
You might also try using the PersistenceManager.ForceIdFixUp() to resolve both parent and child tables’ IDs/keys first in an atemmpt to improve the behavior of the reverse topological sort.
 
There are some rare cases where you cannot solve the problem through Persistence Order.  In these cases, you will have to solve the problem by performing a 2-step save.


Posted By: Yaron
Date Posted: 10-Oct-2007 at 9:21pm

Thanks, regaring those rare cases, Has anybody been able to Isolate those scenarios, it will help alot if I can know them before building my Save methods

 
 


Posted By: Hawkaye1
Date Posted: 05-Mar-2008 at 8:37am
Do you have an example of how to modify the persistence order, any attempt I have made says the colloection is readonly.


Posted By: davidklitzke
Date Posted: 05-Mar-2008 at 9:25am
Persistemce Order is a property of the Save operation and is not a property of the Business Objects themselves.  We used to include a tutorial on Persistence Order, but after we implemented automatic calculaion of a default Persistence Order a few years ago, we dropped the tutorial since we couldn't find any real life examples of situations where we would want to modify the Persistence Order.
 
Why are you trying to modify thE Default Persistence Order?  BTW, Your error message about modifying a readonly entity has nothing to do with Persistence Order,
 
I can send you an example of modifying the Persistence Order if you wish.


Posted By: Hawkaye1
Date Posted: 07-Mar-2008 at 6:45am
When I save changes, I get the error:
 
A first chance exception of type 'IdeaBlade.Persistence.PersistenceManagerSaveException' occurred in IdeaBlade.Persistence.dll
Additional information: Save failed - Collection was modified; enumeration operation may not execute.
 
All I have to do to get the error (Example: Create a new Order, Create a new OrderDetail, Then add the Order to the CustomerOrders collection.)
 
Order.Create(persMgr)
OrderDetail.Create(persMgr, mOrder)
mOrderBS.MoveLast()
currentOrder = CType(OrderBS.Current, Order)
mCustomerOrder = CType(CustomerOrderBS.Current, CustomerOrder)
mCustomerOrder.Order = currentOrder
 
Dim pMgrSave As SaveHelper = New SaveHelper(pm)
Try
If pMgrSave.SaveAll Then
DevExpress.XtraEditors.XtraMessageBox.Show("Information Saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
DevExpress.XtraEditors.XtraMessageBox.Show("Nothing to Save", "No changes", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show("An exception of type " & ex.GetType().ToString() & _
" was encountered. ")
End Try
This is my save function:
 
Public Class SaveHelper
Private pMgr As PersistenceManager
 
Public Sub New(ByVal persMgr As PersistenceManager)
pMgr = persMgr
End Sub
 
Public Function SaveAll() As Boolean
Dim result As SaveResult
 
If pMgr.HasChanges Then
Try
result = pMgr.SaveChanges()
If result.Ok Then
Return True
End If
Catch ex As Exception
DevExpress.XtraEditors.XtraMessageBox.Show("An exception of type " & ex.GetType().ToString() & _
" was encountered. ")
End Try
End If
Return False
End Function
 
If I retry the save, it will save the tempId to the record and save it to the database. This only happens if I create a new Order
Yes, I would like an example to modify the persistence order.


Posted By: Hawkaye1
Date Posted: 07-Mar-2008 at 9:29am

After further investigation, I found that the problem is tied to the IdGenerator. I use the IdGenerator to generate the next order number as well as the Unique Id. Everything works fine until I add the Order to the CustomerOrder collection. I show the Unique Id to be fine, it is the OrderNo (-1) that get saved to the database.

 
    Public Shared Function Create(ByVal pManager As PersistenceManager, ByVal pCustomer As Customer) As Order
        Dim aOrder As Order
        aOrder = pManager.CreateEntity(Of Order)()
        ' if this object type requires a unique id and you have implemented
        ' the IIdGenerator interface implement the following line
        pManager.GenerateId(aOrder, Invoice.IdEntityColumn)
        pManager.GenerateId(aOrder, Order.OrderNoEntityColumn)
        ' Add custom code here
        aOrder.AddToManager()
        aOrder.RowVersion = 0
        aOrder.Customer = pCustomer
        aOrder.TermId = 1
        aOrder.OrderTypeId = 1
        Return aOrder   
   End Function
Database Row: (OrderNo in Red)
      
28364 -1 29608 4123 1 1 1045.0000 NULL NULL False NULL False False NULL NULL 0
 
NextId Table
 
Name             NextId     DefaultGroupSize
Trip 15001 1
Order 42869 1
Pay 1047 1
Customer 6599 1
Driver 5251 1
NULL NULL NULL


Posted By: davidklitzke
Date Posted: 07-Mar-2008 at 10:23am
Before the Business Objects are saved to the database, all of the temporary IDs are converted to permanent IDs.  This means not only fixing up the IDs of the newly created objects, but all references to these objects.  However, DevForce can only do this if it knows about the relationship between the two Business Objects.  I think that what is happening is that you have not declared this relationship.
 
To prove that DEvForce does do this correctly,  take the IdeaBlade Tutorial Database, and write a little application that creates an order and an order detail that belongs to the  order.  Save the two new business objects.  You will find that you do not have a problem.
 
 


Posted By: davidklitzke
Date Posted: 07-Mar-2008 at 10:28am
Here is the tutorial on Persistence Order:
 
http://www.ideablade.com/forum/uploads/20080307_132827_Persistence_Ord.zip - uploads/20080307_132827_Persistence_Ord.zip



Print Page | Close Window