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
Ad

More Related Content

What's hot (20)

Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
Harshit Choudhary
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
Cheng Ta Yeh
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
Arturs Drozdovs
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
Brett Meyer
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
Akshay Ballarpure
 
Spring Boot
Spring BootSpring Boot
Spring Boot
Jiayun Zhou
 
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Spring annotations notes
Spring annotations notesSpring annotations notes
Spring annotations notes
Vipul Singh
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
Staples
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
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
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in Java
InnovationM
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
Anuj Singh Rajput
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
Arturs Drozdovs
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
Aneega
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Spring Framework - AOP
Spring Framework - AOPSpring Framework - AOP
Spring Framework - AOP
Dzmitry Naskou
 
ORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate OverviewORM, JPA, & Hibernate Overview
ORM, JPA, & Hibernate Overview
Brett Meyer
 
Hibernate
HibernateHibernate
Hibernate
Ajay K
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
Joshua Long
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Spring annotations notes
Spring annotations notesSpring annotations notes
Spring annotations notes
Vipul Singh
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
Staples
 
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
 
Serialization & De-serialization in Java
Serialization & De-serialization in JavaSerialization & De-serialization in Java
Serialization & De-serialization in Java
InnovationM
 
Spring Boot and REST API
Spring Boot and REST APISpring Boot and REST API
Spring Boot and REST API
07.pallav
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
Jeevesh Pandey
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
Anuj Singh Rajput
 

Viewers also liked (20)

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 Introduction
Ranjan 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 JMS
Bruce 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 POJOs
Matt 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 hibernate
hr1383
 
Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQ
Geert Pante
 
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 Introduction
Ranjan Kumar
 
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 JMS
Bruce 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 POJOs
Matt Stine
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
Thomas Wöhlke
 
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 hibernate
hr1383
 
Ad

Similar to jpa-hibernate-presentation (20)

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çı
 
Java EE and Glassfish
Java EE and GlassfishJava EE and Glassfish
Java EE and Glassfish
Carol McDonald
 
Hibernate
HibernateHibernate
Hibernate
Shaharyar khan
 
Lift Framework
Lift FrameworkLift Framework
Lift Framework
Jeffrey Groneberg
 
MyBatis
MyBatisMyBatis
MyBatis
Roman Dovgan
 
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 Jdbc
Sam 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
Hibernate
Harish Patil ( ハリシュパテイル)
 
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
 
ReactJS.ppt
ReactJS.pptReactJS.ppt
ReactJS.ppt
MOMEKEMKUEFOUETDUREL
 
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 Implementation
Mert Çalışkan
 
Hibernate
HibernateHibernate
Hibernate
Harish Patil ( ハリシュパテイル)
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
Asya Dudnik
 
Web&java. jsp
Web&java. jspWeb&java. jsp
Web&java. jsp
Asya Dudnik
 
Ad

jpa-hibernate-presentation

  • 1. JPA and Hibernate J. Slick Computer Science Corp., General Dynamics Electric Boat [email protected]
  • 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”