hibernate crud example in eclipse
For Explanation Watch video:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>HibernateCrud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HibernateCrud</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.9</maven.compiler.source>
<maven.compiler.target>1.9</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/new</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration>
Employee
package com.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "emptab")
public class Employee {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "eid")
private Integer empId;
@Column(name = "ename")
private String empName;
@Column(name = "dept")
private String empDept;
@Column(name = "sal")
private Double empSal;
public Employee(String empName, String empDept, Double empSal) {
super();
this.empName = empName;
this.empDept = empDept;
this.empSal = empSal;
}
}
HibernateUtil
package com.utility;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.entity.Employee;
public class HibernateUtil {
static SessionFactory factory = null;
static {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
cfg.addAnnotatedClass(Employee.class);
factory = cfg.buildSessionFactory();
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
}
Insert
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Employee;
import com.utility.HibernateUtil;
public class Insert {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin the trnsaction
ses.beginTransaction();
//create emp obj
Employee emp1 = new Employee("Angela","QA",6000.0);
Employee emp2 = new Employee("Brock","DEV",5000.0);
Employee emp3 = new Employee("Smith","BA",7000.0);
Employee emp4 = new Employee("Carla","DEV",5000.0);
//save
ses.save(emp1);
ses.save(emp2);
ses.save(emp3);
ses.save(emp4);
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Retrive
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Employee;
import com.utility.HibernateUtil;
public class Retrive {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
Employee emp = ses.get(Employee.class, 1);
System.out.println("emp with id 1 info "+emp);
}catch(Exception e) {
e.printStackTrace();
}
}
}
RetriveAll
package com.test;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Employee;
import com.utility.HibernateUtil;
public class RetriveAll {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//select * from emptab;
List<Employee> list = ses.createQuery("from Employee").getResultList();
list.forEach(System.out::println);
}catch(Exception e) {
e.printStackTrace();
}
}
}
Update
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Employee;
import com.utility.HibernateUtil;
public class Update {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin the trnsaction
ses.beginTransaction();
//get the emp with id 1
Employee emp = ses.get(Employee.class, 1);
//set the value
emp.setEmpSal(5000.0);
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
BulkUpdate
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.utility.HibernateUtil;
public class BulkUpdate {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin the trnsaction
ses.beginTransaction();
//Update emptab set sal=sal+1000;
ses.createQuery("UPDATE Employee SET empSal=empSal+"+1000.0).executeUpdate();
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Delete
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.entity.Employee;
import com.utility.HibernateUtil;
public class Delete {
public static void main(String[] args) {
SessionFactory factory = HibernateUtil.getSessionFactory();
Session ses = HibernateUtil.getSession();
try(factory;ses){
//begin the trnsaction
ses.beginTransaction();
//get the emp
Employee emp = ses.get(Employee.class, 4);
//delete
ses.delete(emp);
//commit transaction
ses.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
}
}
}
Comments
Post a Comment