Hi All,
I have a problem. I have developed a persistentClass that contains a collection (many-to-many relattion). The problem is that when I add an Item into collection it is saved correctly, but when I get this persistent class in other session, the collection is empty, and I don't know why this collection doesn't contain the items.
Can you help me please? Thanks in advance!
Code:
public Model.Client getClient(string codi)
{
Model.Client client = null;
try
{
NHibernate.ITransaction transaction = this.session.BeginTransaction();
client = (Model.Client)this.session.Get(typeof(Model.Client), codi);
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
return client;
}
------------------------------------
public void addDireccioEntrega(string codi_client, Model.DireccioEntrega entrega)
{
try
{
this.save(entrega);
Model.Client client = (Model.Client)this.session.Get(typeof(Model.Client), codi_client);
if (client != null) client.Direccions_entrega.Add(entrega);
this.session.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine(ex.InnerException.Message);
}
}
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Model.Client, Broker" table="CLIENT">
<id name="Codi" column="CODI" type="String">
<generator class="assigned" />
</id>
<property name="Descripcio" column="DESCRIPCIO" type="String"/>
......
<set name="Direccions_entrega" table="CLIENTS_TENEN_DIRECCIO_ENTREGUES" cascade="all">
<key column="CODI"/>
<many-to-many column="ADREĆA" class="Model.DireccioEntrega, Broker"/>
</set>
</class>
</hibernate-mapping>
Code:
namespace Model
{
public class Client
{
private string codi;
private string descripcio;
private string alies;
private System.DateTime data_alta;
private string nif;
private Model.Direccio direccio;
private Iesi.Collections.ISet direccions_entrega;
private Iesi.Collections.ISet contactes;
protected Client ()
{
this.codi = "";
this.descripcio = "";
this.alies = "";
this.data_alta = System.DateTime.MinValue;
this.nif = "";
this.direccio = null;
this.direccions_entrega = new Iesi.Collections.ListSet();
this.contactes = new Iesi.Collections.ListSet();
}
public Client(string codi)
{
this.codi = codi;
this.descripcio = "";
this.alies = "";
this.data_alta = System.DateTime.MinValue;
this.nif = "";
this.direccio = null;
this.direccions_entrega = new Iesi.Collections.ListSet();
this.contactes = new Iesi.Collections.ListSet();
}
public Client(string codi, string descripcio, string alies, System.DateTime data_alta, string nif, Model.Direccio direccio)
{
this.codi = codi;
this.descripcio = descripcio;
this.alies = alies;
this.data_alta = data_alta;
this.nif = nif;
this.Direccio = direccio;
this.direccions_entrega = new Iesi.Collections.ListSet();
this.contactes = new Iesi.Collections.ListSet();
}
#region Propietats
public virtual string Descripcio
{
get { return this.descripcio; }
set { this.descripcio = value; }
}
public virtual string Alies
{
get { return this.alies; }
set { this.alies = value; }
}
public virtual string Codi
{
get { return this.codi; }
set { this.codi = value; }
}
public virtual System.DateTime Data_alta
{
get { return this.data_alta; }
set { this.data_alta = value; }
}
public virtual string Nif
{
get { return this.nif; }
set { this.nif = value; }
}
public virtual Model.Direccio Direccio
{
get { return this.direccio; }
set { this.direccio = value; }
}
public virtual Iesi.Collections.ISet Direccions_entrega
{
get { return this.direccions_entrega; }
set { this.direccions_entrega = value; }
}
public virtual Iesi.Collections.ISet Contactes
{
get { return this.contactes; }
set { this.contactes = value; }
}
#endregion
}
}