At first I have these entities: Persona, Paciente, Alergia, Cita and Consulta.
The problem comes when I try to delete a Persona. My DB looks like this:
I'm currently just working with Persona and Paciente. And those are their mapping and classes:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Odontologia General Admin" namespace="Odontologia_General_Admin.Persistence.Entidades" xmlns="urn:nhibernate-mapping-2.2">
<class name="Persona" table="persona" lazy="true" >
<id name="Cipersona" column="ciPersona" />
<bag name="Cliente" cascade="all-delete-orphan">
<key column="ciCliente"/>
<one-to-many class="Cliente" />
</bag>
<bag name="Paciente" cascade="all-delete-orphan">
<key column="ciPaciente"/>
<one-to-many class="Paciente" />
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Odontologia General Admin" namespace="Odontologia_General_Admin.Persistence.Entidades" xmlns="urn:nhibernate-mapping-2.2">
<class name="Paciente" table="paciente" lazy="true" >
<id name="Cipaciente" column="ciPaciente" />
<bag name="CitasPaciente" table="cita" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="ciPaciente" />
<one-to-many class="Cita"/>
</bag>
<bag name="ConsultasPaciente" table="consulta" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="ciPaciente" />
<one-to-many class="Consulta" />
</bag>
<bag name="AlergiasPaciente" table="alergia_paciente" lazy="true" cascade="all-delete-orphan" inverse="true" >
<key column="ciPaciente"></key>
<many-to-many column="idAlergia" class="Alergia" />
</bag>
</class>
</hibernate-mapping>
And their entities:
public class Persona : Entidad {
public Persona() { Paciente = new List<Paciente>();
Cliente = new List<Cliente>();
public virtual int Cipersona { get; set; }
[Browsable(false)]
public virtual IList<Cliente> Cliente { get; set; }
[Browsable(false)]
public virtual IList<Paciente> Paciente { get; set; }
}
public class Paciente : Entidad
{
public Paciente() { CitasPaciente = new List<Cita>();}
public virtual int Cipaciente { get; set; }
public virtual IList<Alergia> AlergiasPaciente { get; set; }
public virtual IList<Cita> CitasPaciente{ get; set; }
public virtual IList<Consulta> ConsultasPaciente { get; set; }
}
The problem comes when I try to delete a Persona and the cascade somehow try to update a paciente and set it's ID to null
UPDATE paciente SET ciPaciente = null WHERE ciPaciente = ?p0
And SQL throws me exception because that column cannot be null.