2013-08-30 16 views
6

Próbuję zaktualizować wiersz tabeli przy użyciu metody session.saveOrUpdate() w Hibernacja.Hibernate saveOrUpdate nie aktualizuje

Nie można jednak zaktualizować wiersza i próbuje go zapisać, tworząc instrukcję wstawiania. Ta wstawka nie działa z powodu kilku nie-pustych pól w moim DB.

Jestem w stanie pobrać identyfikator obiektu do zapisania w warstwie DAO, więc nie jestem w stanie zrozumieć, dlaczego nie aktualizuje on tylko odpowiedniego wiersza w tabeli DB.

Bean Klasa: (BaseEntityBean ma identyfikator, CreatedBy, etc.)

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 metoda:

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; 
} 

debugger Eclipse wyjątki rzucane są:

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

Czy "employeeMasterBean.id" jest puste? – mabbas

+0

'employeeMasterBean' stworzyłBy zestaw? – Ved

+0

Wygląda na to, że przechodzisz do oddzielonej jednostki;) – Thomas

Odpowiedz

1

Komponent bean nie ma ustawionej właściwości rowVersion (dla optymistycznego blokowania), a tym samym domyślnie ma wartość NULL. Hibernate zinterpretował to jako nowy rekord i zachował go.

Naprawiłem to przez przechowywanie wersji wiersza w moim obiekcie wartości i odpowiadającej fasoli za każdym razem, gdy próbowałem zapisać lub zaktualizować jakiekolwiek rekordy.

0

Jak komunikat o błędzie mówi, baza danych ma kolumnę createdby, która nie może być pusta.

Po wywołaniu saveOrUpdate() ktoś ustawił tę właściwość na null, więc aktualizacja nie jest możliwa.

0

myślę CreatedBy kolumna jest obecny w tabeli DB jako notnull ale twoja fasola nie ma w tej kolumnie odwzorowany, stąd wartość null jest wysyłany, kiedy wykonujesz saveOrUpdate, co powoduje, że powyższy wyjątek do rzucania.

Albo dodaj mapowanie do CreatedBy w komponencie bean z pewną wartością domyślną, a wyzwalacz itp. Może zaktualizować wartość domyślną. Lub jeśli można zmienić kolumnę na zerowalną w bazie danych

Powiązane problemy