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.  [ 2 posts ] 
Author Message
 Post subject: How to map a manytomany relation with a key in the relation?
PostPosted: Tue Feb 20, 2007 6:42 am 
Newbie

Joined: Tue Feb 20, 2007 6:31 am
Posts: 2
Hello I'm trying to map a many to many relationship with a third key in the relation.
I'm doing the tipical example of the VideoClub so i have a class User and a class Film and I want to map the relation rented that have as primary key the user_id,film_id and a date
Do I have to make a third class to represent it? Or I can represent it with the classes I allready have?.
Thank u.
Birras


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 22, 2007 6:04 am 
Newbie

Joined: Tue Feb 20, 2007 6:31 am
Posts: 2
Finally I have mapped it with a intermediate class like this:

The class User:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="data.User" table="USERS">
<id name="userId" type="long">
<generator class="increment"/>
</id>

<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>

<set name="alquiladas" table="ALQUILER" inverse="true">
<key column="userUID"/>
<one-to-many class="data.Alquiler"/>
</set>
</class>
</hibernate-mapping>

The class Film:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="data.Pelis" table="PELIS">
<id name='peliId" type="long">
<generator class="increment"/>
</id>

<property name="descripcion"/>

<set name="alquiler" table="ALQUILER" inverse="true">
<key column="pistaUID"/>
<one-to-many class="data.Alquiler"/>
</set>
</class>
</hibernate-mapping>

And the intermediate class:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="data.Alquiler" table="ALQUILER">
<composite-id>
<key-property name="userId" column="userUID" />
<key-property name="peliId" column="peliUID"/>
<key-property name="fecha"/>
</composite-id>
<many-to-one name="userId" class="data.User" update="false" insert="false" fetch="select" cascade="persist,save-update">
<column name="userUID" not-null="true" />
</many-to-one>
<many-to-one name="pistaId" class="data.Pistas" update="false" insert="false" fetch="select" cascade="persist,save-update">
<column name="peliUID" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

But now the problem is that I don't know how to insert Alquileres in the database


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