Jordan, thanks for pointing this out, for some reason I got it into my head that the parent should be the one where the many-to-one mapping is, which when I think about it, doesn't make much sense. Ramon is correct though, I did mean the Host to be the parent of the relationship.
Ramon, sorry I removed the Sequence to Host many to one to simplify the test case. Please see below for the corrected mapping for the many-to-one unit test. The unit test still fails with the change btw.
---------------------------------Mapping Declaration---------------------------------------<class name="Host"> <id name="id" column="host_id">
<generator class="native" />
</id>
<many-to-one name ="sequence" column="sequence_id" cascade="all" /></class><class name="Sequence"> <id name="id" column="sequence_id">
<generator class="native" />
</id>
<many-to-one name="host" class="Host" column="host_id" cascade="all"/></class>-------------------------------------------------------------------------------------------If I change the many-to-one to one-to-one, as per below, the unit test still fails.
--------------------------------Mapping Declaration--------------------------------------------- <class name="Host"> <id name="id" column="host_id">
<generator class="native" />
</id>
<one-to-one name ="sequence" cascade="all" /> </class> <class name="Sequence"> <id name="id" column="sequence_id">
<generator class="native" />
</id>
<many-to-one name="host" class="Host" cascade="all"/> </class>--------------------------------------------------------------------------------------------- In fact the only way I have been able to get the unit test to pass was to add a one-to-many list of sequences to the host and remove the sequence from that list, see below.
--------------------------------Class declarations---------------------------------------------
public class Host
{
public virtual int id { get; set; }
public virtual Sequence sequence { get; set; }
public virtual ISet<Sequence> sequences { get; set; }
}
public class Sequence
{
public virtual int id { get; set; }
public virtual Host host { get; set; }}--------------------------------Mapping declarations---------------------------------------------<class name="Host"> <id name="id" column="host_id">
<generator class="native" />
</id>
<many-to-one name ="sequence" column="sequence_id" cascade="all" /> <set name="sequences" inverse="true" cascade="all,delete-orphan"> <key column="host_id" /> <one-to-many class="Sequence" /> </set></class><class name="Sequence"> <id name="id" column="sequence_id">
<generator class="native" />
</id>
<many-to-one name="host" class="Host" column="host_id"/>
</class>
--------------------------------Unit Test---------------------------------------------
[TestFixture]
public class Fixture : BugTestCase
{
[Test]
public void CanDeleteChild()
{
int host_id = 0; int sequence_id = 0;
{
Host host = new Host();
Sequence sequence = new Sequence(); ;
host.sequence = sequence;
sequence.host = host;
using (var s = OpenSession())
{
using (s.BeginTransaction())
{
s.SaveOrUpdate(host);
s.Transaction.Commit();
}
s.Close();
}
host_id = host.id; sequence_id = sequence.id; } using (var s = OpenSession()) { using (s.BeginTransaction()) { var host = s.Get<Host>(host_id); host.sequences.Remove(host.sequence);
host.sequence = null;
s.SaveOrUpdate(host);
s.Transaction.Commit();
}
s.Close();
}
using (var s = OpenSession())
{
Assert.IsNull(s.Get<Sequence>(sequence_id));
sessions.Close();
}
}
}
-------------------------------------------------------------------------------------------Thanks
Michael