I'm trying to get both sides of the relationship updated before the commit. Reading the manual I discovered that Transitive persistence does that for me and all I had to do is put the annotation of cascade. But it didn't work.
I have a Basket and Product which I add to the Basket. When I add it to the Basket I need the list of Baskets from product to be updated too. The third code is the an example of what I'm trying to do. The "numberOfBaskets()" should return 1, but it is returning 0.
Is there any other annotations that I should put ? I tried to put cascade at Product too but it didn't work as well...
thx
Code:
@Entity
@Table(name = "PRODUCT")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Product implements java.io.Serializable
{
@Id
@Column(name = "CD_PRODUCT")
@Length(max=10)
@NotNull
private String m_Code;
@Column(name = "CD_UNIT")
@Length(max=3)
private String m_UnitCode;
@Column(name= "NM_PRODUCT")
@Length(max=50)
private String m_Name;
@ManyToMany(mappedBy = "m_Products")
private List<Basket> m_Baskets;
}
Code:
@Entity
@Table(name = "BASKET")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Basket implements java.io.Serializable
{
@Column(name = "QT_MAX")
private int m_MaxQty;
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable(name="BASKET_PRODUCT",
joinColumns={@JoinColumn(name="CD_BASKET", nullable=false)},
inverseJoinColumns={@JoinColumn(name="CD_PRODUCT", nullable=false)})
private List<Product> m_Products;
}
Code:
HibernateUtil.GetSession().beginTransaction();
Product v_Product = Product.Get(p_ProductCode);
Basket v_Basket = Basket.Get(p_BasketCode);
v_Basket.add(v_Product);
System.out.println("Baskets: " + v_Product.numberOfBaskets());
HibernateUtil.GetSession().getTransaction().commit();
System.out.println("Baskets: " + v_Product.numberOfBaskets());