2014-12-13 71 views
3

我試圖更新2代表與許多一對多的關係:如何更新「多對多」在Hibernate中

我有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; 
} 
} 

我對更新代碼的關係船舶:

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"; 
} 

問題是我的代碼運行順利,沒有錯誤,但沒有更新。當我嘗試更新時,我的數據庫仍然是相同的。我的代碼有什麼問題嗎?如果您保存或更新Supplier

@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(); 
} 

回答

0

嘗試用沖洗會議之前提交事務,並關閉會話後提交事務。這意味着,您應該將cascade置於Category對象中,或者以某種方式更改邏輯以保存供應商。

更多的解釋:

修改類別對象如下:

@ManyToMany(mappedBy = "categories", cascade = CascadeType.ALL) 
Set<Supplier> suppliers = new HashSet<Supplier>(0); 

如下修改CategoryController.update()

session.update(s); 
+0

刷新應該是在更新後,如果它有所作爲。 – 2014-12-13 12:51:37

+0

我試過了。但它仍然是一樣的。 – 2014-12-13 12:52:38

+0

@Ean,是的,我糾正它,這只是一個錯字。謝謝 – jfun 2014-12-13 12:53:27

1

您已經Supplier對象指定cascade,所以它被應用:任何幫助將是巨大的

+0

對不起,我沒有得到你的答案。我是一個擁有hibernate的新手。你能給我一個例子或更好地糾正我的代碼? – 2014-12-13 13:02:56

+0

@anhnguyen看到我的編輯。 – 2014-12-13 13:07:34

+0

並確保你做一個建議的修改不是兩個;-) – 2014-12-13 13:13:42

0

我通過改變以下配置來解決我的問題category category:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinTable(name = "Suppliers_Categories", joinColumns = { @JoinColumn(name = "CategoryId") }, inverseJoinColumns = { @JoinColumn(name = "SupplierId") }) 

,改變在供應商類:

@ManyToMany(mappedBy = "suppliers") 

現在worked.The問題是因爲我把從類別更新,所以我需要特定類別類的配置。

+0

這不是我回答的嗎? – 2014-12-14 05:42:17

+0

當我嘗試你的建議時,我評論了錯誤。無論如何,你的回答讓我意識到這一點。謝謝 – 2014-12-14 10:28:50