Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update "many to many" in Hibernate

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

like image 587
anh nguyen Avatar asked Nov 01 '22 13:11

anh nguyen


1 Answers

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);
like image 60
Ean V Avatar answered Nov 09 '22 10:11

Ean V