如何在 Hibernate 中更新“多对多”

anh*_*yen 5 java many-to-many hibernate

我正在尝试使用多对多关系更新 2 个表:

我有2个班级:

供应商.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;
}
}
Run Code Online (Sandbox Code Playgroud)

类别.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;
}
}
Run Code Online (Sandbox Code Playgroud)

我更新关系的代码是:

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";
}
Run Code Online (Sandbox Code Playgroud)

问题是我的代码运行顺利,没有错误,但没有更新。当我尝试更新时,我的数据库仍然相同。我的代码有什么问题吗?任何帮助都会很棒

Nul*_*ull 0

尝试在提交事务之前刷新会话,并在提交事务之后关闭会话:

@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();
}
Run Code Online (Sandbox Code Playgroud)