java - Hibernate 模板更新 hql 中不断出现错误

标签 java hibernate spring

我的客户端类使用 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/

相关文章:

java - Spring Security中的自定义表达式

java - 如何Spring @requestparam验证字符串在字符串列表中

java - 使用 cron 运行 jar 会抛出错误 : '/bin/sh: 1: java: not found'

java - 什么是 WSDL 中的可扩展元素

java - 使用 ejb ql 从数据库中选择随机行

hibernate.cfg.xml配置错误

Java Hibernate 检查一个值是否存在

java - 如何使用 Hibernate HQL 或 Criteria 内部连接两个表?

java - Hibernate 给出了一个奇怪的 ClassCast 异常(使用 Transformers)

java.lang.OutOfMemoryError : Java heap space in Maven