These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: set collection problem
PostPosted: Wed Jun 03, 2009 2:13 pm 
Newbie

Joined: Mon Nov 12, 2007 10:43 pm
Posts: 7
Hello I have a problem with my set collection

This is my set mapping
<set name="NotasList" lazy="true" cascade="all">
<key column="IDMatricula"/>
<one-to-many class="Nota"/>
</set>

I load the root object, the one that contains the set collection.
I add a new item to the collection
then I persist
Then I add a new item to the collection
Then I persist again
and the new item is added but the previous one are removed from the collection.
But if I add an item to the collection
then persist
then create a new session
then load the sema root object
then add a new item to the collection
then persist, nothing go wrong.


this is the database log
INSERT INTO smndb.notas (FechaRegistro, ValorNota, IDProfesor, IDMateria, IDParcialDivicion, IDNotas) VALUES ('2009-06-03 12:55:28', 2, 1, 1872, 594, 10297)
25 Query UPDATE smndb.notas SET IDMatricula = null WHERE IDMatricula = 1486 AND IDNotas = 10296
25 Query UPDATE smndb.notas SET IDMatricula = 1486 WHERE IDNotas = 10297

as you can see the old inserted item is getting a null value to the reference ID so it is removed from the collection.

I'm using the last nhibernate release 2.1.0 from trunk and tested with ISet and ICollection


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Wed Jun 03, 2009 3:32 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Can you post the code for adding and persisting ? And the mapping file for the collection items.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Wed Jun 03, 2009 4:29 pm 
Newbie

Joined: Mon Nov 12, 2007 10:43 pm
Posts: 7
This is a test code:

Code:
IWorkspace mws = WorkspaceFactory.CreateWorkspace(); // This encapsulate the session
            IList<Paralelo> paralelos = new GenericRepository<Paralelo>(mws).GetAll();  // _session.CreateCriteria(typeOfResult).List<T>();
            IList<CursoMateria> cursos = new GenericRepository<CursoMateria>(mws).GetAll();
            //IList<Materia> materias = new GenericRepository<Materia>(mws).GetAll();
            IList<ParcialDivicion> diviciones = new GenericRepository<ParcialDivicion>(mws).GetAll();
            IList<Profesor> profesores = new GenericRepository<Profesor>(mws).GetAll();

            Paralelo paralelo = paralelos[20];
            Materia materia = cursos[1].Materias[1];
            ParcialDivicion divicion = diviciones[0];
            Profesor profesor = profesores[0];


            Nota mnota = new Nota();
            mnota.FechaRegistro = DateTime.Now;
            mnota.Materia = materia;
            mnota.ParcialDivicion = divicion;
            mnota.Profesor = profesor;
            mnota.ValorNota = 1;
            Matricula mat = null;

            foreach (Matricula matr in paralelo.Matriculas)
            {
                mat = matr;
                break;
            }

            mnota.Matricula = mat;
            mat.NotasList.Add(mnota);


            mws.PersistAll(); // _session.Reconnect(); _session.Flush();


// I added this code to clear the session and retreive the object again and this work fine but without this the error happend
            mws.Clean();
            //paralelos = new GenericRepository<Paralelo>(mws).GetAll();
            //paralelo = paralelos[20];
            paralelo = new GenericRepository<Paralelo>(mws).GetById(paralelo.Key);
            foreach (Matricula matr in paralelo.Matriculas)
            {
                mat = matr;
                break;
            }
// End added code

            materia = cursos[1].Materias[2];
            mnota = new Nota();
            mnota.FechaRegistro = DateTime.Now;
            mnota.Materia = materia;
            mnota.ParcialDivicion = divicion;
            mnota.Profesor = profesor;
            mnota.ValorNota = 2;

            mnota.Matricula = mat;
            mat.NotasList.Add(mnota);
            mws.PersistAll();



Paralelo is the root object
As you can see Paralelo has a Matricula iset collection and Matricula has a iset nota collection

