Entity Re-Initialization

Posts   
 
    
DavidGriggs avatar
Posts: 6
Joined: 24-Oct-2008
# Posted on: 04-Nov-2008 02:11:05   

Hello, I am having a problem with entity classes I need your help with. Version 2.6 Final.

I am using the following class definition:

    
    public partial class frmCampus : Form
    {
        public frmCampus(CampusEntity campus)
        {
            InitializeComponent();
            m_campus = campus;
        
        }
        // Data
        private CampusEntity m_campus;
    }

Now this form allows the user to edit the campus entity being passed in (using data binding). However when the user tabs out of the name field I do some duplicate record checks and show some possible matches. If the user selects one of the possible matches I need to re-init the m_campus member with the selected row in the database.

If I do:

m_campus = new CampusEntity(newPK);

Sure enough m_campus now represents the new entity and all the binding on the form is correct.

The problem is however that m_campus now points at a new instance of CampusEntity and the original passed in CampusEntity (in the constructor of the form) is still pointing at the original instance (created outside of the form).

So outside of the form we don't have a reference to the newly selected record.

So, simple I though. Just use the FetchUsingPK record on the existing entity? No, this does not appear to work. I get some of the values back, but all of the relations are left in a strange state. The FK ID's are correct, but the members that point to the relating entities themselves are invalid.

Is there a way to completely re-init an existing entity instance, using the PK, that will also re-init any FK members?

Sorry for the long winded message. Thanks in advance for your help.

David

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 05-Nov-2008 11:44:20   

Is there a way to completely re-init an existing entity instance, using the PK, that will also re-init any FK members?

How did you fetch the entire graph in the first place? You should refetch the graph the same way you did in the original initialization. I suppose you have used prefetchPaths to fetch the related entities, and so you have to do it again.

DavidGriggs avatar
Posts: 6
Joined: 24-Oct-2008
# Posted on: 05-Nov-2008 23:38:20   

I used LazyLoading to access the FK Members. When I try to do that again after using FetchUsingPK.. I do not get the correct members.

So, all I really do is call FetchUsingPK on an entity instance that is not empty and then I am unable to access for example an 'Address' sub-entity (FK relationship) from the newly fetched 'Customer' entity.

If I create a new 'Customer' entity and call FetchUsingPK, everything works well, and I am able to access the 'Address' entity using the lazy loading access.

Does that make sense?

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 06-Nov-2008 12:14:05   

Maybe the following part from the manual can help you out:

Load on demand/Lazy loading

Once loaded, the entity is not loaded again, if you access the property again. This is called load on demand or lazy loading: the load action of the related entity (in our example 'customer') is done when you ask for it, not when the referencing entity (in our example 'order') is loaded. You can set a flag which makes the code load the related entity each time you access the property: AlwaysFetchFieldMappedOnRelation. In our example of Order and Customer, OrderEntity has a property called AlwaysFetchCustomer and CustomerEntity has a property called AlwaysFetchOrders. Default for these properties is 'false'. Setting these properties to true, will assure that the related entity is reloaded from the database each time you access the property. This can be handy if you want to stay up to date with the related entity state in the database. It can degrade performance, so use the property with care.

Another way to force loading of a related entity or collection is by specifying true for the forceFetch parameter in the GetSingleFieldMappedOnRelation call, or when the property contains a collection, GetMultiFieldMappedOnRelation call. Forcing a fetch has a difference with AlwaysFetchFieldMappedOnRelation in that a forced fetch will clear the collection first, while AlwaysFetchFieldMappedOnRelation does not. A forced fetch will thus remove new entities added to the collection from that collection as these are not yet stored in the database.