-->
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.  [ 2 posts ] 
Author Message
 Post subject: [newbie] Impossible to persist a many to many relation
PostPosted: Thu Sep 21, 2006 4:01 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 6:15 pm
Posts: 28
Hi,

After hours of search without finding I think I can explain my problem here:

It's a very classic relation many to many involving table user , role et user_role. I can persist user and role but not the bidirectionnal relation.

If you could show me what I'm missing :

Hibernate version: hibernate-3.2.0.cr4

Mapping for User :
Code:
......
<class name="verlina.domain.model.User" table="user">
       
        <id name="id" column="id">
            <generator class="native"/>
        </id>       
       
        <property name = "login" />
        <property name = "password" />
       
        <set name="roles" table="user_role" cascade="all" lazy="false"  outer-join="true">
            <key column="id_user"/>
            <many-to-many class="verlina.domain.model.Role" column="id_role" />
        </set>
   
    </class>
   ......



Mapping for Role :
Code:
....
<class name="verlina.domain.model.Role" table="role">
       
        <id name="id" column="id">
            <generator class="native"/>
        </id>
       
        <property name="name"/>
       
        <set name="users" table="user_role" cascade="all" lazy="false" outer-join="true" inverse="true">
            <key column="id_role"/>
            <many-to-many class="verlina.domain.model.User" column="id_user" />
        </set>
   
</class>
.....




Code between sessionFactory.openSession() and session.close():
Code:
        User u = new User();
        u.setLogin("mimi"); u.setPassword("cracra");
        Role r = new Role();
        r.setName("ROLE_SUPERADMIN5");
        u.addRole(r);
        session.save(u);



Code that add a role to a user:
Code:
class User{
.....
   public void addRole(Role r){
        getRoles().add(r);
        r.getUsers().add(this);
   }
.....
}



Probleme:

No warning or error message , the role and user object are correctly persited in the database but not their relation, ie the table user_role is not changed at all.

Any help would be greatly appreciated.


Top
 Profile  
 
 Post subject: I answer to myself
PostPosted: Thu Sep 21, 2006 7:01 am 
Beginner
Beginner

Joined: Sun Jul 31, 2005 6:15 pm
Posts: 28
Actually I found out myself the problem.

I forgot to commit the transaction. It's kind of confusing anyway that calling session.save(object) persist the graph of object but not their relationship. It's when you call tx.commit() that you persist the relationship.

It doesn't make sens in my opinion as relationship between object is part of the properties of the objects.


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