How to upgrade many to many in sleep mode
I am trying to update 2 tables with many relationships:
I have class 2:
Supplier.java:
@Entity
@Table(name = "Suppliers")
public class Supplier implements Serializable {
@Id
String id;
String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Suppliers_Categories", joinColumns = { @JoinColumn(name = "SupplierId") }, inverseJoinColumns = { @JoinColumn(name = "CategoryId") })
Set<Category> categories = new HashSet<Category>();
@OneToMany(mappedBy = "supplier")
Collection<Product> products;
public Set<Category> getCategories() {
return this.categories;
}
public void setCategories(Set<Category> categories) {
this.categories = categories;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Product> getProducts() {
return products;
}
public void setProducts(Collection<Product> products) {
this.products = products;
}
}
Category.java:
@Entity
@Table(name = "Categories")
public class Category implements Serializable {
@Id
@GeneratedValue
Integer id;
String name;
String namevn;
@ManyToMany(mappedBy = "categories")
Set<Supplier> suppliers = new HashSet<Supplier>(0);
@OneToMany(mappedBy = "category")
Collection<Product> products;
@OneToOne
@JoinColumn(name = "ProductFeature")
Product featureProduct;
public Set<Supplier> getSuppliers() {
return this.suppliers;
}
public Product getFeatureProduct() {
return featureProduct;
}
public void setFeatureProduct(Product featureProduct) {
this.featureProduct = featureProduct;
}
public String getNamevn() {
return namevn;
}
public void setNamevn(String namevn) {
this.namevn = namevn;
}
public void setSuppliers(Set<Supplier> suppliers) {
this.suppliers = suppliers;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Product> getProducts() {
return products;
}
public void setProducts(Collection<Product> products) {
this.products = products;
}
}
My code for updating the relationship ship:
public class CategoryController extends ActionSupport implements
ModelDriven<Category> {
Category model = new Category();
public Category getModel() {
return model;
}
public void setModel(Category model) {
this.model = model;
}
@Action("/admin/category/update")
public String update() {
try{
Supplier s = XHibernate.load(Supplier.class, "1");
if (!model.getSuppliers().contains(s)) {
model.getSuppliers().add(s);
s.getCategories().add(model);
}
Session session = XHibernate.openSession();
Transaction transaction = session.beginTransaction();
session.update(model);
transaction.commit();
}catch(Exception e){
transaction.rollback();
e.printStackTrace();
}
return "news";
}
The problem is my code runs smoothly, no errors, but nothing updated. My database is still the same when I tried to update it. Is there something wrong with my code? Any help would be great
source to share
You specified cascade
in the object Supplier
, so it applies if you save or update Supplier
. This means that you must either put the object cascade
in a Category object or change your logic to keep the provider.
More explanation:
Modify the Category object as follows:
@ManyToMany(mappedBy = "categories", cascade = CascadeType.ALL)
Set<Supplier> suppliers = new HashSet<Supplier>(0);
OR change CategoryController.update()
as shown below:
session.update(s);
source to share
try doing cleanup session before commit transaction, and also close session after commit transaction:
@Action("/admin/category/update")
public String update() {
Supplier s = XHibernate.load(Supplier.class, "1");
if (!model.getSuppliers().contains(s)) {
model.getSuppliers().add(s);
s.getCategories().add(model);
}
}
Session session = XHibernate.openSession();
Transaction transaction = session.beginTransaction();
session.update(model);
session.flush();
transaction.commit();
session.close();
}
source to share
I solved my problems by changing the following configuration to the category class:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Suppliers_Categories", joinColumns = { @JoinColumn(name = "CategoryId") }, inverseJoinColumns = { @JoinColumn(name = "SupplierId") })
And also change the provider class:
@ManyToMany(mappedBy = "suppliers")
Now it worked. The problem is I am calling the update from a category, so I need to specify the config in the category class.
source to share