HasMany Collection not mapped in Joined Subclass

217 views
Skip to first unread message

Del Brocco Andrea

unread,
Dec 20, 2010, 8:47:01 PM12/20/10
to Fluent NHibernate
I have the following situation

I have tried with NH2.x and NH 3.x

Anagrafica Type and a Persona Type derived from anagrafica.


Two main table ANA_Anagrafiche and ANA_PERSONE one to one the
discriminate subcolumn is TipoAnagrafica value 'PF' with some other
collections and mappings ....

the real situation is with more joined subclasses.

If i have a collection in a joined subclass i have no success to read.

with manual hbm mapping it goes well

Mapping:


public class AnagraficaMap :
ClassMap<Domain.Model.Anagrafica.Anagrafica>
{
public AnagraficaMap()
{
Table("ANA_Anagrafiche");
DynamicInsert();
DynamicUpdate();
LazyLoad();
Id(x => x.Id, "ID_ANAGRAFICA")
.GeneratedBy.Guid().UnsavedValue(Guid.Empty);
Map(x => x.Denominazione, "Denominazione");
Map(x => x.IsAttivo, "isAttivo");
Map(x => x.CodiceFiscale, "Codice_Fiscale");
Map(x => x.PartitaIva, "Partita_IVA");
DiscriminateSubClassesOnColumn("StatTipoAnagrafica");
HasMany(prop => prop.Emails)
.AsBag()
.Fetch.Subselect()
.KeyColumn("ID_ANAGRAFICA")
.LazyLoad()
.Inverse()
.Cascade.AllDeleteOrphan();
HasMany(prop => prop.Indirizzi)
.AsBag()
.Fetch.Select()
.KeyColumn("ID_ANAGRAFICA")
.LazyLoad()
.Cascade.SaveUpdate();
HasMany(prop => prop.Telefoni)
.AsBag()
.Fetch.Select()
.KeyColumn("ID_ANAGRAFICA")
.LazyLoad()
.Cascade.AllDeleteOrphan();

}
}



public class PersonaMap : SubclassMap<Persona>
{
public PersonaMap()
{
Table("ANA_Persone");
LazyLoad();
Join("ANA_Persone", j =>
{
j.KeyColumn("ID_PERSONA");
j.Map(x => x.Cognome, "COGNOME");
j.Map(x => x.Nome, "NOME");
j.Map(x => x.DataNascita,
"DATA_NASCITA");
j.Map(x => x.Sesso, "SESSO");
j.Map(x => x.LuogoNascita,
"LUOGO_NASCITA");
j.References(x => x.ComuneNascita)
.LazyLoad()
.Nullable()
.Fetch.Select()
.Column("ID_COMUNENASCITA")
.NotFound.Ignore();
j.References(x =>
x.StatoEsteroNascita)
.LazyLoad()
.Nullable()
.Fetch.Select()
.Column("ID_STATOESTERONASCITA")
.NotFound.Ignore();
j.References(x => x.Cittadinanza)
.LazyLoad()
.Nullable()
.Fetch.Select()
.Column("ID_CITTADINANZA")
.NotFound.Ignore();
j.References(x => x.Appellativo)
.Nullable()
.Cascade.SaveUpdate()
.Column("ID_APPELLATIVO")
.LazyLoad()
.Fetch.Select();
j.HasMany(prop => prop.Iscrizioni)
.AsBag()
.Inverse()
.Fetch.Select()
.KeyColumn("ID_PERSONA")
.LazyLoad()
.Cascade.AllDeleteOrphan();

j.HasMany(prop =>
prop.TitoliStudio)
.AsBag()
.Fetch.Select()
.KeyColumn("ID_PERSONA")
.LazyLoad()
.Cascade.SaveUpdate();

});
DynamicInsert();
DynamicUpdate();
DiscriminatorValue("PF");
}
}


the problem is that i can't obtains collections in Persona the
references goes well but hasmany have some problems , it seems that it
does not use tha HasMany in derived class, the collections in
Anagrafica worls well.

