The problem description is below:
I have 3 tables: Captador, CaptadorFisico and a CaptadorJuridico. The Captador has the primary key and depending of the value of the tipo_pessoa it has a child CaptadorFisico or CaptadorJuridico with the primary key as a foreign key to the Captador.
Ok. When I mapping this entities I put for the Captador:
Code:
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="SisImob.Database.Captador, SisImob.Database" table="Captador">
<id name="Id" column="idCaptador">
<generator class="native">
<param name="sequence">Captador_idCaptador_seq</param>
</generator>
</id>
<property name="DataRegistro" type="Timestamp" not-null="true" />
<property name="TipoPessoa" type="Int32" not-null="true" />
<property name="Ativo" not-null="true" />
<component name="Local">
<property name="TelCom" type="Serializable" />
<property name="TelRes" type="Serializable" />
<property name="Celular" type="Serializable" />
<property name="Fax" type="Serializable" />
<property name="TelDemais" length="255" />
<property name="Homepage" length="80" />
<property name="Email" length="60" />
<property name="Logradouro" length="80" not-null="true" />
<property name="Lote" length="10" />
<property name="Quadra" length="10" />
<property name="Numero" length="10" not-null="true" />
<property name="Edificio" length="80" />
<property name="Apartamento" length="10" />
<property name="Complemento" length="100" />
<property name="Setor" length="80" not-null="true" />
<property name="Municipio" length="80" not-null="true" />
<property name="Estado" length="2" not-null="true" />
<property name="Referencia" length="255" />
<property name="Cep" type="Serializable" not-null="true" />
</component>
<one-to-one
property-ref="Parent"
name="DadosFisicos"
class="SisImob.Database.CaptadorFisico, SisImob.Database"/>
<one-to-one
property-ref="Parent"
name="DadosJuridicos"
class="SisImob.Database.CaptadorJuridico, SisImob.Database"/>
</class>
</hibernate-mapping>
For the others:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="SisImob.Database.CaptadorFisico, SisImob.Database" table="CaptadorFisico">
<id name="Id" column="idCaptador">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>
<property name="Nome" length="80" not-null="true" />
<property name="Sexo" type="Int32" not-null="true" />
<property name="Nacionalidade" length="60" not-null="true" />
<property name="Profissao" length="60" not-null="true" />
<property name="Cpf" type="Serializable" not-null="true" />
<property name="Rg" type="Serializable" not-null="true" />
<property name="EstadoCivil" type="Int32" not-null="true" />
<one-to-one
name="Parent"
cascade="delete"
class="SisImob.Database.Captador, SisImob.Database"
constrained="true"/>
</class>
</hibernate-mapping>
and
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:nhibernate-mapping-2.0">
<class name="SisImob.Database.CaptadorJuridico, SisImob.Database" table="CaptadorJuridico">
<id name="Id" column="idCaptador">
<generator class="foreign">
<param name="property">Parent</param>
</generator>
</id>
<property name="Razao" length="80" not-null="true"/>
<property name="Cnpj" type="Serializable" not-null="true" />
<property name="InscEstadual" length="60"/>
<property name="InscMunicipal" length="60"/>
<property name="Responsavel" length="80"/>
<one-to-one
name="Parent"
constrained="true"
cascade="delete"
class="SisImob.Database.Captador, SisImob.Database"/>
</class>
</hibernate-mapping>
At now is good, but if I put the attribute cascade = all for the Captador mapping for the children and later try to save a object Captador it give me errors like if the children don't get the ID of the parent, I guess.
So what is wrong? I tryed so many thing like put the attribute property-ref in the Captador mapping and others.
Obs.: I need the mapping to be bidirectional