Reputation: 53
I'm trying to update 2 tables with many-to-many relationship:
I have 2 class:
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 update the relation ship is:
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 run smoothly, no errors but nothing was updated. My database is still the same when i tried to update. Is there anything wrong with my code ? Any help would be great
Upvotes: 5
Views: 8396
Reputation: 53
I solved my problems by change the following config to category class:
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "Suppliers_Categories", joinColumns = { @JoinColumn(name = "CategoryId") }, inverseJoinColumns = { @JoinColumn(name = "SupplierId") })
And also change in Supplier class:
@ManyToMany(mappedBy = "suppliers")
Now It worked.The problem is because I call the update from category so I need to specific the config in category class.
Upvotes: 0
Reputation: 5439
You have specified cascade
in Supplier
object, so it is applied if you save or update a Supplier
. It means, you should either put the cascade
in Category object, or change your logic somehow to save the supplier.
More explanation:
Modify the Category object as below:
@ManyToMany(mappedBy = "categories", cascade = CascadeType.ALL)
Set<Supplier> suppliers = new HashSet<Supplier>(0);
OR modify CategoryController.update()
as below:
session.update(s);
Upvotes: 2
Reputation: 7890
try with flushing 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();
}
Upvotes: 0