Скачать презентацию
Идет загрузка презентации. Пожалуйста, подождите
Презентация была опубликована 9 лет назад пользователемБогдан Шубин
1 Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 6 – ORM Support
2 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 3 Spring :: ORM ORM - Object-relational mapping connects concept of relational database and object-oriented system
4 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 5 Spring :: ORM ORM - Object-relational mapping
6 6 Spring :: Benefits of Working with ORM Easier testing; Exceptions handling; General resource management (DataSource, mappings); Integrated transaction management ;
7 7 Spring :: ORM :: Example BOOK ID: integer TITLE : varchar COMMENT : varchar DATE_RELEASE : timestamp AUTHOR_ID: integer
8 8 Spring :: ORM :: Example Implementation with use of Spring JDBC
9 9 Spring :: ORM :: Example BOOK ID: integer TITLE : varchar DATE_RELEASE : timestamp
10 10 Spring :: ORM :: Example
@Service – for business logic " title="11 Spring :: ORM :: Stereotypes @Repository – for DAO beans translates checked exceptions to DataAccessException hierarchy
12 12 Spring :: ORM :: Example
13 13 Spring :: ORM :: Universal DAO
14 14 Spring :: ORM :: Universal DAO
15 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 16 Spring :: ORM :: JPA
17 17 Spring :: ORM :: JPA
18 18 Spring :: ORM :: JPA
19 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 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 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
22 Spring :: JPA Setup Obtaining EntityManagerFactory from JNDI:
23 23 Spring :: JPA Setup Using LocalEntityManagerFactoryBean:
24
24 Spring :: JPA Setup LocalContainerEntityManagerFactoryBean is a factory that gives full control:
META-INF/persistence.xml: lab.mod" title="25 Spring :: JPA Setup If using Hibernate 4 as JPA provider, an additional configuration is needed. application-context.xml:
26 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 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 28 Injection of EntityManagerFactory example: Spring :: JPA initialization
29 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 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 31 Spring :: JPA initialization EntityManager injection example (Container Managed Entity Manager):
32 32 Exercises : 7 : Using ORM in Spring when handling data – 45 min for practice; – 15 min for discussion;
33 33 Any questions!?
Еще похожие презентации в нашем архиве:
© 2024 MyShared Inc.
All rights reserved.