Spring ORM – Update Project


Spring ORM – Update Project



”;


Let”s add the following classes in the project in the relevant packages.


Project Structure

Employee class to be persisted in database.

Employee.java


package com.tutorialspoint.jpa.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;

@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 javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
import javax.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 javax.transaction.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 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.JpaTransactionManager;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
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 LocalEntityManagerFactoryBean geEntityManagerFactoryBean() {
      LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
      factoryBean.setPersistenceUnitName("Hibernate_JPA");
      return factoryBean;
   }
   @Bean
   public JpaTransactionManager geJpaTransactionManager() {
      JpaTransactionManager transactionManager = new JpaTransactionManager();
      transactionManager.setEntityManagerFactory(geEntityManagerFactoryBean().getObject());
      return transactionManager;
   }
}

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

”;

Leave a Reply

Your email address will not be published. Required fields are marked *