Hi everyone, I have a few classes that look like this,
Code:
public class Token
{
public int Id
{
get;
set;
}
public ITokenInstance Instance
{
get;
set;
}
}
public interface ITokenInstance
{
int Id
{
get;
set;
}
Token Token
{
get;
set;
}
}
and mapping files
Code:
<class name="Token" >
<id name="Id" >
<generator class="hilo" />
</id>
<any name="Instance" meta-type="class" id-type="Int32" cascade="all-delete-orphan">
<column name="instance_type" />
<column name="instance_id" />
</any>
</class>
<class name="TokenInstanceOne" >
<id name="Id" >
<generator class="hilo" />
</id>
<many-to-one name="Token" class="Token" column="token_id"/>
</class>
The problem is that if a new instance is assigned to the token the old instance is not deleted from the database, below is an example of the code i need to write
Code:
Token token = Session.Get<Token>(4);
var instance = Session.Get<TokenInstanceOne>(1);
Assert.AreSame(token.Instance, instance);
var newInstance = new TokenInstanceOne();
token.Instance = newInstance;
newInstance.Token = token;
instance.Token = null;
Session.Flush();
NHibernate produces the following SQL;
1. INSERT INTO TokenInstanceOne (token_id, Id) VALUES (@p0, @p1); @p0 = '4', @p1 = '32768'
2. UPDATE Token SET instance_type = @p0, instance_id = @p1 WHERE Id = @p2; @p0 = 'ClassLibrary1.TokenInstanceOne', @p1 = '32768', @p2 = '4'
3. UPDATE TokenInstanceOne SET token_id = @p0 WHERE Id = @p1; @p0 = '', @p1 = '1'
What i need to happen is that in number 3 instead of it updating to '', it should delete the row. If i put not-null="true" on the many-to-one mapping i get an exception:
PropertyValueException : not-null property references a null or transient value
I have also asked this question
here, but have had no luck. Does anyone know how i can map this correctly?