Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 6 – ORM Support
2 Contents Overview of ORM module Overview of Deprecated API Benefits of Working with ORM when using Spring JPA + Hibernate JPA Setup EntityManagerFactory; Query example with JPA EntityManagerFactory;
3 Spring :: ORM ORM - Object-relational mapping connects concept of relational database and object-oriented system
4 Spring :: Overview of ORM module org.springframework.orm org.springframework.orm.hibernate3 org.springframework.orm.hibernate4 org.springframework.orm.ibatis org.springframework.orm.jdo org.springframework.orm.jpa JPA
5 Spring :: ORM ORM - Object-relational mapping
6 Spring :: Benefits of Working with ORM Easier testing; Exceptions handling; General resource management (DataSource, mappings); Integrated transaction management ;
7 Spring :: ORM :: Example BOOK ID: integer TITLE : varchar COMMENT : varchar DATE_RELEASE : timestamp AUTHOR_ID: integer
8 Spring :: ORM :: Example Implementation with use of Spring JDBC
9 Spring :: ORM :: Example BOOK ID: integer TITLE : varchar DATE_RELEASE : timestamp
10 Spring :: ORM :: Example
11 Spring :: ORM :: – for DAO beans translates checked exceptions to DataAccessException hierarchy <bean class="org.springframework.dao.annotation. – for business logic – for controllers Stereotype is a marker annotation denoting the role of the bean in the overall architecture (at a conceptual, rather than implementation, level). Intended for use by AOP and various tools.
12 Spring :: ORM :: Example
13 Spring :: ORM :: Universal DAO
14 Spring :: ORM :: Universal DAO
15 Spring :: ORM :: JPA Java Persistence API (JPA) API included to Java SE и Java EE since Java 5 Supports the persistency of JPA covers these areas: API, заданный в пакете javax.persistence; Java Persistence Query Language; metainformation. generation of DDL for entities
16 Spring :: ORM :: JPA
17 Spring :: ORM :: JPA
18 Spring :: ORM :: JPA
19 Spring :: ORM: Overview of Deprecated API Spring Framework v.2.* supported ORM through XxxTemplate JpaCallback JdoCallback ; ; org.springframework.orm.hibernate3.HibernateTemplate ; Trend of Spring Framework v.3 is moving away from XxxTemplate and switching to the most native API, particular ORM: JPA: LocalEntityManagerFactoryBean LocalContainerEntityManagerFactoryBean Hibernate: org.springframework.orm.hibernate3.LocalSessionFactoryBean org.springframework.orm.hibernate4.LocalSessionFactoryBean
20 Spring :: JPA + Hibernate Nowadays JPA is a commercial standard, while Hibernate (as of v.3.2) is a JPA implementation. Therefore, during the training we will examine this alternative: using Hibernate 4 as JPA 2.0 provider; Please note that next Spring Framework versions will not support JPA v.1.0; Besides, Spring Framework starting from v.3.0 doesnt support Hibernate versions below 3.2;
21 Spring :: JPA Setup Currently Spring offers three ways of setting up JPA EntityManagerFactory : Obtaining an EntityManagerFactory from JNDI; Using LocalEntityManagerFactoryBean: Persistence.xml that is mandatory from JPA standard point of view is not required; Used in simple applications and prototypes for testing; LocalContainerEntityManagerFactoryBean is a factory that gives full control: Supports multiple persistence units; Creates EntityManager manages by container; Can be configured for various application servers (WebLogic, OC4J, GlassFish, Tomcat, Resin, JBoss)
22 Spring :: JPA Setup Obtaining EntityManagerFactory from JNDI: <jee:jndi-lookup id="myEmf" jndi-name="persistence/myPersistenceUnit"/>
23 Spring :: JPA Setup Using LocalEntityManagerFactoryBean:
24 Spring :: JPA Setup LocalContainerEntityManagerFactoryBean is a factory that gives full control: <bean class="org.springframework.instrument. classloading.InstrumentationLoadTimeWeaver"/>
25 Spring :: JPA Setup If using Hibernate 4 as JPA provider, an additional configuration is needed. application-context.xml: <bean class="org.springframework.instrument. classloading.InstrumentationLoadTimeWeaver" /> META-INF/persistence.xml: lab.model.Country
26 Spring :: JPA, Query Example Weaving public class CountryJpaDaoImpl { protected EntityManagerFactory public void setEntityManagerFactory(EntityManagerFactory emf) { this.emf = emf; } public List getAllCountries() { EntityManager em = emf.createEntityManager(); return = em.createQuery("from Country", Country.class); }
27 Spring :: JPA initialization However since Spring 3.1+ we can avoid defining of persistence.xml by turning on the auto-scanning of all classes of package packagesToScan:
28 Injection of EntityManagerFactory example: Spring :: JPA initialization
29 Spring :: JPA, Query Example Weaving is performed through method annotated Spring calls LocalContainerEntityManagerFactoryBean; Obtains EntityManagerFactory; Using autoweaving mechanism injects into DAO implementation; When you have one EntityManagerFactory instance, call it for executing queries.
30 Spring :: JPA initialization Application Managed Entity Manager EntityManager, injected Used in JSE applications for Java Persistence API (JPA) - not thread-safe - creating everytime - should be closed after use - manual transaction management is needed Container Managed Entity Manager EntityManager injected -thread-safe -transactional proxy - has the same behavor as EntityManager received via JNDI
31 Spring :: JPA initialization EntityManager injection example (Container Managed Entity Manager):
32 Exercises : 7 : Using ORM in Spring when handling data – 45 min for practice; – 15 min for discussion;
33 Any questions!?