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
|