Modification of the batch:
Scene: If there is a student table student, an existing attribute [college] renamed, from "Computer College" to "Computer Engineering" [does not consider the college table].
A DML (data manipulation language) operation is implemented using Hibernate to implement this batch update method. The code is as follows:
public void updateUser(String newName,String oldName) {
Session session = null;
try{
session = this.getSession();
Transaction tc = session.beginTransaction();
String hqlUpdate = "update Student set deptName=:newName where deptName= :oldName";
int updatedEntities = s.createQuery( hqlUpdate )
.setString( "newName", newName )
.setString( "oldName", oldName )
.executeUpdate();
tc.commit();
}catch(RuntimeException re){
log.debug(re.getMessage(),re);
throw re;
}finally{
if (session != null){
session.close();
}
}
}
Method Two bypasses the Hibernate API, implemented with JDBC.
public void UpdateUser(String newName,String oldName) {
Session session = null;
try{
session = this.getSession();
Transaction tc = session.beginTransaction();
Connection connection = session.connection();
PreparedStatement ps = connection.prepareStatement("update Student set deptName='"+newName+"' where deptName= '"+oldName+"'");
ps.execute();
tc.commit();
}catch(RuntimeException re){
log.debug(re.getMessage(),re);
throw re;
}catch(SQLException e){
log.debug(e.getMessage(),e);
}finally{
if (session != null){
session.close();
}
}
}