Example - Delete
---------------------------------------------------------------------------------------------------------
DeleteDemo.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 DeleteDemo {
public static void main(String[] args) {
SessionFactory factory=new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Programmer.class)
.buildSessionFactory();
Session session=factory.getCurrentSession();
try{
//Display data Before delete transaction
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nData Before deletion");
List<Programmer> myProgrammer=session.createQuery("from Programmer").list();
programmerDisplay(myProgrammer);
session.getTransaction().commit();
//deleting programmer with id 1
int programmerId=1;
session=factory.getCurrentSession();
session.beginTransaction();
Programmer obj=session.get(Programmer.class, programmerId);
session.delete(obj);
session.getTransaction().commit();
//deleting programmer with id 5
session=factory.getCurrentSession();
session.beginTransaction();
session.createQuery("delete from Programmer where id=5").executeUpdate();
session.getTransaction().commit();
//Display data After delete transaction
session=factory.getCurrentSession();
session.beginTransaction();
System.out.println("\nData Before deletion");
myProgrammer=session.createQuery("from Programmer").list();
programmerDisplay(myProgrammer);
session.getTransaction().commit();
}
finally{
session.close();
}
}
private static void programmerDisplay(List<Programmer> myProgrammer) {
for(Programmer tmpProgrammer : myProgrammer)
System.out.println(tmpProgrammer);
}
}
---------------------------------------------------------------------------------------------------------
OUTPUT
---------------------------------------------------------------------------------------------------------
Mar 04, 2018 1:10:28 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.14.Final}
Mar 04, 2018 1:10:28 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 04, 2018 1:10:29 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Mar 04, 2018 1:10:29 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Mar 04, 2018 1:10:29 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 1:10:29 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Mar 04, 2018 1:10:29 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 04, 2018 1:10:29 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Mar 04, 2018 1:10:29 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Data Before deletion
Mar 04, 2018 1:10:30 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_
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]
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=?
Hibernate: delete from programmer where id=?
Hibernate: delete from programmer where id=5
Data Before deletion
Hibernate: select programmer0_.id as id1_0_, programmer0_.email as email2_0_, programmer0_.Name as Name3_0_ from programmer programmer0_
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=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