내 클라이언트 클래스는 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>
내가하려는 것은 기존 고객을 customerId가 "2314DD0"으로 업데이트하는 것입니다. 내가 한 것은 고객의 매개 변수 중 하나를 "POJO World"로 대체하는 것입니다. 그래서 업데이트 된 고객은 "새로운 고객"의 속성을 갖지만 customerId 등은 동일합니다. 이것이 내가 "업데이트"가 더 적절할 것이라고 믿었던 이유입니다. – harryo
업데이트하려면 ID로 데이터베이스에서 객체를로드 한 다음 변경 한 다음 업데이트해야합니다. – digitaljoel