Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 6 – ORM Support.

Презентация:



Advertisements
Похожие презентации
Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 4 – DAO, JDBC.
Advertisements

1 © Luxoft Training 2013 Spring Framework Module 10 JMS & EJB.
Evgeniy Krivosheev Andrey Stukalenko Vyacheslav Yakovenko Last update: Nov, 2013 Spring Framework Module 1 - Introduction.
Evgeniy Krivosheev Vyacheslav Yakovenko Last update: Feb, 2012 Spring Framework Module 4 – JNDI.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
Hibernate&EJB3 Studybook For students of university Author Sr.Java developer Dudnik Oxana.
© 2002 IBM Corporation Confidential | Date | Other Information, if necessary © Wind River Systems, released under EPL 1.0. All logos are TM of their respective.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Route Selection Using Policy Controls Using Multihomed BGP Networks.
© 2006 Cisco Systems, Inc. All rights reserved.BCMSN v Defining VLANs Correcting Common VLAN Configuration Errors.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
Lecture # Computer Architecture Computer Architecture = ISA + MO ISA stands for instruction set architecture is a logical view of computer system.
© 2009 Avaya Inc. All rights reserved.1 Chapter Two, Voic Pro Components Module Two – Actions, Variables & Conditions.
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Managing Your Network Environment Managing Cisco Devices.
Mobility Control and one-X Mobile. Mobility Control User Configuration Mobile Call Control requires PRI-U, BRI or SIP (RFC2833) trunks in the IP Office.
Linux Daemons. Agenda What is a daemon What is a daemon What Is It Going To Do? What Is It Going To Do? How much interaction How much interaction Basic.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v Integrating Internet Access with MPLS VPNs Implementing Internet Access as a Separate VPN.
© 2006 Avaya Inc. All rights reserved. Using File Management and SD-Card Handling.
© 2009 Avaya Inc. All rights reserved.1 Chapter Four, UMS Web Services Module Three – Exchange 2007.
Troubleshooting CallManager, Network Signaling and Dial Plan © 2004 Cisco Systems, Inc. All rights reserved. IPTT v Troubleshooting CallManager CTI,
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Module Summary The Cisco Discovery Protocol is an information-gathering tool used by network.
Транксрипт:

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!?