SlideShare a Scribd company logo
JPA and Hibernate
J. Slick
Computer Science Corp., General Dynamics Electric Boat
Jmslick@gmail.Com
JPA and Hibernate
Mid 90’s:
“Object/Relational Impedance Mismatch”
Impedance:
Restriction of flow between two systems
 
JPA and Hibernate
 
Relational model and Object model do not play well together
RELATIONAL OBJECT
Data Business Logic
Tables, Rows Objects, Collections
Identity / Sameness: PK Identity: a==b, a.equals(b)
Associate by FK Associate by Reference
Navigate by join Navigate object graph
No Inheritance Inheritance, Polymorphism
JDBC Data Types Java Types
Object / Relational Impedance Mismatch
Map business objects and associations to relational tables
Solution: Object / Relational Mapping
ORM Benefits?
 Productivity, fewer LOC
 Reduce time and tedious JDBC code
 Maintainability
 Focus on OO business logic
 Hide database architecture from user
 Transparently persist objects to tables
 Encourage OO queries
 Support native SQL queries
 Buffer changes between relational and object models
 Alterations
 Providers
 Data store
Map business objects and associations to relational tables
XML Metadata
<class name=”com.jpahib.Customer” table=”Customer”>
<property name=”username” type=string”/>
. . .
</class>
Annotated POJOs
@Entity
@Table(name=“CUSTOMER”)
public class Customer {
@Column(name=”USERNAME”)
private String username;
. . .
}
ORM
 Java spec for accessing, persisting Java objects to RDBMS
 Prompted by EJB 1 & 2
 Concepts from Hibernate, Oracle TopLink, JDO, EJB
 Finalized in EJB 3.0 spec, J2EE 5, 2006
 Just a specification, cannot perform persistence
 J2EE app servers should implement
 Can be used in Java EE and SE apps
 Many JPA implementations:
Hibernate, EclipseLink, OpenJPA, BEA Koda…
What is Java Persistence API?
JPA Position
RDBMS
Object/Relational Mapping tool to resolve Impedance
Mismatch
Persistence provider implementing JPA specification
 
What is Hibernate?
Hibernate Tools
• Ant command line
• Eclipse plug-in
• Wizards
• Config files
• Map files
• Reverse Engineering
• Code generation
• Editors: configs, queries
• HQL Console
• Query Editor
• SQL Analyze
• Visual Map Diagrams
• Code Hints, Completion
Hibernate Integration
Top Down: generate POJO’s from map file or DB
RDBMS
hbm2java
*.hbm.xml (Metadata)
<class name=”com.jpahib.Customer” table=”Customer”>
<property name=”username” type=string”/>
. . .
</class>
@POJO @POJO
Hibernate Integration
Bottom up: generate schema from map file or POJOs
RDBMS
hbm2ddl
*.hbm.xml (Metadata)
<class name=”com.jpahib.Customer” table=”Customer”>
<property name=”username” type=string”/>
. . .
</class>
@POJO @POJO
JPA Integration: Persistence Unit
 Specifies entity classes, queries, provider, connection
 Identified by name
 Injected by EE container or managed by SE application
