This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes SecondaryTable Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "SecondaryTable Mapping" Watch "SecondaryTable Mapping" New topic

SecondaryTable Mapping

Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Hello Everybody,

I am trying to map following tables to a single class User. Is my mapping alright? Nowhere I specify that user_roles has a composite primary key. Will this work?

create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null

create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key (user_name, role_name)


James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
It will work if the user_name is unique in the role table.

If a user has multiple roles as the schema allows, then it may work for some situations, but is not a very good idea. A OneToMany to Role would be better.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
Thank you so much. I understand better now. Secondary table mapping may be used for table if they have same primary key.
I agree. Here's the link:
subject: SecondaryTable Mapping
Similar Threads
one-to-one mapping.
Trying to move away from MemoryRealm to using JDBCRealm.
form based login example
MySQL: how to enforce the NOT NULL constraint