I realize that there are plenty of posts that pertain to the deference/cascade issue with collections (answer: use Clear), however, I'm working strongly-typed arrays and can't seem to get around the problem of wanting to add items to the array without having to delete everything first.
For backgrounder, here's my mapping:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
<class name="com.brightharbour.schema.MediaProviderType, BH.Service" table="Providers">
<id name="ProviderId" column="ProviderId" type="String">
<generator class="uuid.hex" />
</id>
<property name="ProviderType" type="String"/>
<property name="ProviderUrl" type="String"/>
<property name="LoginId" type="String"/>
<property name="Password" type="String"/>
<array name="Songs" table="Songs" cascade="all-delete-orphan">
<key column="ProviderId"/>
<!--Matching key in the child table that links to the parent-->
<index column="SongIndex" />
<!-- Column in child table used to hold the index (0-based) for the array-->
<one-to-many class="com.brightharbour.schema.SongType, BH.Service"/>
</array> </class>
</hibernate-mapping>
...and here's where I'm having a problem:
Code:
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
// Use Get to determine if the record already exists
UserType user = (UserType)session.Get(typeof(UserType), userId);
MediaProviderType[] mediaProviders;
// Add a new Provider for the User
int len = user.Providers.Length;
Array.Clear(user.Providers, 0,len);
// Expand the array size by 1 to accommodate the new item
MediaProviderType[] temp = new MediaProviderType[len + 1];
mediaProviders.CopyTo(temp, 0);
user.Providers = temp;
user.Providers[len] = new MediaProviderType();
user.Providers[len].ProviderType = "Orb";
user.Providers[len].LoginId = providerLogin;
user.Providers[len].Password = providerPwd;
// Tell NHibernate to update this User
session.SaveOrUpdate(user);
// commit all of the changes to the DB and close the ISession
transaction.Commit(); //Blows up here
session.Close();
All I want to do here is add an item to the strongly-typed array and persist it back to the DB. I've read in the forums that you need to issue a Clear() first, (which I've tried), but this doesn't seem to help.
Any ideas?