2013-08-30 32 views
6

Sto tentando di aggiornare una riga della tabella utilizzando il metodo session.saveOrUpdate() in Hibernate.Hibernate saveOrUpdate non si aggiorna

Tuttavia, non è possibile aggiornare la riga e tenta di salvarla producendo un'istruzione di inserimento. Questo inserto non funziona a causa di alcuni campi non annullabili nel mio DB.

Sono in grado di recuperare l'ID dell'oggetto da salvare sul livello DAO, quindi non sono in grado di capire perché non aggiorna solo la riga corrispondente nella tabella DB.

Bean Classe: (BaseEntityBean ha l'Es, CreatedBy, ecc)

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

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

Eclipse eccezioni debugger gettati sono:

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' ha creatoBy set? – Ved

+0

Sembra che tu stia passando un'entità distaccata;) – Thomas

risposta

1

Il bean non disponeva della proprietà rowVersion (per il blocco ottimistico) e quindi, per impostazione predefinita, era null. Hibernate l'ha interpretato come un nuovo record e ha continuato a salvarlo.

L'ho risolto memorizzando la versione di riga nel mio oggetto valore e il bean corrispondente ogni volta che tentavo di salvare o aggiornare qualsiasi record.

0

Come il messaggio di errore, il database ha una colonna createdby che non può essere nullo.

Quando hai chiamato saveOrUpdate() qualcuno ha impostato questa proprietà su null quindi l'aggiornamento non è possibile.

0

Penso CreatedBy colonna è presente nella tabella di DB come notnull ma il bean non dispone di questa colonna mappata, quindi un valore null viene inviato quando si esegue una saveOrUpdate, che provoca Sopra eccezione a essere gettato.

Aggiungere una mappatura a CreatedBy nel bean con un valore predefinito e lasciare che trigger ecc possa aggiornare il valore predefinito. Oppure se è possibile modificare la colonna in modo da rendere nullable nel database

Problemi correlati