the xml mapping saved from this configuration does not track this
collections.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-
access="property" auto-import="true" default-cascade="none" default-
lazy="true">
- <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true"
dynamic-update="true" lazy="true" mutable="true"
table="ANA_Anagrafiche">
- <id name="Id" type="System.Guid, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-
value="00000000-0000-0000-0000-000000000000">
<column name="ID_ANAGRAFICA" />
<generator class="guid" />
</id>
- <discriminator type="String">
<column name="StatTipoAnagrafica" />
</discriminator>
- <bag cascade="all-delete-orphan" fetch="subselect" inverse="true"
lazy="true" name="Emails" mutable="true">
- <key>
<column name="ID_ANAGRAFICA" />
</key>
<one-to-many class="Email" />
</bag>
- <bag cascade="save-update" fetch="select" lazy="true"
name="Indirizzi" mutable="true">
- <key>
<column name="ID_ANAGRAFICA" />
</key>
<one-to-many class="IndirizzoAnagrafico" />
</bag>
- <bag cascade="all-delete-orphan" fetch="select" lazy="true"
name="Telefoni" mutable="true">
- <key>
<column name="ID_ANAGRAFICA" />
</key>
<one-to-many class="GoPro.Domain.Model.Anagrafica.Telefono,
GoPro.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /
>
</bag>
- <property name="Denominazione" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Denominazione" />
</property>
- <property name="IsAttivo" type="System.Boolean, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="isAttivo" />
</property>
- <property name="CodiceFiscale" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Codice_Fiscale" />
</property>
- <property name="PartitaIva" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Partita_IVA" />
</property>
+ <subclass name="Persona" lazy="true" dynamic-update="true" dynamic-
insert="true" discriminator-value="PF">
- <join table="ANA_Persone">
- <key>
<column name="ID_PERSONA" />
</key>
- <property name="Cognome" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="COGNOME" />
</property>
- <property name="Nome" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="NOME" />
</property>
- <property name="DataNascita"
type="System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="DATA_NASCITA" />
</property>
- <property name="Sesso" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="SESSO" />
</property>
- <property name="LuogoNascita" type="System.String, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="LUOGO_NASCITA" />
</property>
- <many-to-one class="LocalitaGeografica" fetch="select" lazy="proxy"
name="ComuneNascita" not-found="ignore">
<column name="ID_COMUNENASCITA" not-null="false" />
</many-to-one>
- <many-to-one class="LocalitaGeograficaNazione" fetch="select"
lazy="proxy" name="StatoEsteroNascita" not-found="ignore">
<column name="ID_STATOESTERONASCITA" not-null="false" />
</many-to-one>
- <many-to-one class="LocalitaGeograficaNazione" fetch="select"
lazy="proxy" name="Cittadinanza" not-found="ignore">
<column name="ID_CITTADINANZA" not-null="false" />
</many-to-one>
- <many-to-one cascade="save-update" class="Appellativo"
fetch="select" lazy="proxy" name="Appellativo">
<column name="ID_APPELLATIVO" not-null="false" />
</many-to-one>
</join>
</subclass>
</class>
</hibernate-mapping>

Eduardo Santos

unread,
Jan 21, 2011, 2:31:43 PM1/21/11
to Fluent NHibernate
Hi.

I have the same problem, when I use the join in subclass the
NHibernate not brings me the list of object.
my solution was put out join.

In your case:

public PersonaMap()
        {
            Table("ANA_Persone");
            LazyLoad();
            Join("ANA_Persone", j =>
                                {
                                    j.KeyColumn("ID_PERSONA");
...............
}
HasMany(x=> x.Iscrizioni)
.AsBag()
.Inverse()
.Fetch.Select()
.KeyColumn("ID_PERSONA")
.LazyLoad()
.Cascade.AllDeleteOrphan();
HasMany(x=> x.TitoliStudio)
.AsBag()
.Fetch.Select()
.KeyColumn("ID_PERSONA")
.LazyLoad()
.Cascade.SaveUpdate();
......
}

i'm testing now.. because i don't know if is a correct form...

thanks,
Eduardo Santos


On Dec 20 2010, 11:47 pm, Del Brocco Andrea <a.delbro...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages