Nearby lessons

31 of 35

Spring - Hibernate Jars and Library

📌 What You Will Learn
  • Understand Spring - Hibernate Jars and Library
  • See working code examples
  • Learn from common mistakes and Q&A

Learn Spring - Hibernate Jars and Library step by step — simple explanations, complete programs with their output, common beginner mistakes, and exam-style MCQs.

Hibernate Jars and Library

  • import com.durgasoft.entity.Employee;
  • public interface EmployeeDao {
  • public String insertEmployee(Employee emp);
  • public Employee findEmployee(int eno);
  • public String updateEmployee(Employee emp);
  • public String removeEmployee(int eno);

Create Dao implementation class

  • package com.durgasoft.dao;
  • import javax.persistence.EntityManager;
  • import javax.persistence.PersistenceContext;
  • import org.springframework.stereotype.Repository;
  • import org.springframework.transaction.annotation.Transactional;
  • import com.durgasoft.entity.Employee;
  • @Repository
  • public class EmployeeDaoImpl implements EmployeeDao {
  • String status = "";
  • @PersistenceContext
  • private EntityManager entityManager;
  • @Transactional
  • @Override
  • public String insertEmployee(Employee emp) {
  • entityManager.persist(emp);
  • status = "SUCCESS";
  • return status;
  • @Override
  • public Employee findEmployee(int eno) {
  • Employee emp = (Employee)entityManager.find(Employee.class, eno);
  • return emp;
  • @Transactional
  • @Override
  • public String updateEmployee(Employee emp) {
  • Employee employee = (Employee)entityManager.find(Employee.class, emp.getEno())
  • employee.setEname(emp.getEname());
  • employee.setEsal(emp.getEsal());
  • employee.setEaddr(emp.getEaddr());
  • status = "SUCCESS";
  • return status;
  • @Transactional
  • @Override
  • public String removeEmployee(int eno) {
  • Employee employee = (Employee)entityManager.find(Employee.class, eno);
  • entityManager.remove(employee);
  • status = "SUCCESS";
  • return status;

 Where "@PersistenceContext" will inject EntityManager object into Dao implementation class.

 Where "@Transactional" annotation will inject Transaction service in DAO methiods, where it is not required to Create EntityTransaction object and not required to perform commit() or rollback() operations.

Create POJO / Entity class — Employee.java

Example03
Loading editor…

Create POJO / Entity class — Employee.xml

  • Create Hibernate Mapping File.
Example04
Loading editor…

Create POJO / Entity class — applicationContext.xml

  • Create Spring Configuration File
Example05
Loading editor…

Create POJO / Entity class

 Where "org.springframework.jdbc.datasource.DriverManagerDataSource" will provide dataSource object, it required the following dependencies.

  • driverClassName
  • url
  • username
  • password

 Where "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" will provide EntityManager object in Spring Application and it require the following dependencies.

  • dataSource
  • persistenceUnitName
  • jpaVendorAdapter
  • mappingResources
  • jpaProperties

 Where "dataSource" will take a DataSource reference which we configure in Spring configuration File.

 Where "persistenceUnitName" will take persistence name like "emp".  Where "jpaVendorAdpter"property will take the class like

"org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" and it will provide all Hibernate implementation of JPA to Spring application.  Where "mappingResources" property will take all mapping Files which we are using in spring applications in the form of <list> type.  Where "jpaProperties" will take JPA implementation properties like dialect, show_sql,... in the form of "<props>" type.  Where "org.springframework.orm.jpa.JpaTransactionManager" will provide Transaction implementation provided by JPA vendor.  Where "<context:annotation-config/>" tag will activate the annotations like "@Repository"  , "@Service", "@Component".......  Where "<tx:annotation-driven/>" tag will activate @Transactional annotation which will inject Transaction Service in Spring application.

Create Test Application — Employee.java

  • package com.durgasoft.test;
  • import org.springframework.context.ApplicationContext;
  • import org.springframework.context.support.ClassPathXmlApplicationContext;
  • import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  • import com.durgasoft.dao.EmployeeDao;
  • import com.durgasoft.entity.Employee;
  • public class Test {
  • public static void main(String[] args) {
  • ApplicationContext context = new ClassPathXmlApplicationContext("applicationConte

xt.xml");

  • EmployeeDao empDao = (EmployeeDao) context.getBean("empDao");
  • Employee emp = new Employee();
  • emp.setEno(111);
  • emp.setEname("AAA");
  • emp.setEsal(5000);
  • emp.setEaddr("Hyd");
  • String status = empDao.insertEmployee(emp);
  • System.out.println(status);
  • Employee emp = empDao.findEmployee(111);
  • System.out.println("Employee Details");
  • System.out.println("----------------------");
  • System.out.println("Employee Number :"+emp.getEno());
  • System.out.println("Employee Name :"+emp.getEname());
  • System.out.println("Employee Salary :"+emp.getEsal());
  • System.out.println("Employee Address :"+emp.getEaddr());
  • Employee emp = new Employee();
  • emp.setEno(111);
  • emp.setEname("BBB");
  • emp.setEsal(7000);
  • emp.setEaddr("Pune");
  • String status = empDao.updateEmployee(emp);
  • System.out.println(status);
  • String status = empDao.removeEmployee(111);
  • System.out.println(status);

Example:

Example07
Loading editor…

Create Test Application — EmployeeDao.java

Example08
Loading editor…

Create Test Application — EmployeeDaoImpl.java

Example09
Loading editor…

Create Test Application — Employee.hbm.xml

Example10
Loading editor…

Create Test Application — applicationContext.xml

Example11
Loading editor…

Create Test Application — Test.java

Example12
Loading editor…
📝 Key Takeaways
  • Key ideas of Spring - Hibernate Jars and Library explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.