-->
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: Problem with <many-to-many> having additional attribut
PostPosted: Tue Oct 24, 2006 1:42 pm 
Newbie

Joined: Tue Oct 24, 2006 1:21 pm
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0

Problem:
I have a many to many relationship between USER and ROLE. Please find below the mapping files for USER and ROLE.
I need to add additional columns(GROUP, APP_ID) to the association table USER_ROLE .
Is it possible to add above fields (GROUP, APP_ID) to the SET or BAG which has many-to-many?
If it is can any one provide an example of mapping and POJO.

Following the database tables:



USER
_____________

*UID
NAME
DOB
_____________
______________

USER_ROLE
__________________

*USER_UID
*ROLE_RID
__________________
GROUP
APP_ID
__________________

___________

ROLE
_____________


*RID
NAME
DESC

_____________



Following mappings and client works well without new fields (GROUP, APP_ID)..


user.hbm.xml
============
<hibernate-mapping>
.......

<set name="roleList" table="USER_ROLE">
<key column="UID"/>
<many-to-many column="RID" class="Role"/>
</set>

</hibernate-mapping>

User.java
=========

public class User {
..
...
...
private Set roleList = new HashSet();

public Set getRoleList() {
return roleList;
}

public void setRoleList(Set roleList) {
roleList = roleList;
}

.....
.....
}


TestClient.java
===============
public class TestClient {
........
........
User user = (User) session.load(User.class, userId);
Role role = (Role) session.load(Role.class, roleId);

user.getRoleList().add(role);

.........
.......

}

Name and version of the database you are using: Oracle 9i








Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html[/img]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 2:10 pm 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
You have two options:

1) An intermediate Entity Class for the join table with one-to-many mappings


2) Using a collection of components which is described here:
http://www.hibernate.org/hib_docs/v3/re ... ollections
You should be aware that bidirectional navigation is NOT supported with this approach.
This mapping is copy pasted from the Hibernate documentation, I have just marked the interesting part of this kind of mapping.

<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.Purchase">
<property name="purchaseDate"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one name="item" class="eg.Item"/>
</composite-element>
</set>
</class>

_________________
Please don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 2:50 pm 
Newbie

Joined: Tue Oct 24, 2006 1:21 pm
Posts: 2
Thank you for the reply.

Actually I tried this even before posting the message.

Here is the mapping I had:

<bag name="roleList" table="USER_ROLE" cascade="save-update" lazy="false">
<key column="UID" />

<composite-element class="UserRole">
<property name="group" column="GROUP" type="string"/>
<property name="appId" column="APP_ID" type="integer"/>

<many-to-one name="role" class="Role"/>
</composite-element>

</bag>

This produces following error:
Could not find a getter for role in class UserRole.java

I guess I need help in writing the UserRole.java. Also, I am worried about how Hibernate saves the data, I mean the order of ID creation and usage.
Can you provide a sample code segment.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 24, 2006 3:42 pm 
Expert
Expert

Joined: Tue Dec 07, 2004 6:57 am
Posts: 285
Location: Nürnberg, Germany
Quote:
I guess I need help in writing the UserRole.java.

This is straight forward, it is a usual POJO with getters and setters for it's properties.

_________________
Please don't forget to rate


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.