Copy & paste some introductory part from my previous question.
I'm using JPA 2.0 in EclipseLink 2.3.2 in which I have a many-to-many relationship between products and their colours. A product can have many colours and a colour can be associated with many products. This relationship is expressed in the database by three tables.
- product
- prod_colour (join table)
- colour
The prod_colour
table has two reference columns prod_id
and colour_id
from its related parent tables product
and colour
respectively.
As obvious, the entity class Product
has a set of colours - java.util.Set<Colour>
which is named colourSet
.
The entity class Colour
has a set of products - java.util.Set<Product>
which is named productSet
.
To add and delete rows to and from the prod_colour
table, I have defined two methods in the Colour
entity class itself as follows.
public void addToProduct(Product product) {
this.getProductSet().add(product);
product.getColourSet().add(this);
}
public void removeFromProduct(Product product) {
this.getProductSet().remove(product);
product.getColourSet().remove(this);
}
These methods are called from an EJB as follows.
@Override
@SuppressWarnings("unchecked")
public void insert(List<Colour> colours, Product product) {
for (Colour colour : colours) {
colour.addToProduct(product);
}
}
@Override
@SuppressWarnings("unchecked")
public void delete(Colour colour, Product product) {
colour.removeFromProduct(product);
}
I expect them to add and delete rows to and from the prod_colour
table but nothing happens (no error, no exception is caused). The same thing works in Hibernate. So, what else is missing here?
EDIT:
The Product
entity class:
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "prod_id")
private Long prodId;
private static final long serialVersionUID = 1L;
@JoinTable(name = "prod_colour", joinColumns = {
@JoinColumn(name = "prod_id",
referencedColumnName = "prod_id")},
inverseJoinColumns = {
@JoinColumn(name = "colour_id", referencedColumnName = "colour_id")})
@ManyToMany(fetch = FetchType.LAZY)
private Set<Colour> colourSet;
//Getters and Setters.
}
The Colour
entity class.
public class Colour implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "colour_id")
private Long colourId;
private static final long serialVersionUID = 1L;
@ManyToMany(mappedBy = "colourSet", fetch = FetchType.LAZY)
private Set<Product> productSet;
//Setters and getters.
}