-->
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.  [ 5 posts ] 
Author Message
 Post subject: Special many-to-many mapping - help plz
PostPosted: Fri Aug 25, 2006 7:01 am 
Newbie

Joined: Fri Aug 25, 2006 6:46 am
Posts: 2
Hi everyone,

I'm having problem with mapping a relationship between two tables in muy BD. I have been reading the referencial manual of Hibernate and i have found nothing similar to what i'm trying to do. Maybe it's easy, but i'm really new in using Hibernate, so any help you can give me would be wellcome. My BD has a table for users of my application and another one for the systems registered in BD. Each users can play two kind of role for the systems: Administrator or consultant. the relationship is m:n. Each user can be relationed with several systems and viceversa. The way to indicate the role for that system is using another boolean field (true if admin or false otherwise). The situation is shown below:

User
-----
id
// others

System
--------
id
// others

Users_Systems
-----------------
id_user
id_system
admin (boolean)


How can i map this relationship using this boolean value instead of two tables?

Thanks in advance and regards

PS: Sorry, it didn't was well shown


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 11:48 am 
Newbie

Joined: Fri Apr 15, 2005 9:22 am
Posts: 11
I'm looking for the same answer.

Usually having a many-to-many relation the table in the middle hasn't a mapping so the class for that table does not exist.
But I think under this situation the as the mapping as the class should exist.

A ( User ) -> C ( User_System ) <- B ( Sytem )

A would have a Collection of userSystems.
B would also have a Collection of userSystems.
C would have a reference to an User, and also a reference to a System, and also the property saying the type of Rol for that User on that System.

Is that correct ?

And how would be the mappings for that ? I'm a newbie to hibernte too =)

Regards.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 12:05 pm 
Newbie

Joined: Fri Apr 15, 2005 9:22 am
Posts: 11
Forgot to mention I had a similar schema with mapping

Mapping for the many-to-many relation table (SystemUser)
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="sample" auto-import="true">
   <class name="SystemUser" table="SYSTEM_USER">
      <composite-id>
         <key-property name="user">
            <column name="id_user" sql-type="NUMBER(19)" not-null="true" length="19"/>
         </key-property>
         <key-property name="system">
            <column name="id_system" sql-type="NUMBER(19)" not-null="true" length="19"/>
         </key-property>         
      </composite-id>

      <property name="rol" column="rol" type="string" />
   </class>
</hibernate-mapping>



Mapping for User
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="sample" auto-import="true">
   <class name="User" table="USER">
   <id name="id" column="id_user">
      <generator class="sequence">
         <param name="sequence">User_SQ</param>
           </generator>
   </id>

      <bag name="systemUsers" outer-join="false" lazy="true" optimistic-lock="true">
         <key on-delete="noaction" unique="false">
            <column name="id_user" sql-type="NUMBER(19)" not-null="true" length="19"/>
         </key>
         <one-to-many class="SystemUsers"/>
      </bag>
   </class>
</hibernate-mapping>


Mapping for System

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="sample" auto-import="true">
   <class name="System" table="SYSTEM">
   <id name="id" column="id_system">
      <generator class="sequence">
         <param name="sequence">System_SQ</param>
           </generator>
   </id>

      <bag name="systemUsers" outer-join="false" lazy="true" optimistic-lock="true">
         <key on-delete="noaction" unique="false">
            <column name="id_system" sql-type="NUMBER(19)" not-null="true" length="19"/>
         </key>
         <one-to-many class="SystemUsers"/>
      </bag>
   </class>
</hibernate-mapping>




Code:
public class User {

   private Collection systemUsers;

   //....

}


Code:
public class System {

   private Collection systemUsers;

   //....

}


Code:
public class SystemUser {

   private User user;
   private System system;
   private Rol rol;

   //....

}


Is this OK ?

By the way, I had an error with those mapping.

And had to convert SystemUser.User type to Long and System.system type to Long too.

[/quote]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 2:12 am 
Newbie

Joined: Fri Aug 25, 2006 6:46 am
Posts: 2
Hi again,

I have to try about this solution, but i think it could work. Maybe if we use a relational table with three foreing keys (user, system and one for another table for roles), we would have found the solution. I've to investigate it, but i think it could work. This way, the User class would be:

class User {
Set roles = new Hashset();
// Rest of the code
}

and:

class Role {
Long id;
String name;
User user;
System system;

// Getters & Setters
}

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 8:40 am 
Newbie

Joined: Fri Apr 15, 2005 9:22 am
Posts: 11
Any hibernate user that knows how to model what I post above ?

Regards


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