-->
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.  [ 4 posts ] 
Author Message
 Post subject: How to insert additional column to Many-2-Many mapping table
PostPosted: Thu Mar 30, 2006 11:05 am 
Newbie

Joined: Tue Jan 24, 2006 12:04 pm
Posts: 12
suppose i have two tables: User and Role which have many-2-many relationships.

the third mapping table is called REL_USER_ROLE

in user.hbml.xml

<set name="roles"
table="REL_USER_ROLE"
cascade="save-update"
lazy="false"
sort="unsorted">

<key column="USERID"/>
<many-to-many class="package.Role"
column="ROLEID"
not-found="ignore">
</many-to-many>
</set>

The problem is that i need insert some additional information to the relation table (rel_user_role), e.g. add one column "status" where value is fixed (say it is "undeleted" for User).

how can i map the third column in hbm?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 30, 2006 6:23 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
search for <composite-element> in the reference.pdf

http://www.hibernate.org/hib_docs/v3/reference/en/pdf/hibernate_reference.pdf

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


Top
 Profile  
 
 Post subject: Use 2 One-to-Many relationships
PostPosted: Thu Mar 30, 2006 10:47 pm 
Newbie

Joined: Wed Mar 29, 2006 7:48 pm
Posts: 3
See the Caveat Emptor example (originally written for the book Hibernate In Action) mapping CategorizedItem for how to do this.

You can download the example files from http://caveatemptor.hibernate.org/2.html

You can buy the ebook from there too.


The mapping file used in the example is pasted below.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.ce.auction.model">
<!--

Mapping file for the CategorizedItem class of CaveatEmptor.

This is really a very special mapping. The CategorizedItem class
represents an association table. The ER model for this is really
a many-to-many association, but instead of two entities and two
collections, we mapped this as two one-to-many associations between
three Java classes. One of the motivation for this are the additional
attributes on the association table (not only two FKs): username
and creation date.

This class is the entity in the middle, between Category and Item.
You can see that it has references to both. The trick is the usage
of update="false" insert="false" on the <many-to-one> mapping
elements. The foreign/primary key columns of the association table
is therefore managed by the <key-property> mappings in the composite
key.

Note that the composite key is encapsulated in an inner class, which
simplifies the implementation of equals/hashCode. We recommend to
always use a separate composite key class.

@author Christian Bauer <christian@hibernate.org>

-->
<class name="CategorizedItem" table="CATEGORIZED_ITEM" mutable="false">
<composite-id name="id" class="CategorizedItem$Id" access="field">
<key-property name="categoryId" access="field" column="CATEGORY_ID"/>
<key-property name="itemId" access="field" column="ITEM_ID"/>
</composite-id>
<property name="dateAdded" column="ADDED_ON" type="timestamp" not-null="true" access="field"/>
<property name="username" column="ADDED_BY_USER" type="string" not-null="true" access="field"/>
<many-to-one name="category" column="CATEGORY_ID" not-null="true" access="field" insert="false" update="false"/>
<many-to-one name="item" column="ITEM_ID" not-null="true" access="field" insert="false" update="false"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 31, 2006 4:04 am 
Newbie

Joined: Tue Jan 24, 2006 12:04 pm
Posts: 12
thanx for ur reply.

Composite ID, that is it.


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