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
JCode Cell
1 
2package com.durgasoft.entity;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34}
35

Create POJO / Entity class — Employee.xml

  • Create Hibernate Mapping File.
Example04
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.entity.Employee" table="emp1">
8 <id name="eno"/>
9 <property name="ename"/>
10 <property name="esal"/>
11<property name="eaddr"/>
12</class>
13</hibernate-mapping>
14

Create POJO / Entity class — applicationContext.xml

  • Create Spring Configuration File
Example05
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:aop="http://www.springframework.org/schema/aop"
6xmlns:tx="http://www.springframework.org/schema/tx"
7xmlns:context="http://www.springframework.org/schema/context"
8 
9xsi:schemaLocation="
10 http://www.springframework.org/schema/beans
11http://www.springframework.org/schema/beans/spring-beans.xsd
12http://www.springframework.org/schema/tx
13http://www.springframework.org/schema/tx/spring-tx.xsd
14http://www.springframework.org/schema/aop
15http://www.springframework.org/schema/aop/spring-aop.xsd
16 http://www.springframework.org/schema/context
17http://www.springframework.org/schema/context/spring-context.xsd">
18<context:annotation-config/>
19<tx:annotation-driven/>
20<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
21<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
22<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
23<property name="username" value="system"/>
24<property name="password" value="durga"/>
25</bean>
26<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
27<property name="dataSource" ref="dataSource"/>
28<property name="persistenceUnitName" value="emp"/>
29<property name="jpaVendorAdapter">
30<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
31</property>
32<property name="mappingResources">
33<list>
34<value>Employee.xml</value>
35</list>
36</property>
37<property name="jpaProperties">
38<props>
39<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
40<prop key="hibernate.show_sql">true</prop>
41</props>
42</property>
43</bean>
44<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
45<property name="entityManagerFactory" ref="entityManagerFactoryBean"/>
46</bean>
47<bean id="empDao" class="com.durgasoft.dao.EmployeeDaoImpl"/>
48</beans>
49

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
JCode Cell
1 
2package com.durgasoft.pojo;
3 
4public class Employee {
5private int eno;
6private String ename;
7private float esal;
8private String eaddr;
9 
10public int getEno() {
11return eno;
12}
13public void setEno(int eno) {
14this.eno = eno;
15}
16public String getEname() {
17return ename;
18}
19public void setEname(String ename) {
20this.ename = ename;
21}
22public float getEsal() {
23return esal;
24}
25public void setEsal(float esal) {
26this.esal = esal;
27}
28public String getEaddr() {
29return eaddr;
30}
31public void setEaddr(String eaddr) {
32this.eaddr = eaddr;
33}
34 
35public String toString() {
36return "["+eno+","+ename+","+esal+","+eaddr+"]";
37}
38 
39}
40

Create Test Application — EmployeeDao.java

Example08
JCode Cell
1 
2package com.durgasoft.dao;
3 
4import com.durgasoft.entity.Employee;
5 
6public interface EmployeeDao {
7public String insertEmployee(Employee emp);
8public Employee findEmployee(int eno);
9public String updateEmployee(Employee emp);
10public String removeEmployee(int eno);
11}
12

Create Test Application — EmployeeDaoImpl.java

Example09
JCode Cell
1 
2package com.durgasoft.dao;
3 
4import javax.persistence.EntityManager;
5import javax.persistence.PersistenceContext;
6 
7import org.springframework.stereotype.Repository;
8import org.springframework.transaction.annotation.Transactional;
9 
10import com.durgasoft.entity.Employee;
11 
12@Repository
13public class EmployeeDaoImpl implements EmployeeDao {
14 
15String status = "";
16 
17@PersistenceContext
18private EntityManager entityManager;
19 
20@Transactional
21@Override
22public String insertEmployee(Employee emp) {
23entityManager.persist(emp);
24status = "SUCCESS";
25return status;
26}
27 
28@Override
29public Employee findEmployee(int eno) {
30Employee emp = (Employee)entityManager.find(Employee.class, eno);
31return emp;
32}
33 
34@Transactional
35@Override
36public String updateEmployee(Employee emp) {
37Employee employee = (Employee)entityManager.find(Employee.class, emp.getEno());
38employee.setEname(emp.getEname());
39employee.setEsal(emp.getEsal());
40employee.setEaddr(emp.getEaddr());
41status = "SUCCESS";
42return status;
43}
44 
45@Transactional
46@Override
47public String removeEmployee(int eno) {
48Employee employee = (Employee)entityManager.find(Employee.class, eno);
49entityManager.remove(employee);
50status = "SUCCESS";
51return status;
52}
53 
54}
55

