aspose file tools*
The moose likes Object Relational Mapping and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark ""Two classes with an association between them" mapping? Need help.." Watch ""Two classes with an association between them" mapping? Need help.." New topic
Author

"Two classes with an association between them" mapping? Need help..

Grigory BLAH. Ptashko
Greenhorn

Joined: Jan 24, 2006
Posts: 2
Hello.

I'm new to Hibernate and all that Object/Relational mapping stuff. I need a recipe in Hibernate of the simplest relational situation.
Let me explain.

I have three tables



As you see the first two are simple entities and the third one shows which DVD a specific user has taken.
In my application I have to be able to know these things:


1) What user has which DVDs.
SQL query would be:



2) Which DVDs are vacant (not taken by other users).
SQL query would be:


Users also have to be able to take and give back DVDs. It means to insert into/delete from the TakenItem table.

So, I made up a hibernate-mapping for two classes Users and DVD in a standard and simple with one id and some fields (as any of the tutorials say).
But the problem is that I don't understand what to do with the third table and what hibernate-mapping must I write to be able to make queries that I described before?

I appreciate any advices.

Thank you.
Chris Richardson
author
Ranch Hand

Joined: Jan 10, 2006
Posts: 50
Grigory,

I think you want a (possibly bidirectional) many-to-many relationship between users and dvds. See http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-join-m2m for an example.

In your particular example TakenItems is the join table (PersonAddress in the Hibernate example)

Chris


Enterprise Java consulting and training - <a href="http://www.chrisrichardson.net" target="_blank" rel="nofollow">http://www.chrisrichardson.net</a> Author, POJOs in Action - <a href="http://www.manning.com/crichardson" target="_blank" rel="nofollow">http://www.manning.com/crichardson</a> Enterprise POJOs blog - <a href="http://chris-richardson.blog-city.com" target="_blank" rel="nofollow">http://chris-richardson.blog-city.com</a>
Grigory BLAH. Ptashko
Greenhorn

Joined: Jan 24, 2006
Posts: 2
Thanks!
That is exactly what I need!
 
jQuery in Action, 2nd edition
 
subject: "Two classes with an association between them" mapping? Need help..