Spring ORM & Hibernate
- Spring ORM - Create Project
- Spring ORM - Maven Hibernate
- Spring ORM - Persistence Hibernate
- Spring ORM - Update Project
- Spring ORM - Run & Test Hibernate
Spring ORM & EclipseLink
- Spring ORM - Maven EclipseLink
- Spring ORM - Persistence EclipseLink
- Spring ORM - Update Project EclipseLink
- Spring ORM - Run & Test EclipseLink
Spring ORM & Useful Resources
Spring ORM - Update Project
Let's add the following classes in the project in the relevant packages.
Employee class to be persisted in database.
Employee.java
package com.tutorialspoint.jpa.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "Employees")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "NAME")
private String name;
@Column(name = "SALARY")
private double salary;
@Column(name = "DESIGNATION")
private String designation;
public Employee( ) {
}
public Employee(String name, double salary, String designation) {
this.name = name;
this.salary = salary;
this.designation = designation;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}
Employee DAO class to do operations on database.
EmployeeDao.java
package com.tutorialspoint.jpa.dao;
import java.util.List;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.springframework.stereotype.Repository;
import com.tutorialspoint.jpa.entity.Employee;
@Repository
public class EmployeeDao {
@PersistenceContext
private EntityManager em;
public void add(Employee employee) {
em.persist(employee);
}
public List<Employee> listEmployees() {
CriteriaQuery<Employee> criteriaQuery = em.getCriteriaBuilder().createQuery(Employee.class);
@SuppressWarnings("unused")
Root<Employee> root = criteriaQuery.from(Employee.class);
return em.createQuery(criteriaQuery).getResultList();
}
}
Employee Service class to use Employee DAO class.
EmployeeService.java
package com.tutorialspoint.jpa.service;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tutorialspoint.jpa.dao.EmployeeDao;
import com.tutorialspoint.jpa.entity.Employee;
@Service
public class EmployeeService {
@Autowired
private EmployeeDao employeeDao;
@Transactional
public void add(Employee employee) {
employeeDao.add(employee);
}
@Transactional()
public List<Employee> listEmployees() {
return employeeDao.listEmployees();
}
}
Application Configuration to be used by Spring framework.
AppConfig.java
package com.tutorialspoint.jpa;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.hibernate.HibernateTransactionManager;
import org.springframework.orm.jpa.hibernate.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableTransactionManagement
@ComponentScans(value = { @ComponentScan("com.tutorialspoint.jpa.dao"),
@ComponentScan("com.tutorialspoint.jpa.service") })
public class AppConfig {
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessioFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan("com.tutorialspoint.jpa.entity");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
org.springframework.jdbc.datasource.DriverManagerDataSource dataSource = new org.springframework.jdbc.datasource.DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/tutorialspoint");
dataSource.setUsername("guest");
dataSource.setPassword("guest123");
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.MySQL8Dialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "create");
return properties;
}
@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory().getObject());
return txManager;
}
}
Main Application to run and test the functionalities.
MainApp.java
package com.tutorialspoint.jpa;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.tutorialspoint.jpa.entity.Employee;
import com.tutorialspoint.jpa.service.EmployeeService;
public class MainApp {
public static void main(String[] args) throws SQLException {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
EmployeeService employeeService = context.getBean(EmployeeService.class);
employeeService.add(new Employee("Julie", 10000, "Technical Manager"));
employeeService.add(new Employee("Robert", 20000, "Senior Manager"));
employeeService.add(new Employee("Anil", 5000, "Software Engineer"));
// Get Persons
List<Employee> employees = employeeService.listEmployees();
for (Employee employee : employees) {
System.out.println("Id : "+employee.getId());
System.out.println("Name : "+employee.getName());
System.out.println("Salary = "+employee.getSalary());
System.out.println("Designation = "+employee.getDesignation());
System.out.println();
}
context.close();
}
}
Advertisements