gpt4 book ai didi

Java Hibernate JPA 删除 OneToMany 关系和 MapKeyJoinColumn 关系对象

转载 作者:行者123 更新时间:2023-12-01 04:37:47 25 4
gpt4 key购买 nike

我想创建和删除ProductPrice之一,更新工作正常,但其他人出现异常

产品类别

public class Product extends GenericEntity {
@OneToMany(fetch=FetchType.EAGER, mappedBy = "product", targetEntity = ProductPrice.class, cascade = {CascadeType.ALL}, orphanRemoval=true)
@Fetch(FetchMode.SUBSELECT)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region="ncStandardElements")
private List<ProductPrice> productPrices = new ArrayList<ProductPrice>();

@OneToMany(fetch=FetchType.EAGER, mappedBy = "product", targetEntity = ProductPrice.class)
@Fetch(FetchMode.SUBSELECT)
@MapKeyJoinColumn(name="CURRENCY_ID")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region="ncStandardElements")
private Map<Currency, ProductPrice> productPriceMap = new HashMap<Currency, ProductPrice>();
}

产品价格等级

public class ProductPrice extends GenericEntitySimple {

@ManyToOne(targetEntity = Product.class, optional=false)
@JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID")
private Product product;
}




public void removeProductPrice(ProductPrice price){
Product p = price.getProduct();
//Map<Currency, ProductPrice> productPriceMap = p.getProductPriceMap();
//productPriceMap.remove(price);

List<ProductPrice> prices = p.getProductPrices();
prices.remove(price);
p.setProductPrices(prices);
productDao.merge(p);
}

如果价格是在同一 session 上创建的,则删除操作会成功,但是,如果它是在当前 session 之前创建的,则会抛出此错误:

Jun 13, 2013 3:26:29 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet appServlet threw exception
org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.netasoft.commerce.framework.catalog.model.ProductPrice#220]
at org.hibernate.impl.SessionFactoryImpl$2.handleEntityNotFound(SessionFactoryImpl.java:435)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:233)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:285)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:152)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)

我没有完全理解 MapKeyJoinColumn,并且找不到有关这种情况的文档。我认为 map 列表缓存会导致此错误。任何准备充分的文档建议也会得到批准。

最佳答案

我假设在调用 removeProductPrice() 之前您打开一个 session 并开始交易。

removeProductPrice()方法中,通过执行get()将productPrice对象加载到持久化上下文。就像,

ProductPrice price = productPriceDao.get(price.getPriceId());

// Then your logic to delete.

我怀疑传递给 removeProductPrice() 方法的 ProductPrice 对象不存在于 session 的持久性上下文中。

您能否发布完整的堆栈跟踪,其中显示导致异常的确切行。

关于Java Hibernate JPA 删除 OneToMany 关系和 MapKeyJoinColumn 关系对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17087121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com