Advertisement

04.22.2008 at 04:50PM PDT, ID: 23345031
[x]
Attachment Details

Hibernate - Annotations - many to  many association

Asked by gublooo in Java Programming Language, J2EE Frameworks, WebApplications

Tags: java,hibernate,spring

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 advanceStart Free Trial
[+][-]04.22.2008 at 07:35PM PDT, ID: 21417380

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.23.2008 at 01:44AM PDT, ID: 21418614

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.23.2008 at 01:59AM PDT, ID: 21418675

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.23.2008 at 03:10AM PDT, ID: 21418991

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.23.2008 at 05:15AM PDT, ID: 21419744

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.23.2008 at 05:20AM PDT, ID: 21419776

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Programming Language, J2EE Frameworks, WebApplications
Tags: java,hibernate,spring
Sign Up Now!
Solution Provided By: zbyszek_BTG
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.23.2008 at 03:19PM PDT, ID: 21426048

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628