-->
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.  [ 8 posts ] 
Author Message
 Post subject: ManyToMany
PostPosted: Thu Jul 01, 2010 10:46 am 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
What is the best way to describe a ManyToMany relationship in hibernate.
i have User and Document objects.
depending on what access a user has, the user can access various different documents.
and a document can be accessed by various different users.

so should I create a third object UserDocument Object OR use @JoinTable for the Many to Many Relationship.
In either case how will Hibernate behave in terms of returning the objects.


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Sun Jul 04, 2010 2:11 am 
Newbie

Joined: Tue Jun 29, 2010 2:14 am
Posts: 8
Hibernate will create a MTM table for you.
You just need to specify the @JoinTable annotation where you can set the MTM table name
Code:
@ManyToMany
@JoinTable(name="mtm_table_name", joinColumns={ @JoinColumn(name = "table1_id") }, inverseJoinColumns = { @JoinColumn(name = "table2_id") })
private List<Items> items;


In case you do not specify the name the default will be used


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Sun Jul 04, 2010 3:59 am 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
vipavel wrote:
Hibernate will create a MTM table for you.
You just need to specify the @JoinTable annotation where you can set the MTM table name
Code:
@ManyToMany
@JoinTable(name="mtm_table_name", joinColumns={ @JoinColumn(name = "table1_id") }, inverseJoinColumns = { @JoinColumn(name = "table2_id") })
private List<Items> items;


In case you do not specify the name the default will be used


Thanks for the answer. So is @JoinTablethe best way to describe a MTM?
When Hibernate returns the objects how will the MTM be represented and how would I access the values?


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Tue Jul 06, 2010 2:35 am 
Newbie

Joined: Tue Jun 29, 2010 2:14 am
Posts: 8
You don't have to carry about MTM object. Hibernate does it.
Just make your query for the given object - myEntity.getItems() and you'll get your list


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Tue Jul 06, 2010 6:53 am 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
vipavel wrote:
You don't have to carry about MTM object. Hibernate does it.
Just make your query for the given object - myEntity.getItems() and you'll get your list


Cool. Thanks. Will this still work if I use annotation based mapping and SQL scripts to create my Database.
What changes should I make to the tables participating in the join?

eg:
User and Document Tables
User ( id, username )
Document (id, documentName)

How will Hibernate create the Join Table? Will it create the Join Table on every deploy?
I wouldn't want hibernate to create a Join Table (through Hibernate). Instead would prefer to create the Join table using SQL script.


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Wed Jul 07, 2010 3:33 pm 
Newbie

Joined: Tue Jun 29, 2010 2:14 am
Posts: 8
As I can understand you need it to put some additional info.
This you can achieve by creating a third object that will represent your MTM table.
So it will look like this:
user --> users_documents <-- document
That means create OTM relations from user and document to users_documents


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Thu Jul 08, 2010 3:24 pm 
Newbie

Joined: Tue Jun 29, 2010 11:50 am
Posts: 10
vipavel wrote:
As I can understand you need it to put some additional info.
This you can achieve by creating a third object that will represent your MTM table.
So it will look like this:
user --> users_documents <-- document
That means create OTM relations from user and document to users_documents



Looks a bit complicated.
How should I change the @JoinTable annotation to use the Third Object?


Top
 Profile  
 
 Post subject: Re: ManyToMany
PostPosted: Sat Jul 10, 2010 11:03 am 
Newbie

Joined: Tue Jun 29, 2010 2:14 am
Posts: 8
@JoinTable create a third table, but it tells hibernate to manage it (using primary keys)
Anyway, which data (columns) do you want to hold in your users_documents table?


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