DavyBrion wrote:
try changing the cascade option to "save-update"
i think the cascade option refers to the entities of the other side of the many-to-many relationship, not the actual link record. The link records are taken care of automatically, and the cascade option doesn't really affect those. At least, i think that's how it is, but i'm not entirely sure though.
I tried to make a simple unit test to understand what is going on with my problem but it seems not to work anyway.
So i got these two mapping files:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Tag, BusinessEntities" table="Tags" lazy="false">
<id name="TagID" column="TagID" type="String">
<generator class="assigned" />
</id>
<bag name="Informazioni" table="Tags_Informazioni" cascade="all-delete-orphan">
<key column="TagID"/>
<many-to-many column="InformazioneID" class="Informazione, BusinessEntities"/>
</bag>
</class>
</hibernate-mapping>
?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Informazione, BusinessEntities" table="Informazioni" lazy="false">
<id name="InformazioneID" column="InformazioneID">
<generator class="identity" />
</id>
<discriminator column="TipologieInformazioneID"/>
<subclass name="Intervento, BusinessEntities" discriminator-value="0">
</subclass>
<subclass name="Criticita, BusinessEntities" discriminator-value="1">
</subclass>
</class>
</hibernate-mapping>
And the Tag class is:
public class Tag
{
private string tagID;
private IList<Informazione> informazioni = new List<Informazione>();
....
}
Here is a piece of code of the test:
// create two Tag Objects and add them an Informazione object
NHibernateSessionManager.Instance.BeginTransaction();
Criticita criticita = new Criticita();
ITagDao tagDao = daoFactory.GetTagDao();
Tag tag1 = new Tag("1234123412341234", DateTime.Now);
tag1.Informazioni.Add(criticita);
tagDao.Save(tag1);
Tag tag2 = new Tag("4321432143214321", DateTime.Now);
tag2.Informazioni.Add(criticita);
tagDao.Save(tag2);
NHibernateSessionManager.Instance.CommitTransaction();
NHibernateSessionManager.Instance.CloseSession();
//try to delete the Informazione from one object
NHibernateSessionManager.Instance.BeginTransaction();
IInformazioneDao infoDao = daoFactory.GetInformazioneDao();
Informazione informazione = infoDao.GetById(262, false);
ITagDao tagDao = daoFactory.GetTagDao();
Tag tag = tagDao.GetById("4321432143214321", false);
tag.Informazioni.Remove(informazione);
tagDao.SaveOrUpdate(tag);
NHibernateSessionManager.Instance.CommitTransaction();
NHibernateSessionManager.Instance.CloseSession();
In my test i create two Tag parent objects, add them the same children Informazione and then i remove it from the first tag List.
When i remove it from the first Tag object and save again the Tag, the relation beteween the second Tag object and the Informazione is also deleted; I think that is not expected from the cascade"all-delete-orphan" setting.
I tried to change the cascade constraint as cascade="save-update" and obviously when I remove the Informazione reference from all of the List nhibernate does not delete the record in the Informazioni table (it leaves the record "orphan" as expected).
Any idea of what is wrong with my mapping/settings?