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.