如果更改没有反映在数据库中,那么使用hibernate更新记录的正确方法是什么



我有以下映射:

<hibernate-mapping>
<class name="com.jurnil.db.hibernate.Order" table="order_table">
<id name="id" type="long" column="id">
<generator class="identity"/>
</id>
<property name="shippingTotal">
<column name="shipping_total" not-null="false"/>
</property>
<one-to-one name="shippingInfo" class="com.jurnil.db.hibernate.OrderShipping" cascade="all">
</one-to-one> </class>
</hibernate-mapping>

我正在尝试从数据库中检索现有的"顺序",按顺序修改一些值,并将它们更新回数据库。

以下是我的DAO方法:

public boolean updateOrder(Order o) {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = getTransaction(s);
o.setUpdated(new Date());
s.saveOrUpdate(o);
tryCommit(tx);
return true;

我把orderDAO.updateOrder(order);称为

这些变化没有反映在数据库中,在hibernate日志中我可以看到Duplicate entry '332' for key 'PRIMARY'

当我试图将updateOrder中的会话重写为时

Session s = HibernateUtil.getSessionFactory().openSession(); 

s.saveOrUpdate(o);给我以下错误,Duplicate entry '332' for key 'PRIMARY'

在尝试了Guillaume 的建议之后

更改了DAO方法:

public boolean updateOrder(BigDecimal cartTotal, OrderShipping shippingInformation, BigDecimal taxes,
BigDecimal orderTotal, BigDecimal shippingTotal, Long id) {
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = getTransaction(s);
Order order = (Order) s.load(Order.class, id);
order.setCartTotal(cartTotal);
order.setShippingInfo(shippingInformation);
order.setTaxPaid(taxes);
order.setTotalPaid(orderTotal);
order.setShippingTotal(shippingTotal);
order.getShippingInfo().setOrder(order);
order.setUpdated(new Date());
tryCommit(tx);
return true;
}

数据库中的数据仍然没有更新,在SQL日志中,我可以看到事务正在回滚,为什么会发生这种情况

以下是SQL日志

[2020-09-17 17:45:44,125] DEBUG org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader - Done entity load : com.jurnil.db.hibernate.Order#335
[2020-09-17 17:45:44,126] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - committing
[2020-09-17 17:45:44,126] DEBUG org.hibernate.event.internal.AbstractFlushingEventListener - Processing flush-time cascades
[2020-09-17 17:45:44,135] DEBUG org.hibernate.event.internal.AbstractSaveEventListener - Generated identifier: 335, using strategy: org.hibernate.id.ForeignGenerator
org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [com.jurnil.db.hibernate.OrderShipping#335]
[2020-09-17 17:45:44,136] DEBUG org.hibernate.engine.transaction.spi.AbstractTransactionImpl - rolling back

您的Order对象需要与用于事务的会话关联。例如,如果您想用id#322:更新订单

Session s = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = getTransaction(s);
Order o = s.load(Order.class, 322);
o.setUpdated(new Date());
tryCommit(tx);

请注意,saveOrUpdate已被删除:当事务提交时,Hibernate将看到Order已被修改并更新数据库。

最新更新