[英]hibernate delete many-to-many association
I am having a Store class. 我正在上商店课。
@Entity
@Table(name = "Store")
public class Store {
@Id
@Column(name = "ST_Name", nullable = false)
private String name;
...
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="store")
private Set<StoreProductLink> products = new HashSet<StoreProductLink>();
public void addProduct(Product pr, int quantity, String lt) {
StoreProductLink link = new StoreProductLink();
...
this.products.add(link);
pr.getStores().add(link);
}
public void removeProduct(Product pr) {
StoreProductLink link = new StoreProductLink();
....
this.products.remove(link);
pr.getStores().remove(link);
}
and a Product class 和产品类别
@Entity
@IdClass(ProductPK.class)
@Table(name = "Product")
public class Product {
@Id
@Column(name = "PR_Name", nullable = false, length = 45)
private String name;
@Id
@Column(name = "PR_SerialNumber", nullable = false, length = 45)
private String serialNumber;
....
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="product")
private Set<StoreProductLink> stores = new HashSet<StoreProductLink>();
and the class StoreProductLink which is the join table 和类StoreProductLink,这是联接表
@Entity
@IdClass(StoreProductLinkPK.class)
@Table(name = "StoreProductLink")
public class StoreProductLink {
@Id
private String storeName;
@Id
private String productName;
@Id
private String productSerialNumber;
@Column(name = "quantity")
private int quantity;
@Column(name = "lt")
private String lt;
@ManyToOne
@JoinColumn(name = "storeName", updatable = false, insertable = false, referencedColumnName = "ST_Name")
private Store store;
@ManyToOne
@JoinColumns(value = {
@JoinColumn(name = "productName", updatable = false, insertable = false, referencedColumnName = "PR_Name"),
@JoinColumn(name = "productSerialNumber", updatable = false, insertable = false, referencedColumnName = "PR_Serialnumber") })
private Product product;
. 。
Store store = new Store();
Product product = new Product();
product.setName(example);
...
store.addProduct(product, 5, "com");
updateHibernateFunction(store); (this inserts in StoreProductLink table this --> storeName,example, exampleSerialNumber,5,com)
Doing 在做
store.removeProduct(product);
this function removes the product example from the Set named products. 此函数从Set命名产品中删除该产品示例。 And the
和
t1 = entityManager.merge(t);
returns a store having a products Set without the product example. 返回具有产品集但没有产品示例的商店。
updateHibernateFunction(store);
However the StoreProductLink table is still having the record. 但是,StoreProductLink表仍具有该记录。
Add operation works fine. 添加操作正常。 Delete operation doesn't work.
删除操作无效。 :/
:/
ADDED 添加
This is the update function 这是更新功能
public T update(T t)
{
EntityManager entityManager = DataLayer.getEntityManager();
T t1 = null;
EntityTransaction tx = null;
try
{
tx = entityManager.getTransaction();
tx.begin();
t1 = entityManager.merge(t);
tx.commit();
}catch(RuntimeException e)
{
if(tx != null && tx.isActive()) tx.rollback();
throw e;
}finally
{
entityManager.close();
}
return t1;
}
This is not a matter of cascade, because cascade means (in the DELETE case): "delete the target when you delete me". 这不是级联的问题,因为级联的意思是(在DELETE情况下):“删除目标时删除目标”。
Try the orphanRemoval
: 尝试
orphanRemoval
:
public class Product {
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy="product",
orphanRemoval=true)
private Set<StoreProductLink> stores = new HashSet<StoreProductLink>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.