Map Files:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="notas.DomainModel" assembly="notas.DomainModel">
  <class name="Paralelo" table="paralelo" where="Eliminado=0">
    <id name="Key" type="Int64" unsaved-value="0">
      <column name="IDParalelo" sql-type="bigint" not-null="true"/>
      <generator class="hilo">
        <param name="table">hi_value</param>
        <param name="column">next_value</param>
        <param name="max_lo">10</param>
      </generator>
    </id>
    <property name="Nombre">
      <column name="Nombre" length="50" sql-type="varchar" not-null="true"/>
    </property>

    <property name="Periodo">
      <column name="Periodo" length="4" sql-type="char" not-null="true"/>
    </property>
    <property name="FechaRegistro">
      <column name="FechaRegistro" sql-type="datetime" not-null="true"/>
    </property>
    <many-to-one name="Curso" lazy="false" column="IDCurso" class="Curso" cascade="none" not-null="false" />
    <set name="Matriculas" lazy="true" cascade="all">
      <key column="IDParalelo"/>
      <one-to-many class="Matricula"/>
    </set>
    <property name="Activo">
      <column name="Activo" sql-type="smallint" not-null="true"/>
    </property>
    <property name="Eliminado">
      <column name="Eliminado" sql-type="smallint" not-null="true"/>
    </property>
  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="notas.DomainModel" assembly="notas.DomainModel">
  <class name="Matricula" table="matricula">
    <id name="Key" type="Int64" unsaved-value="0">
      <column name="IDMatricula" sql-type="bigint" not-null="true"/>
      <generator class="hilo">
        <param name="table">hi_value</param>
        <param name="column">next_value</param>
        <param name="max_lo">10</param>
      </generator>
    </id>

    <many-to-one name="Estudiante" lazy="false" column="IDEstudiante" class="Estudiante" cascade="none" not-null="false" />

    <many-to-one name="Paralelo" lazy="false" column="IDParalelo" class="Paralelo" cascade="none" not-null="false" />
   
    <set name="NotasList" generic="true" lazy="true" cascade="all" fetch="join">
      <key column="IDMatricula"/>
      <one-to-many class="Nota"/>
    </set>
    <set name="AsistenciaList" lazy="true" cascade="all">
      <key column="IDMatricula"/>
      <one-to-many class="Asistencia"/>
    </set>
   
    <property name="CodigoMatricula">
      <column name="CodigoMatricula" length="20" sql-type="varchar" not-null="false"/>
    </property>

  </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="notas.DomainModel" assembly="notas.DomainModel">
  <class name="Nota" table="notas">
    <id name="Key" type="Int64" unsaved-value="0">
      <column name="IDNotas" sql-type="bigint" not-null="true"/>
      <generator class="hilo">
        <param name="table">hi_value</param>
        <param name="column">next_value</param>
        <param name="max_lo">10</param>
      </generator>
    </id>
    <property name="FechaRegistro">
      <column name="FechaRegistro" sql-type="datetime" not-null="true"/>
    </property>
    <property name="ValorNota">
      <column name="ValorNota" sql-type="decimal" not-null="true"/>
    </property>
   
    <many-to-one name="Profesor" lazy="false" column="IDProfesor" class="Profesor" cascade="none" not-null="false" />
    <many-to-one name="Materia" lazy="false" column="IDMateria" class="Materia" cascade="none" not-null="false" />
    <!--<many-to-one name="TipoNota" lazy="false" column="IDTipoNota" class="TipoNota" cascade="none" not-null="false" />-->
    <many-to-one name="ParcialDivicion" lazy="false" column="IDParcialDivicion" class="ParcialDivicion" cascade="none" not-null="false" />
   
    <many-to-one name="Matricula" column="IDMatricula" class="Matricula" lazy="false" cascade="none"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Thu Jun 04, 2009 1:56 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
What happens in PersistAll() ? Just a flush ? If you create new objects you have to session.Save() them or session.Update() the parent.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Thu Jun 04, 2009 3:02 am 
Newbie

Joined: Mon Nov 12, 2007 10:43 pm
Posts: 7
mws.PersistAll(); // _session.Reconnect(); _session.Flush();


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Thu Jun 04, 2009 3:04 am 
Newbie

Joined: Mon Nov 12, 2007 10:43 pm
Posts: 7
Never have this problem with list only set
and only if I add object and flush then add and flush
If I add and add and then flush no problem or if is add flush reload add and flush no problem.


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Thu Jun 04, 2009 3:39 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try the NHibernate profiler, maybe you can figure out what's going wrong there:

http://nhprof.com/

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: set collection problem
PostPosted: Thu Jun 04, 2009 1:25 pm 
Newbie

Joined: Mon Nov 12, 2007 10:43 pm
Posts: 7
I try the NHibernate profiler but it doesn't detect the problem.
I'm going to create a concrete test to see if it is a bug.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.