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.