BY NAGOOR BABU
SPRING COURSE MATERIAL BY NAGOOR BABU
{ SPRING ORM} 1 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
2 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
SPRING CORE MODULE INDEX 1. 2. 3. 4.
Spring ORM…………………………………..……………………………..….…..PAGE 04 Application on Spring-Hibernate integration………..…………..…..PAGE 12 JPA[Java Persistence API]…………………………………………….………PAGE 18 Integration of JPA with Spring application in ORM Module ……PAGE 32
3 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
Spring ORM Spring ORM: In enterprise applications both the data models are having their own approaches to represent data in effective manner, these differences are able to provide Paradiagm Mismatches, these mismatches are able to reduce data persistency in ent erprise applications. In general, Object Oriented Data Model and Relational data model are are having the following mismatches. 1. Granualarity Mismatch 2. Sub types mismatch 3. Associations Mismatch 4. Identity Mismatch To improve Data persistency in Enterprise applications we have to resolve the above specified mismtahces between Data models, for this, we have to use "ORM" implementations. To implement ORM in Enterprise applications we have to use the following ORM implementations. 1. EJBs-Entity Beans 2. JPA 3. Hibernate 4. IBatis 5. JDO If we want to use Hibernate in enterprise applications then we have to use the following sateps. 1) Persistence Class or Object. Object. 2) Prepare Mapping File. 3) Prepare Hibernate Configuration File 4) Prepare Hibernate Client Application To prepare Hibernate Client Application we have to use the following steps. 1. Create Configuration class object 2. Create SessionFactory object 3. Create Session Object 4. Create Transaction Transaction object if it is required. 5. Persistence operations 6. Close Session Session Factory Factory and Session Session objects. objects. Example: 1. Configuration cfg cfg = = new new Configuration(); Configuration(); 2. cfg.configure("hibernate.cfg.xml"); 3. SessionFactory session_Factory session_Factory = = cfg cfg.buildSessionFactory(); .buildSessionFactory(); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
4
BY NAGOOR BABU
4. Session session session = = session_Factory session_Factory.openSession(); .openSession(); 5. Transaction tx tx = = session session.beginTransaction(); .beginTransaction(); 6. Employee emp emp = = new new Employee(); Employee(); 7. emp.setEno(111); 8. emp.setEname("AAA"); 9. emp.setEsal(5000); 10. emp.setEaddr("Hyd"); 11. session.save(emp); 12. tx.commit(); 13. System.out.println("Employee Record inserted Succesfully"); 14. session.close(); 15. session_Factory.close(); To remove the above boilerplate code, SPRING Framework has provided ORM Module. Spring has provided the complete ORM module in i n the form of "org.springframework.orm" package. To abstract the above boilerplate code Spring-ORM module m odule has provided a predefined class in the fomr of "org.springframework.orm.hiberna "org.springframework.orm.hibernate4.HibernateTemplate" te4.HibernateTemplate" w.r.t Hibernate4 version Note: If Note: If we use Hibernate3.x version then we have to use "org.springframework.orm.hibernate3.HibernateTemplate" "org.springframework.orm.hibernate3 .HibernateTemplate" class. org.springframework.orm.hibernate4.HibernateTemplate class has provided the following methods org.springframework.orm.hibernate4.HibernateTemplate inorder to perform persistence operations. 1. public void persist(Object entity) 2. public Serializable save(Object entity) 3. public void saveOrUpdate(Object saveOrUpdate(Object entity) entity) 4. public void update(Object entity) 5. public void delete(Object entity) 6. public Object Object get(Class get(Class entityClass, entityClass, Serializable Serializable id) 7. public Object Object load(Class load(Class entityClass, entityClass, Serializable Serializable id) 8. public List loadAll(Class entityClass) If we want to Integrate Hibernate with Spring then we have to use the following steps. 1) 2) 3) 4) 5) 6) 7)
Create Java Project with both Spring[including Spring[including ORM] and Hibernate Hibernate Libraries. Create Bean/POJO class. Prepare Hibernate Mapping File Create DAO interface with with persistence methods. Create DAO implementation implementation class with HibernateTemplate as property. Prepare Spring Configuration File Prepare Client Application
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
5
BY NAGOOR BABU
1. Create Java Project with both Spring[including ORM] and Hibernate Libraries. Prepare JAVA project in Eclipse IDE and add the following JAR files to Buildpath in the form of the following Libraries. Spring4_Lib: spring-aop-4.0.4.RELEASE.jar spring-beans-4.0.4.RELEASE.jar spring-context-4.0.4.RELEASE.jar spring-context-support-4.0.4.RELEASE.jar spring-core-4.0.4.RELEASE.jar spring-expression-4.0.4.RELEASE.jar spring-jdbc-4.0.4.RELEASE.jar commons-io-2.6.jar commons-logging-1.2.jar spring-tx-4.0.4.RELEASE.jar spring-aspects-4.0.4.RELEASE.jar spring-orm-4.0.4.RELEASE.jar Hibernate4_Lib: ojdbc6.jar antlr-2.7.7.jar dom4j-1.6.1.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-commons-annotations-4.0.5.Final.jar hibernate-core-4.3.11.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar jandex-1.1.0.Final.jar javassist-3.18.1-GA.jar jboss-logging-3.1.3.GA.jar jboss-logging-annotations-1.2.0.Beta1.jar jboss-logging-annotation s-1.2.0.Beta1.jar jboss-transaction-api_1.2_spec-1.0.0.Final.jar jboss-transaction-api_1.2_s pec-1.0.0.Final.jar hibernate-entitymanager-4.3.11.Final.jar 2. Create Bean/POJO class. 1. 2. 3. 4. 5. 6.
public class class Student{ Student{ private String private String sid; private String private String sname; private String private String saddr; setXXX() and getXXX() } 6 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
3. Prepare Hibernate Mapping File: Student.hbm.xml 1. 2.
3. > 4. /> 5. /> 6. /> 7. 8. 4. Create DAO interface with persistence methods. The main intention of DAO interface is to declare all Services. EX: 1. public interface interface StudentDao StudentDao { 2. public String public String insert(Student std); 3. public String public String update(Student std); 4. public String public String delete(Student std); 5. public Employee public Employee getStudent(int getStudent(int eno); eno); 6. } 5. Create DAO implementation class with wi th HibernateTemplate as property. The main intention of DAO implementation class is to implement all DAO methods. In DAO implementation class every every DAO method must be declared declared with @Transactional annotation annotation inorder to activate Spring Transaction Service. Note: If we use @Transactional annotation then it is not required to create Transaction object explicitly and iit is not required to perform commit and rollback operations explicitly. In DAO implementation class we must declare HibernateTemplate property and its respective setXXX() method inorder to inject i nject HibernateTemplate object. Example: 1. public class class StudentDaoImpl StudentDaoImpl implements implements StudentDao StudentDao { 2. String status = "" "";; 3. private HibernateTemplate private HibernateTemplate hibernateTemplate; 4. setHibernateTemplate(HibernateTemplate hibernateTemplate) { public void void setHibernateTemplate(HibernateTemplate 5. this.hibernateTemplate this .hibernateTemplate = hibernateTemplate; CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
7
BY NAGOOR BABU
6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48.
} @Transactional public String public String insert(Student std) { try { try { hibernateTemplate.save(std); status = "Insertion Success"; Success"; }catch catch(Exception (Exception ex) { ex.printStackTrace(); status = "Insertion Failure"; Failure"; } status; return status; return } @Transactional public String public String update(Student std) { try { try { hibernateTemplate.update(std); status = "Updations Success"; Success"; }catch catch(Exception (Exception ex) { ex.printStackTrace(); status = "Updations Failure"; Failure"; } return status; return status; } @Transactional public String public String delete(Student std) { try { try { hibernateTemplate.delete(std); status = "Deletion Success"; Success"; }catch (Exception ex) { catch(Exception ex.printStackTrace(); status = "Deletion Failure"; Failure"; } return status; return status; } @Transactional public Employee public Employee getStudent(int getStudent(int sid) sid) { Student std = null null;; try { try {
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
8 e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
49. std = (Student)hibernateTempl (Student)hibernateTemplate.get(Student. ate.get(Student.class class,, sid); 50. }catch catch(Exception (Exception ex) { 51. ex.printStackTrace(); 52. } 53. return std; return std; 54. } 55. } 6. Prepare Spring Configuration File: In Spring Configuration File we must configure the following beans 1. DriverManagerDataSource 2. LocalSessionFactoryBean 3. HibernateTransactionManager 4. HibernateTemplate 5. StudentDaoImpl
Where org.springframework.jdbc.datas org.springframework.jdbc.datasource.DriverManagerD ource.DriverManagerDataSource ataSource configuration is able to provide Spring inbuilt Connectionpooling Connectionpooling Mechanism and it will include the properties liike "driverClassName, url, username, password".
Where org.springframework.orm.hibernate4.Lo org.springframework.orm.hibernate4.LocalSessionFactory calSessionFactoryBean Bean configuration configuration is able to create SessionFactory object by including the properties like 1. dataSource : represents DataSource bean which was configured in Spring Configuration file. 2. mappingResources: It will take list of values contains mapping files in the form of "
" tag. 3. hibernateProperties: hibernateProperties: It will take hibernate properties in the form of "" tag, it must includes mainly "hibernate.dialect" property.
Where org.springframework.orm.hibernate4 org.springframework.orm.hibernate4.HibernateTransactionMa .HibernateTransactionManager nager configuration will activate Transaction Manager inorder to provide Tr ansaction Support and it will include "sessionFactory" property.
Where org.springframework.orm.hibernate4.Hibern org.springframework.orm.hibernate4.HibernateTemplate ateTemplate configuration will provide HibernateTemplate object inorder to perform persistence operations and it will include "sessionFactory" and "checkWriteOperations" with false value.
Where com.durgasoft.dao.StudentDaoImpl com.durgasoft.dao.StudentDaoImpl configuration configuration will provide provide DAO object object inorder to access Dao methods and it will include "hibernateTemplate" property. @Transactional annotation in DAO methods , we we must use " " tag in spring configuration file. P
a
g
e
9
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
Example: applicationContext.xml 1. ?> 2. http://www.springframework.org/schema/aop/spring-aop.xsd" > 13. 14. ataSource" > 15. /> 16. /> 17. /> 18. /> 19. 20. actoryBean" > 21. /> 22. > 23. 24. Student.hbm.xml Student.hbm.xml 25.
26. 27. > 28. 29. >org.hibernate.dialect.Oracle10gDialect org.hibernate.dialect.Oracle10gDialect 30. >true true 31. 32. 33. 34. 35. nsactionManager" > 36. /> CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
1
0
BY NAGOOR BABU
37. 38. 39. emplate" > 40. /> 41. > 42. 43. > 44. /> 45. 46. 7. Prepare Client Application The main intention of Client Application is to get Dao object and to access Dao object. Example: 1. ApplicationContext context context = = new ClassPathXmlApplicationCo ClassPathXmlApplicationContext("applicationContex ntext("applicationContext.xml t.xml "); 2. StudentDao stdDao stdDao = = (StudentDao)context.getBean("stdDao"); (StudentDao)context.getBean("stdDao"); 3. Student std std = = new new Student(); Student(); 4. std.setSid("S-111"); 5. std.setSname("AAA"); 6. std.setSaddr("Hyd"); 7. String status status = = stdDao stdDao.insert(std); .insert(std); 8. System.out.println(status); 9. or 10. Student std std = = (Student)stdDao.getStud (Student)stdDao.getStudent(Student.class,"S-111"); ent(Student.class,"S-111"); 11. System.out.println("Student Details"); 12. System.out.println("--------------------"); 13. System.out.println("Student Id :"+std.getSid()); 14. System.out.println("Student Name :"+std.getSname()); 15. System.out.println("Student Address :"+std.getSaddr()); 16. or 17. Student std std = = new new Student(); Student(); 18. std.setSid("S-111"); 19. std.setSname("BBB"); 20. std.setSaddr("Hyd"); 21. String status status = = stdDao stdDao.update(std); .update(std); 22. System.out.println(status); 23. or 24. String status status = = stdDao stdDao.delete("S-111"); .delete("S-111"); 25. System.out.println(status); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
1
1
BY NAGOOR BABU
Application on Spring-Hibernate integration Application on Spring-Hibernate integration: Employee.java 1. package package com.durgasoft.pojo; com.durgasoft.pojo; 2. 3. public class class Employee Employee { 4. eno; private int int eno; 5. private String private String ename; 6. private float float esal; esal; 7. private String private String eaddr; 8. 9. public int int getEno() getEno() { 10. return eno; return eno; 11. } 12. public void void setEno( setEno(int int eno) eno) { 13. this.eno this .eno = eno; 14. } 15. public String getEname() { public String 16. return ename; return ename; 17. } 18. public void void setEname(String setEname(String ename) { 19. this.ename this .ename = ename; 20. } 21. public float float getEsal() getEsal() { 22. esal; return esal; return 23. } 24. public void void setEsal( setEsal(float float esal) esal) { 25. this.esal this .esal = esal; 26. } 27. public public String String getEaddr() { 28. return eaddr; return eaddr; 29. } 30. public void void setEaddr(String setEaddr(String eaddr) { 31. this.eaddr this .eaddr = eaddr; 32. } 33. 34. public public String String toString() { 35. return "[" "["+eno+ +eno+"," ","+ename+ +ename+"," ","+esal+ +esal+"," ","+eaddr+ +eaddr+"]" "]";; 36. } CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
2 1 e g a P
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
37. 38. } EmployeeDao.java 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. 3. import import com.durgasoft.pojo.Employee; com.durgasoft.pojo.Employee; 4. 5. public interface interface EmployeeDao EmployeeDao { 6. public String public String insert(Employee e); 7. public String public String update(Employee e); 8. public String public String delete(Employee e); 9. public Employee public Employee getEmployee(int getEmployee(int eno); eno); 10. } EmployeeDaoImpl.java 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. import import org.hibernate.FlushMode; org.hibernate.FlushMode; 3. import import org.hibernate.Transaction; org.hibernate.Transaction; 4. import org.springframework.orm.hibernate4. org.springframework.orm.hibernate4.HibernateTemplate; HibernateTemplate; 5. import org.springframework.transaction.an org.springframework.transaction.annotation.Transactional; notation.Transactional; 6. 7. import import com.durgasoft.pojo.Employee; com.durgasoft.pojo.Employee; 8. 9. public class class EmployeeDaoImpl EmployeeDaoImpl implements implements EmployeeDao EmployeeDao { 10. String status = "" "";; 11. private private HibernateTemplate HibernateTemplate hibernateTemplate; 12. public void void setHibernateTemplate(HibernateTemplate setHibernateTemplate(HibernateTemplate hibernateTemplate) { 13. this.hibernateTemplate this .hibernateTemplate = hibernateTemplate; 14. 15. } 16. 17. @Override 18. @Transactional 19. public public String String insert(Employee e) { 20. try { try { 21. hibernateTemplate.save(e); 22. status = "Insertion Success"; Success"; 23. }catch catch(Exception (Exception ex) { 24. ex.printStackTrace(); 25. status = "Insertion Failure"; Failure"; CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
1
3
BY NAGOOR BABU
26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. }
} return status; return status; } @Override @Transactional String update(Employee e) { public String public try { try { hibernateTemplate.update(e); status = "Updations Success"; Success"; }catch catch(Exception (Exception ex) { ex.printStackTrace(); status = "Updations Failure"; Failure"; } return status; return status; } @Override @Transactional public String public String delete(Employee e) { try { try { hibernateTemplate.delete(e); status = "Deletion Success"; Success"; }catch catch(Exception (Exception ex) { ex.printStackTrace(); status = "Deletion Failure"; Failure"; } return status; return status; } @Override @Transactional public Employee public Employee getEmployee(int getEmployee(int eno) eno) { Employee emp = null null;; try { try { emp = (Employee)hibernateT (Employee)hibernateTemplate.get(Employee. emplate.get(Employee.class class,, eno); }catch catch(Exception (Exception ex) { ex.printStackTrace(); } return emp; return emp; } a
g
e
1
4
P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
Employee.hbm.xml 1. ?> 2. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > 5. 6. > 7. /> 8. /> 9. /> 10. 11. 12. applicationContext.xml 1. ?> 2. http://www.springframework.org/schema/aop/spring-aop.xsd" > 13. 14. 15. ataSource" > 16. /> 17. /> 18. /> 19. /> 20. 21. actoryBean" > 22. /> 23. > 24. CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
1
5
BY NAGOOR BABU
25. Employee.hbm.xml Employee.hbm.xml 26.
27. 28. > 29. 30. >org.hibernate.dialect.Oracle10gDialect org.hibernate.dialect.Oracle10gDialect 31. 32. >true true 33. 34. 35. 36. 37. nsactionManager" > 38. /> 39. 40. 41. emplate" > 42. /> 43. > 44. 45. > 46. /> 47. 48. Test.java 1. package package com.durgasoft.test; com.durgasoft.test; 2. 3. import org.springframework.contex org.springframework.context.ApplicationContext; t.ApplicationContext; 4. import org.springframework.contex org.springframework.context.support.ClassPathXmlApplica t.support.ClassPathXmlApplicationContext; tionContext; 5. import org.springframework.orm.hibernate4. org.springframework.orm.hibernate4.HibernateTemplate; HibernateTemplate; 6. 7. import com.durgasoft.dao.Emplo com.durgasoft.dao.EmployeeDao; yeeDao; 8. import import com.durgasoft.pojo.Employee; com.durgasoft.pojo.Employee; 9. 10. public class class Test Test { 11. 12. public static void void main(String[] main(String[] args)throws args)throws Exception Exception { 13. ApplicationContext ApplicationContext context = new ClassPathXmlApplica ClassPathXmlApplicationContext( tionContext("applicationConte "applicationConte xt.xml"); xt.xml" ); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
1
6
BY NAGOOR BABU
14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. } 26. 27. }
EmployeeDao empDao = (EmployeeDao)context.getBean( (EmployeeDao)context.getBean("empDao" "empDao"); ); Employee emp = new new Employee(); Employee(); emp.setEno(111 emp.setEno(111); ); emp.setEname("AAA" emp.setEname("AAA"); ); emp.setEsal(5000 emp.setEsal(5000); ); emp.setEaddr("Hyd" emp.setEaddr("Hyd"); ); String status = empDao.insert(emp); System.out.println(status); System.out.println(empDao.getEmployee(111 System.out.println(empDao.getEmployee( 111)); ));
7 1 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
JPA[Java Persistence API] JPA[Java Persistence API] Introduction: database operation operations s in enterprise applications JPA is a an API , it can be used to perform database with ORM implementation tools.
JPA was provided provided by J2EE along along with EJB3.0 version as a persistence mechanism.
JPA is a specification specification provided by SUN Microsystems and and it is implemented implemented by some third party vendors like JBOSS, Eclipse Foundations, Apache......
JPA is following following ORM rules regulations to achieve data persistency in enterprise applications and it is implemented by the following tools.
1. Hibernate ---------> JBOSS 2. EclipseLink -------> Eclipse Foundation 3. Open JPA --------> Apache Software Foundations Foundations Note: If Note: If we want to use JPA in enterprise applications then we must use either of the JPA implementations. If we want to prepare JPA applications with "Hibernate" JPA provider then we have to use the following steps. 1. Create Java project project in Eclipse with JPA library which includes all Hibernate Hibernate JARs. 2. Create Entity Entity class under src folder. 3. Create mapping File or Use Use JPA annotations annotations in POJO class. 4. Create JPA configuration File[persistence.xml] 5. Create Test Application. 1. Create Java project in Eclipse with JPA library which includes all Hibernate JARs. This step is same as Java project creation and it will include JPA provider Library that is Hibernate jars. 2. Create Entity class under src folder. Student.java 8 1
1. package com.durgasoft.entity; package com.durgasoft.entity; 2. public class class Student{ Student{ CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
3. private private String String sid; 4. private private String String sname; 5. private private String String saddr; 6. setXXX() and getXXX() 7. } 3. Create mapping File or Use JPA annotations in POJO class. It is same as Hibernate mapping file, it will provide mapping between Object Oriented Data model elements like class, Id property, normal properties with the relational data model elements like Table name, Primary Key Columns, normal columns,.... columns,.... EX: Student.xml 1. 2. > 3. /> 4. /> 5. /> 6. 7. 4. Create JPA configuration File[persistence.xml] JPA configuration File is same as Hibernate COnfiguration File, it include all JPA configuration details which are required to interact with database . IN general, we will provide the following configuration details in JPA configuration file. 1. Jdbc Parameters like Driver class name, Driver Driver URL, Database Database user name, Database Database password. 2. Dialect configurations 3. Mapping File or Annotated classes configuration 4. Cache Mechanisms configurations 5. Transactions configurations The default name of the JPA configuration file is "persistence.xml". Ex persistence.xml: 9 1 e
1. g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
2. > 3. 4. Student.xml Student.xml 5. 6. /> 7. /> 8. /> 9. /> 10. /> 11. name= ="hibernate.show_sql" value value= ="true" /> 12. /> 13. 14. 15.
Where tag is root tag in JPA configuration File. Where tag is able to take mapping mapping file configuration where tag will include JPA properties. properties. Where tag will take a single JPA JPA property like driver driver class name, driver driver url,....
5. Create Test Application: The main intention of Test /Client application is to perform persistence operations . To prepare Test application in JPA we have to use the following steps. 1. Create EntityManagerFactory Object. 2. Create EntityManager Object. 3. Create EntityTransaction EntityTransaction Object Object as per per the requirement 4. Perform Persistence operation 5. Perform Commit or rollback operations if we use EntityTransaction. EntityTransaction. 1. Create EntityManagerFactory Object: javax.persistence.EntityManagerFactory is a Factory class, it able to manage no of EntityManager javax.persistence.EntityManagerFactory EntityManager object. To get EntitymanagerFactory class object we have to use the following method from javax.persistence.Persistence javax.persistence.Pers istence class. public static EntityManagerFactory createEntityManagerFactory(String createEntityManagerFactory(String persistence_Unit_Name); e
2
0 g
EX: EntityManagerFactory EX: EntityManagerFactory factory = Persistence.createEntitymanagerFactory("std"); Persistence.createEntitymanagerFactory("std"); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
BY NAGOOR BABU
2. Create EntityManager Object: javax.persistence.EntityManager is an interface, it able to provide predefined Library to perform javax.persistence.EntityManager persistence operations. To get EntityManager object we have to use the following method from EntiotyManagerFactory. EntiotyManagerFactory. public EntityManager createEntityManager() EX: EntityManager EX: EntityManager entManager = factory.createEntitymanag f actory.createEntitymanager(); er(); 3. Create EntityTransaction Object as per the requirement javax.persistence.EntityTransaction is a class, it able to provide Tranmsaction javax.persistence.EntityTransaction Tranmsaction support in JPA applications inorder to perform persistence operations. To get EntityTramsaction object we have to use the following method from EntityManager. public EntityTransaction getTransaction() EX: EntityTransaction EX: EntityTransaction entTransaction = entManager.getTrans entManager.getTransaction(); action(); Note: EntityTransaction Note: EntityTransaction contains the following methods inorder to complete Transaction. public void commit() public void rollback() Note: EntityTranmsaction Note: EntityTranmsaction is required for only non select operations only, not for select operations. 4. Perform Persistence operation: To perform Persistence operations operations we have to use the following following methods from EntityManager object. 1. public Object Object find(Class find(Class entity_Class, entity_Class, Serializable Serializable pk_Value) pk_Value) 2. public void persist(Object obj) 3. public void remove(Object obj) Note: To Note: To perform Updations , first we have to get Entity object fr om Database table by using find() method then we have to use set New data to Entity Object then perform commit operation. EX: 1
1. EntityTransaction entTranction entTranction = = entManager .getTransaction(); .getTransaction(); 2. entTransaction.begin(); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
2
BY NAGOOR BABU
3. 4. 5. 6. 7.
Student std std = = (Student)entManager.find(Student.class, "S-111"); std.setSname("BBB'"); std.setSaddr("Sec"); entTransaction.commit(); System.out.println("Student Updated Successfully"); Successfully");
EX: Test.java 1. public class class Test Test { 2. 3. public static void void main(String[] main(String[] args)throws args)throws Exception Exception { 4. EntityManagerFactory factory = Persistence.createEntityMan Persistence.createEntityManagerFactory( agerFactory("std" "std"); ); 5. EntityManager entManager = factory.createEntityManage factory.createEntityManager(); r(); 6. Student std = new new Student(); Student(); 7. std.setSid("S-111" std.setSid("S-111"); ); 8. std.setSname("AAA" std.setSname("AAA"); ); 9. std.setSaddr("Hyd" std.setSaddr("Hyd"); ); 10. EntityTransaction tx = entManager.getTransaction(); 11. tx.begin(); 12. entManager.persist(std); 13. tx.commit(); 14. System.out.println("Student System.out.println("Student Inserted Succssfully"); Succssfully"); 15. } 16. }
Simple JPA Example with XML Mapping File: Employee.java
1. package com.durgasoft.pojo; package com.durgasoft.pojo; 2. public class class Employee Employee { 3. private int eno; int eno; 4. private String private String ename; 5. private float float esal; esal; 6. private String private String eaddr; 7. public int int getEno() getEno() { 8. return eno; return eno; 9. } 2 2 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. }
public void void setEno( setEno(int int eno) eno) { this.eno this .eno = eno; } public String public String getEname() { return ename; return ename; } setEname(String ename) { public void void setEname(String this.ename this .ename = ename; } public float float getEsal() getEsal() { return esal; return esal; } public void void setEsal( setEsal(float float esal) esal) { this.esal this .esal = esal; } public String public String getEaddr() { return eaddr; return eaddr; } public void void setEaddr(String setEaddr(String eaddr) { this.eaddr this .eaddr = eaddr; }
Employee.xml 1. ?> 2. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > 5. 6. > 7. /> 8. /> 9. /> 10. /> 11. 12. src/META-INF/persistence.xml 3
1. ?> 2.
a
g
e
2
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
3.
xsi:schemaLocation="http://java.sun.com/xml/ns/persistenc xsi:schemaLocation= "http://java.sun.com/xml/ns/persistence e http://java.sun.com/xml/ns/ http://java.sun.com/xml/ns/p p ersistence/persistence_2_0.xsd" 4. version= version ="2.0" xmlns xmlns= ="http://java.sun.com/xml/ns/persistence" "http://java.sun.com/xml/ns/persistence"> > 5. > 6. 7. Employee.xml Employee.xml 8. 9. /> 10. /> 11. /> 12. name= ="javax.persistence.jdbc.password" value value= ="durga" /> 13. /> 14. /> 15. /> 16. 17. 18. Test.java 1. package package com.durgasoft.test; com.durgasoft.test; 2. 3. import import javax.persistence.EntityManager; javax.persistence.EntityManager; 4. import javax.persistence.EntityM javax.persistence.EntityManagerFactory; anagerFactory; 5. import javax.persistence.EntityTra javax.persistence.EntityTransaction; nsaction; 6. import javax.persistence.Persis javax.persistence.Persistence; tence; 7. 8. import import com.durgasoft.pojo.Employee; com.durgasoft.pojo.Employee; 9. 10. public class class Test Test { 11. 12. public static void void main(String[] main(String[] args)throws args)throws Exception Exception { 13. EntityManagerFactory factory = Persistence.createEntityMan Persistence.createEntityManagerFactory( agerFactory("emp" "emp"); ); 14. EntityManager entManager = factory.createEntityManage factory.createEntityManager(); r(); 15. Employee emp = new new Employee(); Employee(); 16. emp.setEno(111 emp.setEno(111); ); 17. emp.setEname("AAA" emp.setEname("AAA"); ); 18. emp.setEsal(5000 emp.setEsal(5000); ); 19. emp.setEaddr("Hyd" emp.setEaddr("Hyd"); ); 20. EntityTransaction tx = entManager.getTransaction(); 21. tx.begin(); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
2
4
BY NAGOOR BABU
22. entManager.persist(emp); 23. tx.commit(); 24. System.out.println("Employee System.out.println("Employee Inserted Succssfully" Succssfully"); ); 25. } 26. }
Simple JPA Example with Annotations: If we want to use Annotations in JPA application then we have to use the following steps. 1.Use javax.persistence provided provided annotations in Entity class: 1. @Entity 2. @Table 3. @Id 4. @Column 2. Configure Annotated class in persistence.xml file: 1. 2. > 3. com.durgasoft.entity.Student com.durgasoft.entity.Student 4. -----5. 6. Example: Employee.java 1. package package com.durgasoft.pojo; com.durgasoft.pojo; 2. 3. import import javax.persistence.Column; javax.persistence.Column; 4. import import javax.persistence.Entity; javax.persistence.Entity; 5. import import javax.persistence.Id; javax.persistence.Id; 6. import import javax.persistence.Table; javax.persistence.Table; 7. 8. @Entity 9. @Table @Table(name= (name="emp2" "emp2")) 10. public class class Employee Employee { 11. @Id 12. @Column @Column(name= (name="ENO" "ENO")) 13. private int int eno; eno; 5 2 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. }
@Column(name="ENAME" @Column(name= "ENAME")) private String private String ename; @Column(name= @Column (name="ESAL" "ESAL")) private float float esal; esal; @Column(name= @Column (name="EADDR" "EADDR")) private String private String eaddr; getEno() { public int int getEno() return eno; return eno; } public void void setEno( setEno(int int eno) eno) { this.eno this .eno = eno; } public String public String getEname() { return ename; return ename; } public void void setEname(String setEname(String ename) { this.ename this .ename = ename; } public float float getEsal() getEsal() { return esal; return esal; } public void void setEsal( setEsal(float float esal) esal) { this.esal this .esal = esal; } public String public String getEaddr() { return eaddr; return eaddr; } public void void setEaddr(String setEaddr(String eaddr) { this.eaddr this .eaddr = eaddr; }
src/META-INF/persistence.xml 1. ?> 2. > 5. > 6. com.durgasoft.pojo.Employee com.durgasoft.pojo.Employee CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
2
6
BY NAGOOR BABU
7. 8. 9. 10.
/> /> 11. /> 12. name= ="javax.persistence.jdbc.password" value value= ="durga" /> 13. /> 14. /> 15. /> 16. 17. 18. Test.java 1. package package com.durgasoft.test; com.durgasoft.test; 2. 3. import import javax.persistence.EntityManager; javax.persistence.EntityManager; 4. import javax.persistence.EntityM javax.persistence.EntityManagerFactory; anagerFactory; 5. import javax.persistence.EntityTra javax.persistence.EntityTransaction; nsaction; 6. import javax.persistence.Persis javax.persistence.Persistence; tence; 7. 8. import import com.durgasoft.pojo.Employee; com.durgasoft.pojo.Employee; 9. 10. public class Test { class Test 11. 12. public static void void main(String[] main(String[] args)throws args)throws Exception Exception { 13. EntityManagerFactory factory = Persistence.createEntityMan Persistence.createEntityManagerFactory( agerFactory("emp" "emp"); ); 14. EntityManager entManager = factory.createEntityManage factory.createEntityManager(); r(); 15. Employee emp = new new Employee(); Employee(); 16. emp.setEno(111 emp.setEno(111); ); 17. emp.setEname("AAA" emp.setEname("AAA"); ); 18. emp.setEsal(5000 emp.setEsal(5000); ); 19. emp.setEaddr("Hyd" emp.setEaddr("Hyd"); ); 20. EntityTransaction tx = entManager.getTransaction(); 21. tx.begin(); 22. entManager.persist(emp); 23. tx.commit(); 24. System.out.println("Employee System.out.println("Employee Inserted Succssfully" Succssfully"); ); 25. } 26. } CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
2
7
BY NAGOOR BABU
JPA With ECLIPSE Link Implementattion: If we want to use JPA with EclipseLink implementation then we have to use the following steps. 1. Create JPA project. 2. Create Entity Class with Annotations 3. Create JPA configuration File 4. Create Test Application 1. Create JPA project. 1. Right Click on "Project Explorer". 2. Select on "New". 3. Select "Others" 4. Search and Select JPA Project 5. Click on "Next" button. 6. Provide package name:app6 7. Click on "next" button. 8. Click on "Next" button. 9. Click on "Download Libraries" icon. 10. Select EclipseLink2.5.2 library. lib rary. 11. Click on "Next" button. 12. Select "Check box" of Accepct Licence of this Aggrement. 13. Click on "Finish" Button. 14. Click on "Finish" button. 15. Click on "Open Perspective". With these steps JPA project will be created in projects Explorer part.. 2. Create Entity Class with Annotations Create package "com.durgasoft.entity" under src folder and create Entity class. Employee.java 1. 2. 3. 4. 5. 6. 7. 8.
package com.durgasoft.entity; package com.durgasoft.entity; import java.io.Serializable; import java.io.Serializable; import java.lang.String; import java.lang.String; import javax.persistence.*; import javax.persistence.*; @Entity @Table(name= @Table (name="emp1" "emp1")) public class class Employee Employee implements implements Serializable Serializable { 8 2 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51.
@Id @Column(name= @Column (name="ENO" "ENO")) private int int eno; eno; @Column(name= @Column (name="ENAME" "ENAME")) private String private String ename; @Column(name= @Column (name="ESAL" "ESAL")) private float float esal; esal; @Column(name= @Column (name="EADDR" "EADDR")) private String private String eaddr; private static final long long serialVersionUID serialVersionUID = 1L; public Employee() { public Employee() super (); (); } public int int getEno() getEno() { return this this.eno; .eno; } public void void setEno( setEno(int int eno) eno) { this.eno this .eno = eno; } public String public String getEname() { return this this.ename; .ename; } public void void setEname(String setEname(String ename) { this.ename this .ename = ename; } public float float getEsal() getEsal() { return this this.esal; .esal; } public void void setEsal( setEsal(float float esal) esal) { this.esal this .esal = esal; } String getEaddr() { public String public return this this.eaddr; .eaddr; } 9
public void void setEaddr(String setEaddr(String eaddr) { this.eaddr this .eaddr = eaddr; }
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
2 e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
52. 53. } 3. Create JPA configuration File Open persistence.xml file which is existed under"src\META-INF" folder and provide the following details. persistence.xml 1. ?> 2. ml/ns/persistence/persistence_2_1.xsd" > 3. > 4. com.durgasoft.entity.Employee com.durgasoft.entity.Employee 5. 6. /> 7. 8. /> 9. /> 10. 11. 12. 4. Create Test Application Create a package "com.durgasoft.test" and prepare Test class: Test.java 1. package package com.durgasoft.test; com.durgasoft.test; 2. 3. import import javax.persistence.EntityManager; javax.persistence.EntityManager; 4. import javax.persistence.EntityM javax.persistence.EntityManagerFactory; anagerFactory; 5. import javax.persistence.EntityTra javax.persistence.EntityTransaction; nsaction; 6. import javax.persistence.Persis javax.persistence.Persistence; tence; 7. 8. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 9. 10. public class class Test Test { 11. 0 3 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
12. public static void void main(String[] main(String[] args)throws args)throws Exception Exception { 13. EntityManagerFactory EntityManagerFactory factory = Persistence.createEntity Persistence.createEntityManagerFactory( ManagerFactory("emp" "emp"); ); 14. EntityManager entityManager = factory.createEntityManage factory.createEntityManager(); r(); 15. /* 16. EntityTransaction entityTransaction = entityManager.getTransac entityManager.getTransaction(); tion(); 17. entityTransaction.begin(); 18. Employee emp = new Employee(); 19. emp.setEno(111); 20. emp.setEname("AAA"); 21. emp.setEsal(5000); 22. emp.setEaddr("Hyd"); 23. entityManager.persist(emp); 24. entityTransaction.commit(); 25. System.out.println("Employee Inserted Successfully"); 26. */ 27. /* 28. Employee emp = entityManager.find(Employ entit yManager.find(Employee.class, ee.class, 111); 29. System.out.println("Employee Details"); 30. System.out.println("------------------------"); 31. System.out.println("Employee Number :"+emp.getEno()); 32. System.out.println("Employee Name :"+emp.getEname()); 33. System.out.println("Employee Salary :"+emp.getEsal()); 34. System.out.println("Employee Address :"+emp.getEaddr()); 35. */ 36. /* 37. EntityTransaction entityTransaction = entityManager.getTransac entityManager.getTransaction(); tion(); 38. entityTransaction.begin(); 39. Employee emp = entityManager.find(Employ entit yManager.find(Employee.class, ee.class, 111); 40. emp.setEname("BBB"); 41. emp.setEsal(7000); 42. emp.setEaddr("Sec"); 43. entityTransaction.commit(); 44. System.out.println("Employee updated Successfully"); 45. */ 46. EntityTransaction entityTransaction = entityManager.getTransac entityManager.getTransaction(); tion(); 47. entityTransaction.begin(); 48. Employee emp = entityManager.find(Employ entityManager.find(Employee. ee.class 111); ); class,, 111 49. entityManager.remove(emp); 50. entityTransaction.commit(); 51. System.out.println("Employee System.out.println("Employee Deleted Successfully"); Successfully" ); 52. entityManager.close(); 53. } 54. } CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
3
1
BY NAGOOR BABU
Integration of JPA with Spring application in ORM Module Integration of JPA with Spring application in ORM Module: 1. 2. 3. 4. 5. 6. 7.
Create Java Project with both Spring Library and Hibernate Library. Create Dao interface Create Dao implementation classs. Create POJO / Entity Entity class. Create Hibernate Mapping File. Create Spring Configuration File. Create Test Application.
1. Create Java Project with both Spring Library and and Hibernate Library. Spring Library: commons-logging-1.2.jar spring-aop-4.3.9.RELEASE.jar spring-beans-4.3.9.RELEASE.jar spring-context-4.3.9.RELEASE.jar spring-context-support-4.3.9.RELEASE.jar spring-core-4.3.9.RELEASE.jar spring-expression-4.3.9.RELEASE.jar spring-jdbc-4.3.9.RELEASE.jar spring-orm-4.3.9.RELEASE.jar spring-tx-4.3.9.RELEASE.jar Hibernate Library: hibernate3.jar antlr-2.7.6.jar commons-collections-3.1.jar dom4j-1.6.1.jar javassist-3.12.0.GA.jar jta-1.1.jar slf4j-api-1.6.1.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ojdbc6.jar
2 3 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
2. Create Dao interface EmployeeDao.java 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. 3. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 4. 5. public interface EmployeeDao { interface EmployeeDao 6. public String public String insertEmployee(Employee emp); 7. public Employee public Employee findEmployee(int findEmployee(int eno); eno); 8. public String public String updateEmployee(Emplo updateEmployee(Employee yee emp); 9. public String public String removeEmployee(int removeEmployee(int eno); eno); 10. } 3.Create Dao implementation class 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. 3. import import javax.persistence.EntityManager; javax.persistence.EntityManager; 4. import javax.persistence.Persis javax.persistence.PersistenceContext; tenceContext; 5. 6. import org.springframework.stereotype.R org.springframework.stereotype.Repository; epository; 7. import org.springframework.transaction.an org.springframework.transaction.annotation.Transactional; notation.Transactional; 8. 9. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 10. 11. @Repository 12. public class class EmployeeDaoImpl EmployeeDaoImpl implements implements EmployeeDao EmployeeDao { 13. 14. String status = "" "";; 15. 16. @PersistenceContext 17. private private EntityManager EntityManager entityManager; 18. 19. @Transactional 20. @Override 21. public public String String insertEmployee(Employee emp) { 22. entityManager.persist(emp); 23. status = "SUCCESS" "SUCCESS";; 24. return status; return status; P
a
g
e
3
3
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.
} @Override public Employee public Employee findEmployee(int findEmployee(int eno) eno) { Employee emp = (Employee)entityManage (Employee)entityManager.find(Employee. r.find(Employee.class class,, eno); return emp; return emp; } @Transactional @Override public String public String updateEmployee(Employee emp) { Employee employee = (Employee)entityManager. (Employee)entityManager.find(Employee. find(Employee.class class,, emp.getEno()) ;
37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. }
employee.setEname(emp.getEname()); employee.setEsal(emp.getEsal()); employee.setEaddr(emp.getEaddr()); status = "SUCCESS" "SUCCESS";; return status; return status; } @Transactional @Override public String public String removeEmployee(int removeEmployee(int eno) eno) { Employee employee = (Employee)entityManager. (Employee)entityManager.find(Employee. find(Employee.class class,, eno); entityManager.remove(employee); status = "SUCCESS" "SUCCESS";; return status; return status; }
Where "@PersistenceContext" "@PersistenceContext" will inject EntityManager EntityManager object into Dao implementation class. "@Transactional" annotation will inject Transaction Transaction service in DAO methiods, methiods, where Where "@Transactional" it is not required to Create EntityTransaction object and not required to perform commit() or rollback() operations. 4. Create POJO / Entity class: Employee.java 4 3
1. package package com.durgasoft.entity; com.durgasoft.entity; 2. CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
3. public class class Employee Employee { 4. private int int eno; eno; 5. private String private String ename; 6. private float float esal; esal; 7. private String private String eaddr; 8. 9. getEno() { public int int getEno() 10. return eno; return eno; 11. } 12. public void void setEno( setEno(int int eno) eno) { 13. this.eno this .eno = eno; 14. } 15. public public String String getEname() { 16. return ename; return ename; 17. } 18. public void void setEname(String setEname(String ename) { 19. this.ename this .ename = ename; 20. } 21. public float float getEsal() getEsal() { 22. return esal; return esal; 23. } 24. public void void setEsal( setEsal(float float esal) esal) { 25. this.esal this .esal = esal; 26. } 27. public public String String getEaddr() { 28. return eaddr; return eaddr; 29. } 30. public void void setEaddr(String setEaddr(String eaddr) { 31. this.eaddr this .eaddr = eaddr; 32. } 33. } 5. Create Hibernate Mapping File. Employee.xml 1. ?> 2. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > 5. 6. > 7. name= ="eno" /> CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
3
5
BY NAGOOR BABU
8. /> 9. /> 10. /> 11. 12. 6. Create Spring Configuration File applicationContext.xml 1. ?> 2. http://www.springframework.org/schema/context/spring-context.xsd" > 17. 18. 19. Source" > 20. /> 21. /> 22. /> 23. /> 24. 25. rEntityManagerFactoryBean" > 26. /> 27. /> 28. > 29. /> 30. 31. > CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
3
6
BY NAGOOR BABU
32. 33. Employee.xml Employee.xml 34.
35. 36. > 37. 38. key= ="hibernate.dialect" "hibernate.dialect"> org.hibernate.dialect.OracleDialect org.hibernate.dialect.OracleDialect 39. >true true 40. 41. 42. 43. ger" > 44. /> 45. 46. /> 47.
Where "org.springframework.jdbc.datasou "org.springframework.jdbc.datasource.DriverManagerDataSo rce.DriverManagerDataSource" urce" will provide dataSource object, it required the following f ollowing dependencies. 1. driverClassName 2. url 3. username 4. password
Where "org.springframework.orm.jpa.LocalCo "org.springframework.orm.jpa.LocalContainerEntityManagerFac ntainerEntityManagerFactoryBean" toryBean" will provide EntityManager object in Spring Application and it require the following dependencies. 1. dataSource 2. persistenceUnitName 3. jpaVendorAdapter 4. mappingResources 5. jpaProperties
Where "dataSource" "dataSource" will take a DataSource reference which which we configure in Spring Spring configuration File.
Where "persistenceUnitName" "persistenceUnitName" will will take persistence name name like "emp".
Where "jpaVendorAdpter"property "jpaVendorAdpter"property will take the class like "org.springframework.orm.jpa.vendor.HibernateJpaVend "org.springframework.orm.jpa.vendo r.HibernateJpaVendorAdapter" orAdapter" and it will provide all Hibernate implementation of JPA to Spring application.
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
3
7
BY NAGOOR BABU
Where "mappingResources" "mappingResources" property will take all mapping mapping Files which we are using in spring applications in the form of type.
Where "jpaProperties" "jpaProperties" will take JPA implementation properties like dialect, show_sql,... in the form of "" type.
Where "org.springframework.orm.jpa.JpaTransac "org.springframework.orm.jpa.JpaTransactionManager" tionManager" will provide Transaction implementation provided by JPA vendor.
Where "" "" tag will activate the annotations annotations like "@Repository" , "@Service", "@Component".......
Where "" "" tag will activate @Transactional @Transactional annotation which will inject Transaction Service in Spring application.
7. Create Test Application: 1. package package com.durgasoft.test; com.durgasoft.test; 2. 3. import org.springframework.contex org.springframework.context.ApplicationContext; t.ApplicationContext; 4. import org.springframework.contex org.springframework.context.support.ClassPathXmlApplicationCo t.support.ClassPathXmlApplicationContext; ntext; 5. import org.springframework.orm.jpa.vend org.springframework.orm.jpa.vendor.HibernateJpaVendo or.HibernateJpaVendorAdapter; rAdapter; 6. 7. import com.durgasoft.dao.Emplo com.durgasoft.dao.EmployeeDao; yeeDao; 8. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 9. 10. public class class Test Test { 11. 12. public static void void main(String[] main(String[] args) { 13. ApplicationContext ApplicationContext context = new ClassPathXmlApplica ClassPathXmlApplicationContext( tionContext("applicationConte "applicationConte xt.xml"); xt.xml" ); 14. EmployeeDao empDao = (EmployeeDao) context.getBean("empDao" context.getBean("empDao"); ); 15. /* 16. Employee emp = new Employee(); 17. emp.setEno(111); 18. emp.setEname("AAA"); 19. emp.setEsal(5000); 20. emp.setEaddr("Hyd"); 21. String status = empDao.insertEmploy empDao.insertEmployee(emp); ee(emp); 22. System.out.println(status); 23. */ 24. /* CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
3
8
BY NAGOOR BABU
25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. } 45. 46. }
Employee emp = empDao.findEmployee empDao.findEmployee(111); (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.updateEmploy empDao.updateEmployee(emp); ee(emp); System.out.println(status); */ String status = empDao.removeEmplo empDao.removeEmployee( yee(111 111); ); System.out.println(status);
Example: Employee.java 1. package package com.durgasoft.pojo; com.durgasoft.pojo; 2. 3. public class class Employee Employee { 4. private int int eno; eno; 5. private String private String ename; 6. private float float esal; esal; 7. private String private String eaddr; 8. 9. public int int getEno() getEno() { 10. return eno; return eno; 11. } 12. public void void setEno( setEno(int int eno) eno) { 13. this.eno this .eno = eno; 14. } 15. public public String String getEname() { 16. ename; return ename; return CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
9 3 e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. }
} public void void setEname(String setEname(String ename) { this.ename this .ename = ename; } public float float getEsal() getEsal() { return esal; return esal; } public void void setEsal( setEsal(float float esal) esal) { this.esal this .esal = esal; } public String public String getEaddr() { eaddr; return eaddr; return } public void void setEaddr(String setEaddr(String eaddr) { this.eaddr this .eaddr = eaddr; } public String toString() { public String return "[" "["+eno+ +eno+"," ","+ename+ +ename+"," ","+esal+ +esal+"," ","+eaddr+ +eaddr+"]" "]";; }
EmployeeDao.java 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. 3. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 4. 5. public interface interface EmployeeDao EmployeeDao { 6. public String public String insertEmployee(Employee emp); 7. public Employee public Employee findEmployee(int findEmployee(int eno); eno); 8. public String public String updateEmployee(Emplo updateEmployee(Employee yee emp); 9. public String public String removeEmployee(int removeEmployee(int eno); eno); 10. } EmployeeDaoImpl.java 1. package package com.durgasoft.dao; com.durgasoft.dao; 2. 3. import import javax.persistence.EntityManager; javax.persistence.EntityManager; 4. import javax.persistence.Persis javax.persistence.PersistenceContext; tenceContext; 5. CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
0 4 e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
6. import org.springframework.stereotype.R org.springframework.stereotype.Repository; epository; 7. import org.springframework.transaction.an org.springframework.transaction.annotation.Transactional; notation.Transactional; 8. 9. import import com.durgasoft.entity.Employee; com.durgasoft.entity.Employee; 10. 11. @Repository 12. public class EmployeeDaoImpl implements EmployeeDao { class EmployeeDaoImpl implements EmployeeDao 13. 14. String status = "" "";; 15. 16. @PersistenceContext 17. private EntityManager entityManager; private EntityManager 18. 19. @Transactional 20. @Override 21. public public String String insertEmployee(Employee emp) { 22. entityManager.persist(emp); 23. status = "SUCCESS" "SUCCESS";; 24. return status; return status; 25. } 26. 27. @Override 28. public public Employee Employee findEmployee(int findEmployee(int eno) eno) { 29. Employee emp = (Employee)entityManage (Employee)entityManager.find(Employee. r.find(Employee.class class,, eno); 30. return emp; return emp; 31. } 32. 33. @Transactional 34. @Override 35. public public String String updateEmployee(Employee emp) { 36. Employee employee = (Employee)entityManager. (Employee)entityManager.find(Employee. find(Employee.class class,, emp.getEno()) ; 37. employee.setEname(emp.getEname()); 38. employee.setEsal(emp.getEsal()); 39. employee.setEaddr(emp.getEaddr()); 40. status = "SUCCESS" "SUCCESS";; 41. status; return status; return 42. } 43. 44. @Transactional 45. @Override 46. public public String String removeEmployee(int removeEmployee(int eno) eno) { 47. Employee employee = (Employee)entityManager. (Employee)entityManager.find(Employee. find(Employee.class class,, eno); CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
P
a
g
e
4
1
BY NAGOOR BABU
48. entityManager.remove(employee); 49. status = "SUCCESS" "SUCCESS";; 50. return status; return status; 51. } 52. 53. } Employee.hbm.xml 1. ?> 2. "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > 5. 6. > 7. /> 8. /> 9. name= ="esal" column column= ="ESAL" /> 10. 11. 12. applicationContext.xml 1. ?> 2. http://www.springframework.org/schema/context/spring-context.xsd" > 17. 18. P
a
g
e
4
2
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
19. Source" > 20. /> 21. /> 22. /> 23. /> 24. 25. rEntityManagerFactoryBean" > 26. /> 27. /> 28. > 29. /> 30. 31. > 32. 33. Employee.xml Employee.xml 34.
35. 36. > 37. 38. >org.hibernate.dialect.OracleDialect org.hibernate.dialect.OracleDialect 39. >true true 40. 41. 42. 43. ger" > 44. /> 45. 46. /> 47. Test.java 1. 2. 3. 4. 5. 6. 7. 8.
package com.durgasoft.test; package com.durgasoft.test; import org.springframework.contex org.springframework.context.ApplicationContext; t.ApplicationContext; import org.springframework.context.supp org.springframework.context.support.ClassPathXmlApplicati ort.ClassPathXmlApplicationContext; onContext; import org.springframework.orm.jpa.vend org.springframework.orm.jpa.vendor.HibernateJpaVendo or.HibernateJpaVendorAdapter; rAdapter; 4
import com.durgasoft.dao.Emplo com.durgasoft.dao.EmployeeDao; yeeDao; com.durgasoft.entity.Employee; import com.durgasoft.entity.Employee; import
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
3 e g a P
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,
BY NAGOOR BABU
9. 10. public class class Test Test { 11. 12. public static void void main(String[] main(String[] args) { 13. ApplicationContext ApplicationContext context = new ClassPathXmlApplica ClassPathXmlApplicationContext( tionContext("applicationConte "applicationConte xt.xml"); xt.xml" ); 14. EmployeeDao empDao = (EmployeeDao) context.getBean("empDao" context.getBean("empDao"); ); 15. /* 16. Employee emp = new Employee(); 17. emp.setEno(111); 18. emp.setEname("AAA"); 19. emp.setEsal(5000); 20. emp.setEaddr("Hyd"); 21. String status = empDao.insertEmploy empDao.insertEmployee(emp); ee(emp); 22. System.out.println(status); 23. */ 24. /* 25. Employee emp = empDao.findEmployee empDao.findEmployee(111); (111); 26. System.out.println("Employee Details"); 27. System.out.println("----------------------"); 28. System.out.println("Employee Number :"+emp.getEno()); 29. System.out.println("Employee Name :"+emp.getEname()); 30. System.out.println("Employee Salary :"+emp.getEsal()); 31. System.out.println("Employee Address :"+emp.getEaddr()); 32. */ 33. /* 34. Employee emp = new Employee(); 35. emp.setEno(111); 36. emp.setEname("BBB"); 37. emp.setEsal(7000); 38. emp.setEaddr("Pune"); 39. String status = empDao.updateEmploy empDao.updateEmployee(emp); ee(emp); 40. System.out.println(status); 41. */ 42. String status = empDao.removeEmplo empDao.removeEmployee( yee(111 111); ); 43. System.out.println(status); 44. } 45. 46. } 4 4 e g a P
CONTACT US: Mobile: +91- 8885 25 26 27 +91- 7207 21 24 27/28 US NUM: 4433326786
Mail ID:
[email protected]
WEBSITE:
www.durgasoftonline.com
FLAT NO: NO: 202, HMDA MYTRIVANUM, AMEERPET, HYDERABAD.,