Hi,
I am trying to use 'Form based Authentication' with User and Role tables created with Hibernate3. In my User and Role tables I want to use a unique DB generated id as the primary key. However, to get 'Form based Authentication' to work I need to define the relationship between users and roles using user-name and role-name fields instead of primary keys (see user_roles table for details).
I am stuck trying to
generate foreign key constraints on the user and role names in the user_roles table. Either I get no foreign key constraints or the constraints end up with primary key (id) of user and role tables. What kind of SET mapping should I have inside the user mapping to generate the foreign key constraints on user_name and role_name? Is there a better way to accomplish this in hibernate mappings?
Thanks in Advance,
Baldeep
Code:
//--------------------DESIRED SQL TABLE STRUCTURE--------------------
TABLE users (
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
...
CONSTRAINT pk_users PRIMARY KEY (id)
);
TABLE roles {
id INTEGER NOT NULL,
name VARCHAR NOT NULL,
...
CONSTRAINT pk_roles PRIMARY KEY (id)
);
TABLE user_roles {
user_name VARCHAR NOT NULL,
role_name VARCHAR NOT NULL,
CONSTRAINT fk1_user_roles FOREIGN KEY (user_name) REFERENCES users(name),
CONSTRAINT fk2_user_roles FOREIGN KEY (role_name) REFERENCES roles(name)
);
//--------------------CURRENT HIBERNATE MAPPINGS--------------------
<class name="org.abc.User" table="users">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name" not-null="true" unique="true"/>
...
<set name="roles" table="roles">
<key column="name" property-ref="name"/>
<many-to-many column="role_name" class="org.abc.Role"/>
</set>
</class>
<class name="org.abc.Role" table="roles">
<id name="id" unsaved-value="-1">
<generator class="native"/>
</id>
<property name="name" not-null="true" unique="true"/>
...
</class>