-->
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.  [ 3 posts ] 
Author Message
 Post subject: "Two classes with an association between them" map
PostPosted: Tue Jan 24, 2006 4:37 pm 
Newbie

Joined: Tue Jan 24, 2006 4:32 pm
Posts: 2
Hibernate version: 2.1.7

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

Code:
Users
(id INT PRIMARY KEY,
login VARCHAR(10)
)


Dvd
(id INT PRIMARY KEY,
title VARCHAR(255)
);


TakenItems
(id_user INT,
id_dvd INT,
FOREIGN KEY (id_user) REFERENCES users (id),
FOREIGN KEY (id_dvd) REFERENCES Dvd (id)
);


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:
Code:
SELECT id_dvd FROM TakenItems WHERE id_users = <id user>



2) Which DVDs are vacant (not taken by other users).
SQL query would be:
Code:
SELECT a.* from DVD a LEFT JOIN TakenItem b on a.id = b.id_dvd WHERE b.id_dvd IS NULL
or
SELECT id FROM dvd WHERE id NOT IN (SELECT id_dvd FROM TakenItem)


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).


Mapping documents:

Code:
   <class name="ru.ptashko.grigory.hibernate.entities.User" table="users">
      <id name="id" type="string" unsaved-value="null">
         <column name="id" sql-type="char(32)" not-null="true"/>
         <generator class="uuid.hex"/>
      </id>

      <property name="login">
         <column name="login" length="10" not-null="true"/>
      </property>

      <property name="password">
         <column name="password" length="10" not-null="true"/>
      </property>
   </class>

   <class name="ru.ptashko.grigory.hibernate.entities.DVD" table="DVD">
      <id name="id" type="string" unsaved-value="null">
         <column name="id" sql-type="char(32)" not-null="true"/>
         <generator class="uuid.hex"/>
      </id>

      <property name="name" >
         <column name="name" sql-type="varchar(255)" not-null="true"/>
      </property>
   </class>


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.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:03 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Have a look at section "Bidirectional associations with join tables" in the Hib Ref Docs. That shows you how to map your tables, assuming that the join table (TakenItems) doesn't contain any extra data (e.g. rental date or deposit left, etc.). If you need extra columns in that table, check out section "Collections of dependent objects".

You need to add sets to each of your classes, something like this:
In User:
Code:
    <set name="DVDs">
        <key column="id"/>
        <many-to-many column="id"  class="DVD"/>
    </set>


In DVD:
Code:
    <set name="Users" inverse="true">
        <key column="id"/>
        <many-to-many column="id"  class="User"/>
    </set>


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 24, 2006 5:07 pm 
Newbie

Joined: Tue Jan 24, 2006 4:32 pm
Posts: 2
tenwit,

thanks! I am not going to add any extra fields to the third table. Thanks for the tip, I'm off to read it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.