SlideShare uma empresa Scribd logo
1 de 25
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

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of ControlJava Spring framework, Dependency Injection, DI, IoC, Inversion of Control
Java Spring framework, Dependency Injection, DI, IoC, Inversion of Control
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Hibernate
HibernateHibernate
Hibernate
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Lets make a better react form
Lets make a better react formLets make a better react form
Lets make a better react form
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring User Guide
Spring User GuideSpring User Guide
Spring User Guide
 
Spring mvc
Spring mvcSpring mvc
Spring mvc
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
REST APIs with Spring
REST APIs with SpringREST APIs with Spring
REST APIs with Spring
 
React for Beginners
React for BeginnersReact for Beginners
React for Beginners
 
Jsp presentation
Jsp presentationJsp presentation
Jsp presentation
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 

Destaque

Spring JMS and ActiveMQ
Spring JMS and ActiveMQSpring JMS and ActiveMQ
Spring JMS and ActiveMQGeert Pante
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring FrameworkRaveendra R
 
Hibernate an introduction
Hibernate   an introductionHibernate   an introduction
Hibernate an introductionjoseluismms
 
02 Hibernate Introduction
02 Hibernate Introduction02 Hibernate Introduction
02 Hibernate IntroductionRanjan 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 IntroductionNguyen Cao
 
Introduction To Hibernate
Introduction To HibernateIntroduction To Hibernate
Introduction To Hibernateashishkulkarni
 
Hibernate presentation
Hibernate presentationHibernate presentation
Hibernate presentationManav Prasad
 
Introduction to Hibernate Framework
Introduction to Hibernate FrameworkIntroduction to Hibernate Framework
Introduction to Hibernate FrameworkRaveendra 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 APIThomas 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 OverviewCraig 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 TechniquesBrett 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 Examplekamal kotecha
 
Introduction to hibernate
Introduction to hibernateIntroduction to hibernate
Introduction to hibernatehr1383
 

Destaque (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
 

Semelhante a jpa-hibernate-presentation

Semelhante a 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”