<persistence>
<persistence-unit name=“pu1">
<provider>org.hibernate.ejb.HibernatePersistenceProvider</provider>
<mapping-file>META-INF/queries.xml</mapping-file>
<jar-file>packedEntity.jar</jar-file>
<class>com.jpahib.Customer</class>
<class>com.jpahib.Address</class>
<properties>
<property name=“javax.persistence.jdbc.driver”
value=“oracle.jdbc.driver.OracleDriver”/>
<property name="javax.persistence.jdbc.user"value="admin"/>
<property name="javax.persistence.jdbc.password"value="admin"/>
<property name=“hibernate.dialect"value=“Oracle10gDialect"/>
</properties>
</persistence-unit>  
</persistence>
JPA Integration: Entity Manager
Container Managed (EE)
Persistence context propagated to application components
@PersistenceContext
EntityManager em;
em.find(), em.persist(T), em.remove(T), etc…
Application Managed (SE)
Persistence context from javax.persistence.Persistence
EntityManagerFactory emf = Persistence.createEntityManagerFactory(“pu1”);
EntityManager em = emf.createEntityManager();
em.find(), em.persist(T), em.remove(T), etc…
CRUD
Insert
SQL / JDBC
String sql = "INSERT INTO CUSTOMER (USER_ID, USERNAME, ADDRESS) "
+ "VALUES(1, 'Joe Sixpack', '123 North St.')";
statement = dbConnection.createStatement();
statement.execute (sql);
JPA
Customer c = new Customer(1, “Joe Sixpack”, “123 North St.”);
entityManager.persist(c);
Read
SQL / JDBC
PreparedStatement ps = con.prepareStatement("select customer_id, name,
address, from customer");
ResultSet result = ps.executeQuery();
while(result.next()) {
Customer cust = new Customer();
cust.setCustomerID(result.getLong("customer_id"));
cust.setName(result.getString("name"));
cust.setAddress(result.getString("address"));
list.add(cust);
}
return list;
JPA
TypedQuery<Customer> q =
entityManager.createTypedQuery(“select * from customer”, Customer.class);
return q.getResultList();
Update
SQL / JDBC
String sql = "UPDATE CUSTOMER “
+ “SET USERNAME = ‘Ginger Vitys’, ADDRESS = ‘321 South St.’ “
+ “WHERE USER_ID = 1”;
statement = dbConnection.createStatement();
statement.execute (sql);
JPA / Hibernate
Customer c = entityManager.find(Customer.class, 1);
c.setName(“Ginger Vitys”);
c.setAddress(“123 North South St.”);
entityManager.merge(c);
Delete
SQL / JDBC
String sql = "DELETE FROM CUSTOMER WHERE USER_ID = 1”;
statement = dbConnection.createStatement();
statement.execute (sql);
JPA / Hibernate
Customer c = entityManager.find(Customer.class, 1);
entityManager.remove(c);
Short Case Study
Case Study
• Port legacy FoxPro data to Oracle, optimize, export to JavaDB
• Write new Java web app
• Eight seasoned remote dev’s no JPA/Hibernate experience
• Single code base
• 217 Classes
• 153 Entities
• 107 Pages
• 96 Named queries
• Run application on shore (Oracle) and at sea (JavaDB)
• Oracle and JavaDB dialect differences
• Reduced functionality at sea, read-only DB, fewer pages
• Change persistence unit with single word: “shore” or “boat”
Case Study
jpa-hibernate-presentation

More Related Content

What's hot

Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
VMware Tanzu
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
Anuj Singh Rajput
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
Rasheed Waraich
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
tola99
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
Naphachara Rattanawilai
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
Jonathan Holloway
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
Carol McDonald
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplateGuo Albert
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
Vinay Kumar
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Knoldus Inc.
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jaran Flaath
 

What's hot (20)

Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
Jpa
JpaJpa
Jpa
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
 
Spring framework Introduction
Spring framework IntroductionSpring framework Introduction
Spring framework Introduction
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
 
Hibernate
HibernateHibernate
Hibernate
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Spring Boot Tutorial
Spring Boot TutorialSpring Boot Tutorial
Spring Boot Tutorial
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 
Spring JDBCTemplate
Spring JDBCTemplateSpring JDBCTemplate
Spring JDBCTemplate
 
Spring framework in depth
Spring framework in depthSpring framework in depth
Spring framework in depth
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 

Viewers also liked

Spring JMS
Spring JMSSpring JMS
Spring JMS
Emprovise
 
Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQ
Geert Pante
 
Hibernate
HibernateHibernate
Hibernate
reddivarihareesh
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Raveendra R
 
Hibernate an introduction
Hibernate   an introductionHibernate   an introduction
Hibernate an introduction
joseluismms
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate IntroductionRanjan Kumar
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
Collaboration Technologies
 
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS  +27631229624 THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS  +27631229624
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
mamaalphah alpha
 
Hibernate An Introduction
Hibernate An IntroductionHibernate An Introduction
Hibernate An Introduction
Nguyen Cao
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernate
ashishkulkarni
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
Manav Prasad
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
Raveendra R
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSBruce Snyder
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsMatt Stine
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
Thomas Wöhlke
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
Muhammad Zeeshan
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
Craig Dickson
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
Brett Meyer
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernatehr1383
 

Viewers also liked (20)

Spring JMS
Spring JMSSpring JMS
Spring JMS
 
Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQ
 
Hibernate
HibernateHibernate
Hibernate
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Hibernate an introduction
Hibernate   an introductionHibernate   an introduction
Hibernate an introduction
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate Introduction
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS  +27631229624 THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS  +27631229624
THE WORLDS NO.1 BLACK MAGIC EXPERT WITH POWERFUL LOVE SPELLS +27631229624
 
Hibernate An Introduction
Hibernate An IntroductionHibernate An Introduction
Hibernate An Introduction
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernate
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentation
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate Framework
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMS
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief OverviewJava Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernate
 

Similar to jpa-hibernate-presentation

Hibernate
HibernateHibernate
Hibernate
Prashant Kalkar
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
Onkar Deshpande
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
Talha Ocakçı
 
Hibernate
HibernateHibernate
Hibernate
Shaharyar khan
 
Lift Framework
Lift FrameworkLift Framework
Lift Framework
Jeffrey Groneberg
 
JUG Berlin Brandenburg: What's new in Java EE 7?
JUG Berlin Brandenburg: What's new in Java EE 7?JUG Berlin Brandenburg: What's new in Java EE 7?
JUG Berlin Brandenburg: What's new in Java EE 7?
gedoplan
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance JdbcSam Pattsin
 
Rails and alternative ORMs
Rails and alternative ORMsRails and alternative ORMs
Rails and alternative ORMs
Jonathan Dahl
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
IMC Institute
 
Hibernate
HibernateHibernate
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
IMC Institute
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
Haroon Idrees
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
DrMeenakshiS
 
Enterprise Java Web Application Frameworks Sample Stack Implementation
Enterprise Java Web Application Frameworks   Sample Stack ImplementationEnterprise Java Web Application Frameworks   Sample Stack Implementation
Enterprise Java Web Application Frameworks Sample Stack ImplementationMert Çalışkan
 
Hibernate
HibernateHibernate
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
Asya Dudnik
 

Similar to jpa-hibernate-presentation (20)

Hibernate
HibernateHibernate
Hibernate
 
Hibernate in Nutshell
Hibernate in NutshellHibernate in Nutshell
Hibernate in Nutshell
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Java EE and Glassfish
Java EE and GlassfishJava EE and Glassfish
Java EE and Glassfish
 
Hibernate
HibernateHibernate
Hibernate
 
Lift Framework
Lift FrameworkLift Framework
Lift Framework
 
MyBatis
MyBatisMyBatis
MyBatis
 
JUG Berlin Brandenburg: What's new in Java EE 7?
JUG Berlin Brandenburg: What's new in Java EE 7?JUG Berlin Brandenburg: What's new in Java EE 7?
JUG Berlin Brandenburg: What's new in Java EE 7?
 
High Performance Jdbc
High Performance JdbcHigh Performance Jdbc
High Performance Jdbc
 
Rails and alternative ORMs
Rails and alternative ORMsRails and alternative ORMs
Rails and alternative ORMs
 
Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3Java Web Programming Using Cloud Platform: Module 3
Java Web Programming Using Cloud Platform: Module 3
 
Hibernate
HibernateHibernate
Hibernate
 
Java Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet AdvancedJava Web Programming [3/9] : Servlet Advanced
Java Web Programming [3/9] : Servlet Advanced
 
Spring framework part 2
Spring framework part 2Spring framework part 2
Spring framework part 2
 
ReactJS.ppt
ReactJS.pptReactJS.ppt
ReactJS.ppt
 
jdbc_presentation.ppt
jdbc_presentation.pptjdbc_presentation.ppt
jdbc_presentation.ppt
 
Enterprise Java Web Application Frameworks Sample Stack Implementation
Enterprise Java Web Application Frameworks   Sample Stack ImplementationEnterprise Java Web Application Frameworks   Sample Stack Implementation
Enterprise Java Web Application Frameworks Sample Stack Implementation
 
Hibernate
HibernateHibernate
Hibernate
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
 

jpa-hibernate-presentation

  • 1. JPA and Hibernate J. Slick Computer Science Corp., General Dynamics Electric Boat Jmslick@gmail.Com
  • 2. JPA and Hibernate Mid 90’s: “Object/Relational Impedance Mismatch”
  • 3. Impedance: Restriction of flow between two systems   JPA and Hibernate
  • 4.   Relational model and Object model do not play well together
  • 5. RELATIONAL OBJECT Data Business Logic Tables, Rows Objects, Collections Identity / Sameness: PK Identity: a==b, a.equals(b) Associate by FK Associate by Reference Navigate by join Navigate object graph No Inheritance Inheritance, Polymorphism JDBC Data Types Java Types Object / Relational Impedance Mismatch
  • 6. Map business objects and associations to relational tables Solution: Object / Relational Mapping
  • 7. ORM Benefits?  Productivity, fewer LOC  Reduce time and tedious JDBC code  Maintainability  Focus on OO business logic  Hide database architecture from user  Transparently persist objects to tables  Encourage OO queries  Support native SQL queries  Buffer changes between relational and object models  Alterations  Providers  Data store
  • 8. Map business objects and associations to relational tables XML Metadata <class name=”com.jpahib.Customer” table=”Customer”> <property name=”username” type=string”/> . . . </class> Annotated POJOs @Entity @Table(name=“CUSTOMER”) public class Customer { @Column(name=”USERNAME”) private String username; . . . } ORM
  • 9.  Java spec for accessing, persisting Java objects to RDBMS  Prompted by EJB 1 & 2  Concepts from Hibernate, Oracle TopLink, JDO, EJB  Finalized in EJB 3.0 spec, J2EE 5, 2006  Just a specification, cannot perform persistence  J2EE app servers should implement  Can be used in Java EE and SE apps  Many JPA implementations: Hibernate, EclipseLink, OpenJPA, BEA Koda… What is Java Persistence API?
  • 11. Object/Relational Mapping tool to resolve Impedance Mismatch Persistence provider implementing JPA specification   What is Hibernate?
  • 12. Hibernate Tools • Ant command line • Eclipse plug-in • Wizards • Config files • Map files • Reverse Engineering • Code generation • Editors: configs, queries • HQL Console • Query Editor • SQL Analyze • Visual Map Diagrams • Code Hints, Completion
  • 13. Hibernate Integration Top Down: generate POJO’s from map file or DB RDBMS hbm2java *.hbm.xml (Metadata) <class name=”com.jpahib.Customer” table=”Customer”> <property name=”username” type=string”/> . . . </class> @POJO @POJO
  • 14. Hibernate Integration Bottom up: generate schema from map file or POJOs RDBMS hbm2ddl *.hbm.xml (Metadata) <class name=”com.jpahib.Customer” table=”Customer”> <property name=”username” type=string”/> . . . </class> @POJO @POJO
  • 15. JPA Integration: Persistence Unit  Specifies entity classes, queries, provider, connection  Identified by name  Injected by EE container or managed by SE application <persistence> <persistence-unit name=“pu1"> <provider>org.hibernate.ejb.HibernatePersistenceProvider</provider> <mapping-file>META-INF/queries.xml</mapping-file> <jar-file>packedEntity.jar</jar-file> <class>com.jpahib.Customer</class> <class>com.jpahib.Address</class> <properties> <property name=“javax.persistence.jdbc.driver” value=“oracle.jdbc.driver.OracleDriver”/> <property name="javax.persistence.jdbc.user"value="admin"/> <property name="javax.persistence.jdbc.password"value="admin"/> <property name=“hibernate.dialect"value=“Oracle10gDialect"/> </properties> </persistence-unit>   </persistence>
  • 16. JPA Integration: Entity Manager Container Managed (EE) Persistence context propagated to application components @PersistenceContext EntityManager em; em.find(), em.persist(T), em.remove(T), etc… Application Managed (SE) Persistence context from javax.persistence.Persistence EntityManagerFactory emf = Persistence.createEntityManagerFactory(“pu1”); EntityManager em = emf.createEntityManager(); em.find(), em.persist(T), em.remove(T), etc…
  • 17. CRUD
  • 18. Insert SQL / JDBC String sql = "INSERT INTO CUSTOMER (USER_ID, USERNAME, ADDRESS) " + "VALUES(1, 'Joe Sixpack', '123 North St.')"; statement = dbConnection.createStatement(); statement.execute (sql); JPA Customer c = new Customer(1, “Joe Sixpack”, “123 North St.”); entityManager.persist(c);
  • 19. Read SQL / JDBC PreparedStatement ps = con.prepareStatement("select customer_id, name, address, from customer"); ResultSet result = ps.executeQuery(); while(result.next()) { Customer cust = new Customer(); cust.setCustomerID(result.getLong("customer_id")); cust.setName(result.getString("name")); cust.setAddress(result.getString("address")); list.add(cust); } return list; JPA TypedQuery<Customer> q = entityManager.createTypedQuery(“select * from customer”, Customer.class); return q.getResultList();
  • 20. Update SQL / JDBC String sql = "UPDATE CUSTOMER “ + “SET USERNAME = ‘Ginger Vitys’, ADDRESS = ‘321 South St.’ “ + “WHERE USER_ID = 1”; statement = dbConnection.createStatement(); statement.execute (sql); JPA / Hibernate Customer c = entityManager.find(Customer.class, 1); c.setName(“Ginger Vitys”); c.setAddress(“123 North South St.”); entityManager.merge(c);
  • 21. Delete SQL / JDBC String sql = "DELETE FROM CUSTOMER WHERE USER_ID = 1”; statement = dbConnection.createStatement(); statement.execute (sql); JPA / Hibernate Customer c = entityManager.find(Customer.class, 1); entityManager.remove(c);
  • 23. Case Study • Port legacy FoxPro data to Oracle, optimize, export to JavaDB • Write new Java web app • Eight seasoned remote dev’s no JPA/Hibernate experience • Single code base • 217 Classes • 153 Entities • 107 Pages • 96 Named queries • Run application on shore (Oracle) and at sea (JavaDB) • Oracle and JavaDB dialect differences • Reduced functionality at sea, read-only DB, fewer pages • Change persistence unit with single word: “shore” or “boat”