-->
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.  [ 3 posts ] 
Author Message
 Post subject: Hibernate - Annotations - many to many association
PostPosted: Tue Apr 22, 2008 8:50 pm 
Newbie

Joined: Sun Mar 23, 2008 5:03 am
Posts: 3
Hello,
I am using Spring,Hibernate Annotations based approach.
I have two entities. Product and Retailer which have a many to many association.
A product can belong to MANY retailers
A retailer can have MANY products

So I have a join table retailer_product which has the following columns
productId - referring to Product Table (id)
retailerId - referring to Retailer Table (id)
price

So how do I map these associations. This is what I have so far that doesnt work.
Product.java
@Entity
@Table(name = "product")
public class Product {
@OneToMany(mappedBy = "product")
private Set<ProductRetailers> productRetailers = new HashSet<ProductRetailers>(0);
}
********************************************************************************
Retailer.java
@Entity
@Table(name = "retailer")
public class Retailer {
@OneToMany(mappedBy = "retailer")
private Set<ProductRetailers> productRetailers = new HashSet<ProductRetailers>(0);
}
*******************************************************************************
@Entity
@Table(name = "retailer_product")
public class RetailerProduct {
@ManyToOne
@JoinColumn(name = "retailerId", referencedColumnName = "id")
private Retailer retailer;

@ManyToOne
@JoinColumn(name = "productId", referencedColumnName = "id")
private Product product;
}
******************************************************************************
Obviously there is some problem here and it doesnt work.
What I want to be able to do is retrieve all retailers that sell a particular product
and find all products that are sold by a particular retailer.

Can anyone help me with this. Will really appreciate it.
Thanks a lot in advance


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 3:11 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Generally speaking, in hibernate all bi-directional relationships have a owning entity;
the "mappedBy" is to be used to mark the inverted property (the other endpoint of the relationship started by the owning entity).
So you never want to put "mappedBy" on both sides of the relation, but it's critical you define one.

In this case, use @ManyToMany ;-)

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 4:38 am 
Newbie

Joined: Sun Mar 23, 2008 5:03 am
Posts: 3
Thanks Sanne for the reply.

I had read on Hibernate site that we should always model a manyToMany relation by creating an association table and doing two OneToMany associations.

So your telling me to use ManyToMany. Can you explain in a bit more detail. Do I need to to have the RetailerProduct.java class. One thing to note is that my association table retailer_product has extra column of price.

Thanks


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