-->
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: Collection mapping question
PostPosted: Wed Jul 27, 2005 5:15 pm 
Newbie

Joined: Tue May 03, 2005 1:53 am
Posts: 7
I have a question about collection mapping. Here is my senerio:

I have two tables: User and UserRole. I want to use the same two tables for user autherization and authentication in multiple applications. Therefore, the table structures are:

Code:
create table User (
  user_id long,
  username varchar(20),
  password varchar(50),
  primary key (user_id),
);

create table UserRole (
  user_id long,
  app_name varchar(20),
  role_name varchar(20),
  primary key (user_id, app_name, role_name),
);


My User object looks like this:

Code:
class User {
  private long user_id;
  private String username;
  private String password;
  private Set roles;  // set of role names (string)
}


Each application will have a defined constant APPLICATION_NAME. My question is how to map the collection roles to the UserRole table so that it will use APPLICATION_NAME for the app_name field in every CRUD action.

Any help is appreciated. Thanks!

Kenny


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 27, 2005 8:41 pm 
Newbie

Joined: Tue May 03, 2005 1:53 am
Posts: 7
After some research, I figured out one solution. Here is my mapping file:

Code:
<hibernate-mapping auto-import="true">
   <class name="User" table="User" dynamic-update="true">
      <id name="id" column="user_id" type="long" unsaved-value="0">
         <generator class="increment"/>
      </id>
      <version name="version" column="version" type="integer" unsaved-value="negative"/>
      <property name="username" column="username" unique="true"/>
      <property name="password" column="password"/>
       <set name="roles" table="UserRole" [color=red]where="app_name='APP1'"[/color] cascade="delete" lazy="false">
         <key column="user_id" not-null="true"/>
         <element column="role_name" type="string"/>
          [color=red]<sql-insert>insert into UserRole (user_id, app_name, role_name) values (?, 'APP1', ?)</sql-insert>[/color]
          [color=red]<sql-delete>delete from UserRole where user_id = ? and app_name = 'APP1' and role_name = ?</sql-delete>[/color]
          [color=red]<sql-delete-all>delete from TestUserRole where user_id = ? and app_name = 'TESTPLAN'</sql-delete-all>[/color]
       </set>         
   </class>
</hibernate-mapping>


The sections highlighted in red would take care the select, insert and delete. Since all three columns on UserRole table are primary keys, there is no need to include <sql-update> tag, isn't it?

I only tested the insert and select. They seem to work fine. But is there any other way to make this work?


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.