Example - Update
---------------------------------------------------------------------------------------------------------
UpdateDemo.java
---------------------------------------------------------------------------------------------------------
package com.demo.spring.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.demo.spring.entitiy.Programmer;
public class UpdateDemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Programmer.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
int programmerid=1;
session=factory.getCurrentSession();
session.beginTransaction();
List<Programmer> theProgrammer=session.createQuery("from Programmer where id ="+programmerid).list();
System.out.println("\nProgrammer details before updation");
programmerDisplay(theProgrammer);
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nGetting Programmer with id : "+programmerid);
Programmer programmer=session.get(Programmer.class, programmerid);
System.out.println("\nUpdating Programmer");
programmer.setEmail("amitesh.kumar@yahoo.com");
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
theProgrammer=session.createQuery("from Programmer where id ="+programmerid).list();
System.out.println("\nProgrammer details after updation");
programmerDisplay(theProgrammer);
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nBefore updation");
theProgrammer=session.createQuery("from Programmer").list();
programmerDisplay(theProgrammer);
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nUpdating email ids");
session.createQuery("update Programmer set email='abc.xyz@gmail.com'").executeUpdate();
session.getTransaction().commit();
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nAfter updation");
theProgrammer=session.createQuery("from Programmer").list();
programmerDisplay(theProgrammer);
session.getTransaction().commit();
}
finally{
session.close();
}
}
private static void programmerDisplay(List<Programmer> theProgrammer) {
for(Programmer tmpProgrammer : theProgrammer)
System.out.println(tmpProgrammer);
}
}
---------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------
Mar 04, 2018 12:51:52 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 04, 2018 12:51:52 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 04, 2018 12:51:53 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 04, 2018 12:51:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 04, 2018 12:51:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/bikesh?useSSL=false]
Mar 04, 2018 12:51:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 04, 2018 12:51:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 04, 2018 12:51:53 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 04, 2018 12:51:53 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Mar 04, 2018 12:51:54 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select programmer0_.id as id1_0_, programmer0_.email as email2_0_, programmer0_.Name as Name3_0_ from programmer programmer0_ where programmer0_.id=1
Programmer details before updation
Programmer [id=1, name=Amitesh Kumar, email=amitesh.kumar@yahoo.com]
Getting Programmer with id : 1
Hibernate: select programmer0_.id as id1_0_0_, programmer0_.email as email2_0_0_, programmer0_.Name as Name3_0_0_ from programmer programmer0_ where programmer0_.id=?
Updating Programmer
Hibernate: select programmer0_.id as id1_0_, programmer0_.email as email2_0_, programmer0_.Name as Name3_0_ from programmer programmer0_ where programmer0_.id=1
Programmer details after updation
Programmer [id=1, name=Amitesh Kumar, email=amitesh.kumar@yahoo.com]
Before updation
Hibernate: select programmer0_.id as id1_0_, programmer0_.email as email2_0_, programmer0_.Name as Name3_0_ from programmer programmer0_
Programmer [id=1, name=Amitesh Kumar, email=amitesh.kumar@yahoo.com]
Programmer [id=2, name=Bikesh Kumar, email=bikesh.kumar@tiwari.com]
Programmer [id=3, name=John, email=john@tiwari.com]
Programmer [id=4, name=Paula, email=paula@tiwari.com]
Programmer [id=5, name=Mary, email=mary@tiwari.com]
Programmer [id=6, name=Bonita, email=bonita@tiwari.com]
Programmer [id=7, name=Rob, email=rob@tiwari.com]
Programmer [id=8, name=Amit Kumar, email=amit.kumar@tiwari.com]
Updating email ids
Hibernate: update programmer set email='abc.xyz@gmail.com'
After updation
Hibernate: select programmer0_.id as id1_0_, programmer0_.email as email2_0_, programmer0_.Name as Name3_0_ from programmer programmer0_
Programmer [id=1, name=Amitesh Kumar, email=abc.xyz@gmail.com]
Programmer [id=2, name=Bikesh Kumar, email=abc.xyz@gmail.com]
Programmer [id=3, name=John, email=abc.xyz@gmail.com]
Programmer [id=4, name=Paula, email=abc.xyz@gmail.com]
Programmer [id=5, name=Mary, email=abc.xyz@gmail.com]
Programmer [id=6, name=Bonita, email=abc.xyz@gmail.com]
Programmer [id=7, name=Rob, email=abc.xyz@gmail.com]
Programmer [id=8, name=Amit Kumar, email=abc.xyz@gmail.com]
No comments:
Post a Comment