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.  [ 3 posts ] 
Author Message
 Post subject: many-to-many relationship
PostPosted: Tue Jul 04, 2006 1:42 pm 
Newbie

Joined: Mon Jul 03, 2006 4:49 pm
Posts: 5
Hibernate version: LAST

Mapping documents:

Code:
   public class Aluno
    {
        private Int64 _id;
        private Usuario _usuario;
        private ISet _turmas = new HashedSet();

        public virtual Int64 id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual Usuario usuario
        {
            get { return _usuario; }
            set { _usuario = value; }
        }

        public virtual ISet turmas
        {
            get { return _turmas; }
            set { _turmas = value; }
        }
    }

---------------------------------------------------------------------------------------
Code:
   public class Turma
    {
        private Int64 _id;
        private string _nome;
        private short _particular;
        private ISet _alunos = new HashedSet();

        public virtual Int64 id
        {
            get { return _id; }
            set { _id = value; }
        }

        public virtual string nome
        {
            get { return _nome; }
            set { _nome = value; }
        }

        public virtual short particular
        {
            get { return _particular; }
            set { _particular = value; }
        }

        public virtual ISet alunos
        {
            get { return _alunos; }
            set { _alunos = value; }
        }
    }

---------------------------------------------------------------------------------------
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="Core" namespace="Core.Entidade">
  <class name="Aluno" table="tb_aluno" dynamic-update="true">
    <id name="id" column="id_aluno">
      <generator class="native"/>
    </id>
    <set name="turmas" table="tb_turma_aluno" lazy="false" cascade="all" inverse="true">
      <key column="id_aluno"/>
      <many-to-many class="Turma" column="id_turma" outer-join="true"/>
    </set>   
    <one-to-one name="usuario" class="Usuario" cascade="all" constrained="true" fetch="join"/>
  </class>

</hibernate-mapping>

---------------------------------------------------------------------------------------
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0" assembly="Core" namespace="Core.Entidade">
  <class name="Turma" table="tb_turma" dynamic-update="true">

    <id name="id" column="id_turma">
      <generator class="native"/>
    </id>

    <set name="alunos" table="tb_turma_aluno" lazy="false"  cascade="all" inverse="true">
      <key column="id_turma"/>
      <many-to-many class="Aluno" column="id_aluno" outer-join="auto"/>
    </set>
   
    <property name="nome"       column="turma_nome"       type="string" />
    <property name="particular" column="turma_particular" type="short" />

  </class>

</hibernate-mapping>

---------------------------------------------------------------------------------------
Code between sessionFactory.openSession() and session.close():
Code:
       Aluno aluno = (Aluno) session.Load(typeof(Aluno), Convert.ToInt64(5));

        Turma turma = new Turma();       
        turma.nome = "Test";       
        turma.particular = 1;

        turma.alunos.Add(aluno);

        ITransaction transact = session.BeginTransaction();
       
        session.Save(turma);

        transact.Commit();

Name and version of the database you are using: SQL Server 2000

Hibernate don't insert any record in the relationship table (tb_turma_aluno).

When i inserted some records in 'tb_turma_aluno' manually and made a search using nhibernate, the result was perfectly.

Please, help me.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 4:55 pm 
Newbie

Joined: Mon Jul 03, 2006 4:49 pm
Posts: 5
No body ? It's urgent !


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 04, 2006 5:02 pm 
Regular
Regular

Joined: Wed Jun 21, 2006 3:13 pm
Posts: 110
I think your problem is that you have both sides set as inverse. Take it off of the side you're adding the child records to.

For reference, here's one of my functional many-to-many mappings
Code:
<bag name="Roles" lazy="true" cascade="all-delete-orphan" generic="true" table="AccountRoles">
<key column="accountId"></key>
<many-to-many class="MobysThinkTank.mobyProject.Role, mobyProject" column="roleId"></many-to-many>
</bag>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.