-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Refresh lookups after save/update
PostPosted: Thu Feb 15, 2007 7:42 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 11:26 am
Posts: 33
Imagine the following simple classes:

Code:
[Serializable]
public sealed class Address
{
    private Int64 id;
    private AddressType addressType;
    // Other attributes removed for clarity

    public Int64 Id
    {
        get { return id; }
        set { id = value; }
    }

    public AddressType AddressType
    {
        get { return addressType; }
        set { addressType = value; }
    }
}

public sealed class AddressType
{
    private Int64 id;
    private IList addressList;
    private striung descr;

    public Int64 Id
    {
        get { return id; }
        set { id = value; }
    }

    public IList AddressList
    {
        get { addressList; }
    }

    public string Descr
    {
        get { return descr; }
        set { descr = value; }
    }
}


The mapping file for Address is as follows:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
   <class name="Trigger.Qmis.Services.BusinessEntities.Address,Trigger.Qmis.Services.BusinessEntities" table="Address">

      <id name="Id" column="id" type="Int64" unsaved-value="0">
         <generator class="identity"/>
      </id>
      <bag name="ContactAddressList" inverse="true" lazy="true" >
         <key column="addressId" />
         <one-to-many class="Trigger.Qmis.Services.BusinessEntities.ContactAddress,Trigger.Qmis.Services.BusinessEntities" />
      </bag>
      <many-to-one name="AddressTypeId" column="addressTypeId" class="Trigger.Qmis.Services.BusinessEntities.AddressType,Trigger.Qmis.Services.BusinessEntities" />
   </class>
</hibernate-mapping>


When I get a request in from the client to create a new address, I will receive the address details including the address type id. So I want to be able to do the following:
Code:
Address addr = new Address();
// Populate other address attributes

AddressType type = new AddressType();
type.Id = idFromClient;

addr.addressType = type;

session.Save(addr);


This does the save fine, however I want to return the newly created address back to the client. Unfortunately if I do this with the code above, the description field of the address type will be blank. If I do a load immediately after the save and return the loaded addr, the type description is still blank as the cached version of addr is returned and the address type doesn't get refreshed.

The only way I have found to do what I want to achieve is the following:
Code:
Address addr = new Address();
// Populate other address attributes

AddressType type = new AddressType();
type.Id = idFromClient;

addr.addressType = type;

session.Save(addr);

session.Evict(addr);
session.Load(addr, addr.Id);


I really don't like having to do the evict, is there any way of forcing NHibernate to refresh all of its children in the way I want? I don't want to set cascade to all or anything on the address type mapping as I don't want the address type to be updated here.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 15, 2007 3:48 pm 
Senior
Senior

Joined: Sat Sep 10, 2005 3:46 pm
Posts: 178
In your code you are creating a new address and not supplying a description so I dont see why you are expecting to have a description there.

Are you actually trying to get an already existing address type?
Try this:

Code:
AddressType type = session.Get<AddressType>(idFromClient);


P.S. You should generally make your Id properties immutable.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 18, 2007 12:47 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can reload objects after update/insert with session.Refresh(object), e.g. if you use triggers that modifiy the object during update/insert.

Have you checked, if your AddressType objects are really written to the table ? Looking to your piece of code, I miss the session.Save(addressType). The child will not be persisted automatically unless you have a parent mapping in your AddressType mapping.

Regards,
Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 7:49 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 11:26 am
Posts: 33
The address type that I am giving the address, will already exist therefore because I have not specified any cascading for the address type, the address type description will not be overwritten.

What I wanted to be able to do was to get the address type description to be populated without having to do any intermediate steps - whether that be reading the adress type in order to populate the address type field or doing a refresh after the update.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 19, 2007 8:40 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm not really sure, if I understand what you want to do. Your idea is to assign the Id to a new AddressType object and want Hibernate to automatically load the corresponding object from the database ?
I think there is no way of doing this. You can either initially load the address type objects and keep the references in a collection which you can index with your id or you can retrieve the object from the session when you need it like jnapier wrote.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.