2012-01-16 6 views
0

私のクライアントクラスは、たCustomerServiceクラスのupdateCustomerメソッド使用しています。私は、更新のために、「updateOrSave」、異なる引数を回避するためにあらゆる種類のものを試してみた定数誤差が

Customer updatedCustomer = new Customer("2314DD0", "POJO World", "Offers POJO service."); 
    customerService.updateCustomer(updatedCustomer); 

を、などに。スタックトレースに従って私が遭遇している問題は、(HibernateOptimisticLockingFailureException)と(org.hibernate.StaleStateException)です。最初に説明しましょう:私の他のすべてのデータアクセス(つまり、保存、削除、およびクエリ)は完全にうまくいきます。私は自分の顧客ドメインクラスと私が設定したCustomer.hbm.xmlについて疑念があります。

だから、私の顧客サービスクラス:

public void updateCustomer(Customer updatedCustomer) 
{ 
    this.dao.update(updatedCustomer); 
} 

カスタマー・サービスダオ:

public void update(Customer customerToUpdate) 
{ 
    template.update("Customer", customerToUpdate); 
} 

domain.Customerクラス:

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> 

答えて

1

new Customer("2314DD0", "POJO World", "Offers POJO service.");が暗示する新しいオブジェクトの場合、update()の代わりにsave()またはpersist()を呼び出してはいけませんか?

+0

私がしようとしているのは、既存の顧客をcustomerIdが "2314DD0"に更新することです。私がしたことは、顧客のパラメータの1つを「POJO World」に置き換えることです。したがって、更新された顧客は "new Customer"の属性を持ち、customerIdなどは同じです。これが「アップデート」がより適切であると私が信じた理由です。 – harryo

+0

更新したいのであれば、idでデータベースからオブジェクトをロードしてから、変更を加えてから更新してください。 – digitaljoel