2013-08-30 2 views
6

최대 절전 모드에서 session.saveOrUpdate() 메소드를 사용하여 테이블 행을 업데이트하려고한다.Hibernate saveOrUpdate가 갱신되지 않는다.

그러나 행을 업데이트 할 수 없으며 insert 문을 생성하여 행을 저장하려고 시도합니다. 이 삽입은 DB에서 null이 아닌 몇 개의 필드로 인해 작동하지 않습니다.

DAO 계층에 저장할 개체의 ID를 검색 할 수 있으므로 DB 테이블의 해당 행을 업데이트하지 않는 이유를 이해할 수 없습니다.

콩 클래스 :

public class EmployeeMasterBean extends BaseEntityBean { 

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

@Column(name = "FirstName", nullable = false) 
private String firstName; 

@Column(name = "LastName", nullable = false) 
private String lastName; 

@Temporal(TemporalType.TIMESTAMP) 
@Column(name = "Dob", insertable = true, updatable = true, nullable = false) 
private Date dateOfBirth; 

@Column(name = "Email", length = 100) 
private String email; 

@Column(name = "PhoneNumber", nullable = false) 
private String phoneNumber; 

@Column(name = "Address1", nullable = false) 
private String address1; 

@Column(name = "Type", nullable = false) 
private Short employeeType; 

@Column(name = "Gender", nullable = false) 
private Short gender; 


/** 
* @return the firstName 
*/ 
public final String getFirstName() { 
    return firstName; 
} 

/** 
* @param firstName the firstName to set 
*/ 
public final void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

/** 
* @return the lastName 
*/ 
public final String getLastName() { 
    return lastName; 
} 

/** 
* @param lastName the lastName to set 
*/ 
public final void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

/** 
* @return the dateOfBirth 
*/ 
public final Date getDateOfBirth() { 
    return dateOfBirth; 
} 

/** 
* @param dateOfBirth the dateOfBirth to set 
*/ 
public final void setDateOfBirth(Date dateOfBirth) { 
    this.dateOfBirth = dateOfBirth; 
} 

/** 
* @return the email 
*/ 
public final String getEmail() { 
    return email; 
} 

/** 
* @param email the email to set 
*/ 
public final void setEmail(String email) { 
    this.email = email; 
} 

/** 
* @return the phoneNumber 
*/ 
public final String getPhoneNumber() { 
    return phoneNumber; 
} 

/** 
* @param phoneNumber the phoneNumber to set 
*/ 
public final void setPhoneNumber(String phoneNumber) { 
    this.phoneNumber = phoneNumber; 
} 

/** 
* @return the address1 
*/ 
public final String getAddress1() { 
    return address1; 
} 

/** 
* @param address1 the address1 to set 
*/ 
public final void setAddress1(String address1) { 
    this.address1 = address1; 
} 

/** 
* @return the employeeType 
*/ 
public final Short getEmployeeType() { 
    return employeeType; 
} 

/** 
* @param employeeType the employeeType to set 
*/ 
public final void setEmployeeType(Short employeeType) { 
    this.employeeType = employeeType; 
} 


/** 
* @return the gender 
*/ 
public final Short getGender() { 
    return gender; 
} 

/** 
* @param gender the gender to set 
*/ 
public final void setGender(Short gender) { 
    this.gender = gender; 
} 
} 

DAO 방법 (BaseEntityBean 이드, CreatedBy 등이있다) :

public EmployeeMasterBean saveOrUpdateEmployee(EmployeeMasterBean employeeMasterBean) throws Exception{ 
    Session session = null; 
    Transaction tx = null; 

    try { 
     session = HibernateUtil.getSessionFactory().openSession(); 
     tx = session.beginTransaction(); 

     session.saveOrUpdate(employeeMasterBean); 

     tx.commit(); 
    } finally { 
     session.close(); 
    } 
    return employeeMasterBean; 
} 
던져

이클립스 디버거 예외는 다음과 같습니다으로

could not insert: [com.indven.gpil.hrd.entity.EmployeeMasterBean] 
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'CreatedBy' cannot be null 
+0

'employeeMasterBean.id'는 null입니까? – mabbas

+0

'employeeMasterBean'이 createdBy를 설정 했습니까? – Ved

+0

분리 된 엔티티를 전달하는 것처럼 들립니다.) – Thomas

답변

1

bean에는 rowVersion 속성 (낙관적 잠금 용)이 없으므로 기본적으로 null입니다. Hibernate는 이것을 이것을 새로운 레코드로 해석하고 그것을 저장했다.

레코드를 저장하거나 업데이트하려고 할 때마다 행 버전을 값 개체 및 해당 Bean에 저장하여 문제를 해결했습니다.

0

오류 메시지가 데이터베이스에 null이 될 수없는 createdby 열이 있습니다.

saveOrUpdate()을 호출 할 때 누군가가이 속성을 null으로 설정 했으므로 업데이트 할 수 없습니다.

0

나는 CreatedBy 열이 notnull으로 DB 테이블에 존재하지만 콩이 열 당신이 saveOrUpdate을 수행 할 때 그러므로 null 값이 던져 질 예외보다도 일으키는, 전송, 매핑하지 않습니다 생각합니다.

bean에 CreatedBy에 대한 맵핑을 일부 기본값으로 추가하고 트리거 등이 기본값을 갱신 할 수있게하십시오. 또는 데이터베이스에서 Nullable이되도록 열을 변경할 수있는 경우

관련 문제