Hibernate batch update and batch deletion

Source: Internet
Author: User
Tags commit getmessage

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.