-->
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.  [ 4 posts ] 
Author Message
 Post subject: [MAPPING] many-to-many, give me partials inserts (an fail)
PostPosted: Tue Mar 09, 2010 11:22 am 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
Hi everyone,

I have a relation between "User" and "Role"s. But this association will not be mapped to the "User" or the "Role" object. She will be in a "Affectation" object.

My tables (PostgreSQL) :
Code:
CREATE TABLE user (
  uid SERIAL NOT NULL
);
CREATE TABLE role (
  rid SERIAL NOT NULL
);
CREATE TABLE user_role (
  uid INTEGER NOT NULL,
  rid INTEGER NOT NULL,
  -- FOREIGN KEYS ...
);


My "Affectation" object that is used to store teh relation between one user and his roles.
Code:
public class Affectation {
  private User user;
  private List<Role> roles;

  // get and set..
}


And finally, my mapping :
Code:
<class name="xx.Affectation" table="user_role">
  <id column="uid" name="id">
    <generator class="foreign">
      <param name="property">user</param>
    </generator>
  </id>

  <one-to-one name="user" constrained="true" />   

  <bag name="roles" table="user_role" lazy="false">
    <key column="uid"/>            
    <many-to-many class="xx.Role" column="rid" unique="false"/>
  </bag>
</class>


But when y try toplay with this mapping, I can't insert (and thus cannot continue) because Hibernate run this query
Code:
insert into user_role (uid) values ('2')

And I have a NOT NULL constraint on the "user_role"."rid" column. I have tried with "cascade=all" or "cascade=save-update" and also "inverse=true" but generate insert is the same..

Can someone help me ?
Thanks

EDIT :
If I remove the NOT NULL constraint in the column "rid" Hibernate play these two queries :
Code:
Hibernate: insert into user_role (uid) values (?)
Hibernate: insert into user_role(uid, rid) values (?, ?)

And my table contains two rows :
uid=2; rid=1
uid=2; rid=null
...


Top
 Profile  
 
 Post subject: Re: [MAPPING] many-to-many, give me partials inserts (an fail)
PostPosted: Tue Mar 09, 2010 3:14 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The mapping doesn't match the database schema. There is not a <one-to-one> relation between User and Affectation. Since the user_role table contains one uid column and one rid column, the association between Affectation and User / Role should be <many-to-one> for both of them. And the id of the Affectation class is a composite id containing both the uid and rid columns. For an example of how to map this see
http://docs.jboss.org/hibernate/stable/ ... ompositeid
(The example is a bit more complex than your setup)


Top
 Profile  
 
 Post subject: Re: [MAPPING] many-to-many, give me partials inserts (an fail)
PostPosted: Wed Mar 10, 2010 3:59 am 
Beginner
Beginner

Joined: Fri May 23, 2008 4:37 am
Posts: 25
Yes, of course, there is no <one-to-one> relation between User and Affectation. But I can't see how a <many-to-one> relation between User and Relation can help me to achieve what I want : Having one Affectation that contains one User and many Roles.
I will try the example but I don't know how to make my composite id because the Affectation object will contains more than one Role.

Thanks


Top
 Profile  
 
 Post subject: Re: [MAPPING] many-to-many, give me partials inserts (an fail)
PostPosted: Wed Mar 10, 2010 4:06 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Having one Affectation that contains one User and many Roles.


There is no table in the code you have shown that is able to store this kind of relationship. Why not simple get rid of the Affectation altogether and simply use only User and Role with a many-to-many association?


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