-->
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.  [ 1 post ] 
Author Message
 Post subject: Many-to-many with composite key
PostPosted: Thu Feb 10, 2011 4:50 am 
Newbie

Joined: Thu Feb 10, 2011 3:48 am
Posts: 1
Hello
My problem is how to configure a many-to-many relation between 2 tables with an associative table with composite keys.

Here's my Database schema (simplified)

Code:
AREA , DOC  and ROLE are tables with an ID and a VALUE (ie ID_AREA and VALUE_AREA for example)

the Table ROLE_NAME have for PK the 3 PK of the previous tables
ID_AREA
ID_DOC
ID_ROLE
VALUE_ROLE_NAME

Table SECURITY
ID_SEC (not ID_SECURITY !!)
VALUE_SECURITY

Table SECURITYGROUP
ID_AREA
ID_DOC
ID_ROLE
ID_SECURITY 


Here the mapping for the ROLE NAME table
Code:
<hibernate-mapping>
  <class name="Role_Name" table="ROLE_NAME">
   
   <composite-id>
      <key-many-to-one name="area" class="Area" column="ID_AREA" lazy="false"></key-many-to-one>
      <key-many-to-one name="doc" class="Doc" column="ID_DOC" lazy="false"></key-many-to-one>
      <key-many-to-one name="role" class="Role" column="ID_ROLE" lazy="false"></key-many-to-one>
   </composite-id>
   
   <property name="value_role_name" column="VALUE_ROLE_NAME" type="string"/>

   <set name="securityGroups" table="SECURITYGROUP" fetch="select" lazy="false">
        <key>
           <column name="ID_AREA"></column>
           <column name="ID_DOC"></column>
           <column name="ID_ROLE"></column>
        </key>
        <many-to-many column="ID_SECURITY" class="Security" fetch="select" lazy="false" />
   </set>
      
   </class>
</hibernate-mapping>


the corresponding java class
Code:
public class Role_Name implements Serializable{
      
    private Area area;
    private Role role;
    private Doc doc;
    private String value_role_name;
    private Set<Security> security = new HashSet<Security>(0);
     
+ Getters/Setters

}


My problem is that the securityGroups Set is not "loaded" (all other properties are OK) when I load Role_Name like that : For example,

Code:
Role_Name roleName = new RoleNameBDR();
Area a = new Area(1); // Contructor with ID
roleName.setArea(a);
      
Doc d = new Doc(1); // Contructor with ID
roleName.setDoc(d);
      
Role r = new Role(4);  // Contructor with ID
roleName.setRole(r);

Criteria criteria = getSession().createCriteria(Role_Name.class).add(
            Expression.eq("area", rn.getArea())).add(
            Expression.eq("doc", rn.getDoc())).add(
            Expression.eq("role", rn.getRole()));
Role_Name result = (Role_Name) criteria.uniqueResult();




What I'm doing wrong ? Is it the good way to that ?
I think that the following is not correct.
Code:
<many-to-many column="ID_SECURITY" class="Security" fetch="select" lazy="false" />

In all examples found on the web, the key name was the same for the column parameter (ie PK of SECURITY table and ID_SECURITY of SECURITYGROUP).
Is there a param to initialize ? I've tried foreign-key, property-ref but no success.

I precise that I cannot change the database schema (tables are already used)...but I made a test : change ID_SECURITY name by ID_SEC in the test database (table SECURITYGROUP) and update the mapping file like that
Code:
<many-to-many column="ID_SEC" class="Security" fetch="select" lazy="false" />

It works !! but in Production it's not possible to do so...

Help would be appreciated
thanks


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

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.