-->
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.  [ 15 posts ] 
Author Message
 Post subject: simple composite key question
PostPosted: Mon Mar 24, 2008 3:34 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
i am using tomcat container managed security which requires a user and user_roles table. the user_roles table looks like:


Code:
CREATE TABLE user_roles (
login VARCHAR(15) NOT NULL,
role VARCHAR(10) NOT NULL,
PRIMARY KEY (login, role)
);


how can i create the persistant class for user_roles? specifically i know that every persistant class must have an primary key, correct? so how would i create this kind of primary key in the mapping file and the java code? thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 8:17 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
bump


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 24, 2008 8:40 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
ew0kian wrote:
bump



A composite-id is not good enough for you?



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 12:22 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
Allow me to be more specific. Are the following mappings correct? How would i represent them as a java classes? Thanks.



Code:
<class name="User" table="users">
        <property name="userName" type="string" column="login" length="15"/>
         <property name="password" type="string" column="password" length="15"/>
</class>


<class name="Role" table="user_roles">
        <composite-id name="id">
            <key-property name="userName" type="string" column="login" length="15"/>
            <key-property name="role" type="string" column="role" length="15"/>
        </composite-id>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 12:30 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
also how would i create the one to many relation from users to roles?

<set name="roles">
<key column="_________

what would go there for the key column?

and would i need to specify a <generator class="native" /> inside of the <composite-id> tag?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 12:36 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I assume user name is a primary key for user table then you would have:


Code:
<class name="User" table="users">
        <id name="userName" type="string" column="login" length="15"/>
         <property name="password" type="string" column="password" length="15"/>
</class>


<class name="Role" table="user_roles">
        <composite-id>
            <key-many-to-one name="user" class="User" column="userName"/>
            <key-property name="role" type="string" column="role" length="15"/>
        </composite-id>
</class>



User class is easy. Role class will be:

Code:
class Role implements Serializable {
  User user;
  String role;

//  setters and getters //

  public boolean equals(...) {...}
  public int hashCode(...) {...}
}




Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 12:58 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
thanks. and how would i map the one to many from user to user_roles so i could do something like user.getRoles()?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 25, 2008 9:54 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
ew0kian wrote:
thanks. and how would i map the one to many from user to user_roles so i could do something like user.getRoles()?


You could do:

Code:
<hibernate-mapping>
    <class name="test.model.data.User" table="Users" mutable="true" >

        <id name="userId" type="string" unsaved-value="null">
            <column name="USER_ID"/>
            <generator class="assigned"/>
        </id>

        <set name="roles">
            <key column="USER_ID"/>
            <one-to-many class="test.model.data.Role"   />
        </set>
    </class>
</hibernate-mapping>


in which roles is a collection of Role. However, you can do everything is a simpler way, in which you don't need a Role class and a User class is enough:

Code:
<hibernate-mapping>
    <class name="test.model.data.User" table="Users" mutable="true" >

        <id name="userId" type="string" unsaved-value="null">
            <column name="USER_ID"/>
            <generator class="assigned"/>
        </id>

        <set name="roles" table="ROLES">
            <key column="USER_ID"/>
            <element column="ROLE_ID" type="string" />
        </set>
    </class>
</hibernate-mapping>


in which, roles is a collection of Strings. I do suggest this one if the role class does not have more properties. The Java class for the latter will be:

Code:
package test.model.data;

import java.util.Set;
import java.util.HashSet;

public class User
{
    private String userId;
    private Set<String> roles = new HashSet<String>();

    public String getUserId()
    {
        return userId;
    }

    public void setUserId(String userId)
    {
        this.userId = userId;
    }

    public Set<String> getRoles()
    {
        return roles;
    }

    public void setRoles(Set<String> roles)
    {
        this.roles = roles;
    }
}



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 18, 2008 3:56 am 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
okay the single class method seems a little too confusing to me so i think i will just use the two class method and not worry about doing user.getRoles()... so my question is how do i implement the .equals() and .hashCode() methods?

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 1:11 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Look at here http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#persistent-classes-equalshashcode



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 2:31 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
if i have no intention of trying to use persistant objects from a previously closed session.... then do i need to implement hashcode/equals for the role class using a composite-id?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 21, 2008 8:08 pm 
Beginner
Beginner

Joined: Thu Feb 28, 2008 11:53 am
Posts: 23
No you don't, the implementation of equals() and hashcode() is so Hibernate can determine whether there are dirty objects in the session so it can see which is fresher by comparison.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 15, 2008 9:28 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
in your second set of code you don't use a key-property for the username column for the Role, but instead a key-many-to-one that maps to the userName from the Users table. is this intentional? if so, will schemaexport create the userName column in the Roles table if i do this?


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 18, 2008 4:52 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
can someone plz help?


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 20, 2008 8:34 pm 
Beginner
Beginner

Joined: Wed Mar 19, 2008 12:10 am
Posts: 36
anyone?


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