我的客户端类使用 CustomerService 类中的 updateCustomer 方法:
Customer updatedCustomer = new Customer("2314DD0", "POJO World", "Offers POJO service.");
customerService.updateCustomer(updatedCustomer);
我尝试了各种方法来解决“updateOrSave”问题、不同的更新参数等等。根据堆栈跟踪,我遇到的问题是 (HibernateOptimisticLockingFailureException) 和 (org.hibernate.StaleStateException)。但首先让我解释一下:我的所有其他数据访问(即保存、删除和查询)都运行良好。我对我的客户域类以及我设置的 Customer.hbm.xml 表示怀疑。
所以,我的客户服务类(class):
public void updateCustomer(Customer updatedCustomer)
{
this.dao.update(updatedCustomer);
}
客户服务道:
public void update(Customer customerToUpdate)
{
template.update("Customer", customerToUpdate);
}
域.客户类别:
public class Customer
{
private int id;
private String customerId, companyName, email, telephone, notes;
private Customer() {}
public Customer(String customerId, String companyName, String email,
String telephone, String notes)
{
this.customerId = customerId;
this.companyName = companyName;
this.email = email;
this.telephone = telephone;
this.notes = notes;
}
//Getters and Setters for ALL instance variables listed after this point.
}
最后,我的 Customer.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.crm.domain.Customer">
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="customerId"/>
<property name="companyName"/>
<property name="email"/>
<property name="telephone"/>
<property name="notes"/>
</class>
最佳答案
如果它是一个新对象,则由 new Customer("2314DD0", "POJO World", "Offers POJO service.");
暗示那你不应该打电话save()
或persist()
而不是update()
?
关于java - Hibernate 模板更新 hql 中不断出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8886751/