Create Test Application — Employee.hbm.xml

Example10
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<!DOCTYPE hibernate-mapping PUBLIC
4"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
6<hibernate-mapping>
7<class name="com.durgasoft.pojo.Employee" table="emp2">
8 <id name="eno" column="ENO"/>
9 <property name="ename" column="ENAME"/>
10 <property name="esal" column="ESAL"/>
11<property name="eaddr" column="EADDR"/>
12</class>
13</hibernate-mapping>
14

Create Test Application — applicationContext.xml

Example11
JCode Cell
1 
2<?xml version="1.0" encoding="UTF-8"?>
3<beans xmlns="http://www.springframework.org/schema/beans"
4xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5xmlns:aop="http://www.springframework.org/schema/aop"
6xmlns:tx="http://www.springframework.org/schema/tx"
7xmlns:context="http://www.springframework.org/schema/context"
8 
9xsi:schemaLocation="
10 http://www.springframework.org/schema/beans
11http://www.springframework.org/schema/beans/spring-beans.xsd
12http://www.springframework.org/schema/tx
13http://www.springframework.org/schema/tx/spring-tx.xsd
14http://www.springframework.org/schema/aop
15http://www.springframework.org/schema/aop/spring-aop.xsd
16 http://www.springframework.org/schema/context
17http://www.springframework.org/schema/context/spring-context.xsd">
18<context:annotation-config/>
19<tx:annotation-driven/>
20<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
21<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
22<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
23<property name="username" value="system"/>
24<property name="password" value="durga"/>
25</bean>
26<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
27<property name="dataSource" ref="dataSource"/>
28<property name="persistenceUnitName" value="emp"/>
29<property name="jpaVendorAdapter">
30<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
31</property>
32<property name="mappingResources">
33<list>
34<value>Employee.xml</value>
35</list>
36</property>
37<property name="jpaProperties">
38<props>
39<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
40<prop key="hibernate.show_sql">true</prop>
41</props>
42</property>
43</bean>
44<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
45<property name="entityManagerFactory" ref="entityManagerFactoryBean"/>
46</bean>
47<bean id="empDao" class="com.durgasoft.dao.EmployeeDaoImpl"/>
48</beans>
49

Create Test Application — Test.java

Example12
JCode Cell
1 
2package com.durgasoft.test;
3 
4import org.springframework.context.ApplicationContext;
5import org.springframework.context.support.ClassPathXmlApplicationContext;
6import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
7 
8import com.durgasoft.dao.EmployeeDao;
9import com.durgasoft.entity.Employee;
10 
11public class Test {
12 
13public static void main(String[] args) {
14ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
15EmployeeDao empDao = (EmployeeDao) context.getBean("empDao");
16/*
17Employee emp = new Employee();
18emp.setEno(111);
19emp.setEname("AAA");
20emp.setEsal(5000);
21emp.setEaddr("Hyd");
22String status = empDao.insertEmployee(emp);
23System.out.println(status);
24*/
25/*
26Employee emp = empDao.findEmployee(111);
27System.out.println("Employee Details");
28System.out.println("----------------------");
29System.out.println("Employee Number :"+emp.getEno());
30System.out.println("Employee Name :"+emp.getEname());
31System.out.println("Employee Salary :"+emp.getEsal());
32System.out.println("Employee Address :"+emp.getEaddr());
33*/
34/*
35Employee emp = new Employee();
36emp.setEno(111);
37emp.setEname("BBB");
38emp.setEsal(7000);
39emp.setEaddr("Pune");
40String status = empDao.updateEmployee(emp);
41System.out.println(status);
42*/
43String status = empDao.removeEmployee(111);
44System.out.println(status);
45}
46 
47}
48
📝 Key Takeaways
  • Key ideas of Spring - Hibernate Jars and Library explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end