Solved

Using Hibernate with a link table

Posted on 2004-09-29
2
745 Views
Last Modified: 2008-01-09
Hi.
I have a pretty complex problem and i'm not sure how to map it.

I have a standard tomcat User & UserRoles tables setup.
I also have what's going to be an event/class scheduler with the tables Event & EventInstructor

the schema looks like this:

 ----------       -------------------
|   User   |     |   User_Role    |
 ----------       -------------------
| ID (int) |     | ID (int)           |
| Name   |     | user_id (int)   |
| ...        |     | role (varchar) |
 ----------       -------------------
 

 ----------       -----------------------
|   Event  |     |  Event_Instructor |
 ----------       -----------------------
| ID (int) |     | instructor_id (int) |
| Name   |     | event_id (int)       |
| ...        |      -----------------------
 ----------


Problem #1:
How do i map the Event & User using the Event_Instrucotr table?
I've looked at some posts about many-to-many, but my problem is that and event may or may not have an instructor, but if it does it can only have 1.

Problem #2:
An instructor is a User who has the role "instructor" in the User_Role table.
Not sure how to get only these Users to map to the Event_Instructor table.

Again, and event may or may not have up to 1 instructor, but and instructor can have many events.

Can anyone help me map this?

Thanks!

- Jonathan
0
Comment
Question by:jdoklovic
2 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12199371
Problem #1:
How do i map the Event & User using the Event_Instrucotr table?
I've looked at some posts about many-to-many, but my problem is that and event may or may not have an instructor, but if it does it can only have 1.

I assume that INSTRUCTORID should have the value of USER id, which shall be implemented by defining INSTRUCTORID being a foreign key to userid (in table user). The value can be null, which indicates no instructor.

Problem #2:
An instructor is a User who has the role "instructor" in the User_Role table.
Not sure how to get only these Users to map to the Event_Instructor table.

IN RDBMS like SQL Server and Oracle, I would use contraints or triggers to validate this requirements. In MySQL, there are no triggers unless you use MaxDB by MySQL...

So unless you have a DBMS Version that allows triggers, you need to solve this on application level.

CHeers
0
 
LVL 7

Accepted Solution

by:
petoskey-001 earned 500 total points
ID: 12199891
----------              -------------------
|   User   |           |   User_Role    |
 ----------             -------------------
| ID (int) | <---    | ID (int)           |
| Name   |       |--| user_id (int)   |
| ...        |           | role (varchar) |
 ----------             -------------------
 

 ----------       -----------------------
|   Event  |     |  Event_Instructor |
 ----------       -----------------------
| ID (int) |     | instructor_id (int) |
| Name   |     | event_id (int)       |
| ...        |      -----------------------



Problem #1:
How do i map the Event & User using the Event_Instrucotr table?
I've looked at some posts about many-to-many, but my problem is that and event may or may not have an instructor, but if it does it can only have 1.

Problem #2:
An instructor is a User who has the role "instructor" in the User_Role table.
Not sure how to get only these Users to map to the Event_Instructor table.

Again, and event may or may not have up to 1 instructor, but and instructor can have many events.
-----------------------------------------------------------------
As stated you can't enforce specific links based on data without triggers to check things.  You can't force Event_Instroctor to link to User_Role with a value of Instructor.  You'll have to put that logic in your middleware or application.

BUT it appears to me that you don't really need the Event_Instructor table.  Each event would point to the User_Role table for their instructor.  Again you would have to check that they were an instructor if code elsewhere, but you don't really need a separate table.  Just put any extra fields from the event_instructor table into the event.

So the linking would look like this...  

  USERS <--- USER_ROLES <--- EVENT

That sounds like it would work based on what you've mentioned so far.  
 
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysqli Query 5 50
selecting date modified field from a table 2 44
Mysql sync between 3-4 mysql db 4 31
Mysql Left Join Case 10 47
As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question