-->
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: @MappedBy & @ManyToMany
PostPosted: Fri Jun 11, 2010 11:48 pm 
Beginner
Beginner

Joined: Sat Sep 12, 2009 10:20 am
Posts: 44
It it possible to use @MappedBy with @ManyToMany?


I.E.

Person
@NotNull
@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy=authors)
private List<Person> authors = new ArrayList<Articles>();


Article
@NotNull
@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY)
@Cascade( { org.hibernate.annotations.CascadeType.DETACH, org.hibernate.annotations.CascadeType.LOCK, org.hibernate.annotations.CascadeType.REFRESH, org.hibernate.annotations.CascadeType.REPLICATE })
private List<Person> authors = new ArrayList<Person>();


Top
 Profile  
 
 Post subject: Re: @MappedBy & @ManyToMany
PostPosted: Sat Jun 12, 2010 3:18 am 
Newbie

Joined: Fri Jun 04, 2010 8:57 pm
Posts: 19
Hi,
Its possible. Here is what I have used. For my example, the two entities are Category and Item. A Category may contain many Items and similarly an Item may belong to multiple Categories. Here is the code that I have used and is working.

In Category.java
Code:
    @ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE})
    private Set<Item> items = new HashSet<Item> ();


In Item.java
Code:
    @ManyToMany(mappedBy="items")
    private Set<Category> categories = new HashSet<Category>();


In this case an intermediate table is constructed by Hibernate though. If you want to explicitly control the intermediate table attributes, you can use @JoinTable. In either case, you need to map only the one side of Collection. The other can be specified by "mappedBy". Hope this helps.

regards,
Nirvan.


Top
 Profile  
 
 Post subject: Re: @MappedBy & @ManyToMany
PostPosted: Sat Jun 12, 2010 1:04 pm 
Beginner
Beginner

Joined: Sat Sep 12, 2009 10:20 am
Posts: 44
I need to do some more testing but from what I can tell this isn't working right.

Hibernate created an "id field" for both sides of the relationship, which seems to be that it wouldn't work as a mapped by.


Top
 Profile  
 
 Post subject: Re: @MappedBy & @ManyToMany
PostPosted: Sun Jun 13, 2010 5:02 am 
Beginner
Beginner

Joined: Sat Sep 12, 2009 10:20 am
Posts: 44
I got this to work, but in order to do so I had to explicitly define the join table.

Otherwise it was looking for an articles_id column when loading Person and a authors_id column when loading Article.
After explicitly defining the join table & columns it is working just fine.

I'm using 3.5.1-FINAL



Person
Code:
@NotNull
@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY, mappedBy=authors)
private List<Articles> articles = new ArrayList<Articles>();


Article
Code:
@NotNull
@JoinTable(name="Article_Person",
        joinColumns=
            @JoinColumn(name="Article_id", referencedColumnName="id"),
        inverseJoinColumns=
            @JoinColumn(name="authors_id", referencedColumnName="id")
        )   
@ManyToMany(cascade = { CascadeType.REFRESH }, fetch = FetchType.LAZY)
@Cascade( { org.hibernate.annotations.CascadeType.DETACH, org.hibernate.annotations.CascadeType.LOCK, org.hibernate.annotations.CascadeType.REFRESH, org.hibernate.annotations.CascadeType.REPLICATE })
private List<Person> authors = new ArrayList<Person>();


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.