Presentation is loading. Please wait.

Presentation is loading. Please wait.

2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian.

Similar presentations


Presentation on theme: "2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian."— Presentation transcript:

1 2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian Görler & Robin de Silva Jayasinghe SAP AG www.sap.com YOUR LOGO HERE

2 2007 JavaOne SM Conference | Session BOF-4181 | 2 Goal of our BOF-Session Learn about possible migration paths from Spring/Hibernate to Java EE 5

3 2007 JavaOne SM Conference | Session BOF-4181 | 3 Agenda Introduction Hibernate vs. JPA Migrating Persistence Spring Beans vs. EJB 3 Migrating Business Logic

4 2007 JavaOne SM Conference | Session BOF-4181 | 4 J2EE 1.4 Widely adopted standard environment for Enterprise Java Many implementations by different vendors But: Complex to use & implement Heavy-weight persistence model (EJB CMP) Invasive Boilerplate coding

5 2007 JavaOne SM Conference | Session BOF-4181 | 5 Rise of Spring and Hibernate Spring Application framework Dependency Injection AOP support Non-invasiveness as a basic principle O/R persistence support for various frameworks Hibernate POJO based O/R persistence Spring and Hibernate have become a popular alternative to J2EE development.

6 2007 JavaOne SM Conference | Session BOF-4181 | 6 Java EE 5 Major goal: Ease of development Adopted patterns from the community: POJO based persistence (JPA 1.0) IoC (Resource Injection) Limited AOP capability (EJB Interceptors) Reduced configuration effort by enhanced defaulting. Emphasizes on configuration via annotations XML still possible

7 2007 JavaOne SM Conference | Session BOF-4181 | 7 Motivation for a Migration Acceptance of Java EE 5 in the community Support of Java EE 5 by major platform vendors Fully exploit the features of your Java EE 5 server Adopt the new JPA standard Have a portable persistence layer Independence off 3rd party libraries

8 2007 JavaOne SM Conference | Session BOF-4181 | 8 Agenda Introduction Hibernate vs. JPA Migrating Persistence Spring Beans vs. EJB 3 Migration Business Logic

9 2007 JavaOne SM Conference | Session BOF-4181 | 9 Hibernate vs. Java Persistence API JPA 1.0 Part of Java EE 5 Basic but complete persistence solution Integrates very smoothly with EJB 3 Pluggable persistence provider Hibernate 3 Open source Very capable, solid and mature

10 2007 JavaOne SM Conference | Session BOF-4181 | 10 Roots of Java Persistence API Features and Restrictions Hibernate JDO Toplink JPA

11 2007 JavaOne SM Conference | Session BOF-4181 | 11 Hibernate vs. JPA: Mapping Java Persistence API standardized object-relational mapping common ground (incl. relationships, inheritance,...) annotations or XML (orm.xml) Hibernate XML (*.hbm.xml) very powerful and flexible Hibernate Annotations bridge between Hibernate and JPA makes Hibernate understand JPA annotations Hibernate-specific extensions

12 2007 JavaOne SM Conference | Session BOF-4181 | 12 Hibernate vs. JPA: API persist getReference refresh find no equivalent no equivalent merge save load(Class, id) load (Object, id) get saveOrUpdate/update evict merge JPAHibernate

13 2007 JavaOne SM Conference | Session BOF-4181 | 13 Hibernate vs. JPA: Queries Common Features very similar SQL-like query languages Named, dynamic and native (SQL) queries Java Persistence Query Language (JPQL) Less prone to portability issues Hibernate Query Language (HQL) Fully exploits database Bridge: Hibernate understands both HQL and JPQL Hibernate beyond JPA: API Query.iterate, Scrollable iteration, Criteria,...

14 2007 JavaOne SM Conference | Session BOF-4181 | 14 Hibernate to JPA: Migration Options JPA with default Persistence Provider Hardly any Hibernate-specific Mapping Uses only a subset of the API covered by JPA JPA with Hibernate EM as Persistence Provider Hibernate-specific Mapping Uses only a subset of the API covered by JPA partly sacrifices standard-adoption depends on different third-party libs

15 2007 JavaOne SM Conference | Session BOF-4181 | 15 Agenda Introduction Hibernate vs. JPA Migrating Persistence Spring Beans vs. EJB 3 Migrating Business Logic

16 2007 JavaOne SM Conference | Session BOF-4181 | 16 Persistent Classes DB UI Spring Local Session FactoryBean Tx Hibernate TxManager Spring Beans Business Layer DAO Layer HibernateSession *.hmb.xml Spring Hibernate Java EE Initial Application

17 2007 JavaOne SM Conference | Session BOF-4181 | 17 Migration Step 1: Mapping: Hibernate/Annotations Mapping: Hibernate -> JPA Hibernate XML -> JPA Annotations Data Access API: Hibernate Resources/Transactions : Spring Integration Technology: Spring

18 2007 JavaOne SM Conference | Session BOF-4181 | 18 Hibernate/Annotations - Convert the model to JPA Evaluate applicability: Can mappings be expressed using JPA annotations? Declare mapping of entities using JPA annotations: @Entity public class Customer { @Id int id; String name; } Remove *.hbm.xml files

19 2007 JavaOne SM Conference | Session BOF-4181 | 19 Hibernate/Annotations - Configure SessionFactory for Annotations application context xml: com.sap...Organization... com.sap.efs.model...

20 2007 JavaOne SM Conference | Session BOF-4181 | 20 Entities DB UI Spring Annotations Session FactoryBean Tx Hibernate TxManager Spring Beans Business Layer DAO Layer HibernateSession JPA Annotations Spring Hibernate Java EE Hibernate Annotations

21 2007 JavaOne SM Conference | Session BOF-4181 | 21 Migration Step 2: Data Access Layer: Spring/JPA Mapping: JPA Data Access API: Hibernate -> JPA SessionFactory -> EntityManagerFactory HibernateTransactionManager -> JpaTransactionManager HibernateTemplate -> JPA Resources/Transactions : Spring Integration Technology: Spring

22 2007 JavaOne SM Conference | Session BOF-4181 | 22 Spring/JPA - Convert the DAO Layer to JPA Evaluate applicability: Can Hibernate API be replaced by JPA? Use JPA annotations in your business logic: @PersistenceContext (unitName="EFSApplication") EntityManager em;... Customer findCustomer(int id) { return em.find(Customer.class, id) }

23 2007 JavaOne SM Conference | Session BOF-4181 | 23 Spring/JPA - Declare the Persistence Unit persistence.xml:

24 2007 JavaOne SM Conference | Session BOF-4181 | 24 Spring/JPA - Configuration Options LocalEntityManagerFactoryBean Application perspective EMF created using javax.persistence.Persistence little control LocalContainerEntityManagerFactoryBean JPA Container Perspective EMF created using javax.persistence.spi.PersistenceProvider full control EntityMangerFactory or EntityManager from JNDI

25 2007 JavaOne SM Conference | Session BOF-4181 | 25 Spring/JPA - Configure Spring for JPA...

26 2007 JavaOne SM Conference | Session BOF-4181 | 26 Entities DB UI Spring EntityManager FactoryBean Tx Jpa TxManager Spring Beans Business Layer DAO Layer EntityManager JPA Annotations Spring Hibernate Java EE Spring & JPA

27 2007 JavaOne SM Conference | Session BOF-4181 | 27 Migration Step 3: Resources/Transactions Mapping: JPA Data Access API: JPA Resources/Transactions: Java EE/JTA Resource Creation: Spring -> Java EE Resource Management: Spring -> Java EE Transactions: Local -> JTA Integration Technology: Spring

28 2007 JavaOne SM Conference | Session BOF-4181 | 28 Spring/JPA – Bind/Lookup EntityManager in JNDI web.xml: em/EFS EFSApplication application context xml:

29 2007 JavaOne SM Conference | Session BOF-4181 | 29 Spring/JPA - Switch Transaction Type to JTA persistence.xml:... application context xml: TransactionManager

30 2007 JavaOne SM Conference | Session BOF-4181 | 30 Entities DB UI Spring Tx Spring Beans Business Layer DAO Layer JPA Annotations Java EE JNDI JTA EJB3 JPA Container EntityManager JPA PostProcessor Jta TxManager Spring Hibernate Java EE Backed by Java EE

31 2007 JavaOne SM Conference | Session BOF-4181 | 31 Agenda Introduction Hibernate vs. JPA Migrating Persistence Spring Beans vs. EJB 3 Migrating Business Logic

32 2007 JavaOne SM Conference | Session BOF-4181 | 32 Spring vs. EJB 3 Evaluate if migration-step is evaluable. Following areas have to be analyzed: Declarative transactions Aspect-oriented Programming (AOP) Inversion of Control (IoC) If all is applicable: Remove the middleman! ;-)

33 2007 JavaOne SM Conference | Session BOF-4181 | 33 AOP Support EJB 3 Interceptors Pointcut declaration with interceptor-binding Per class/method and default interceptors One Joinpoint definition (@AroundInvoke) Spring AOP Flexible pointcut-expression language Joinpoint-types

34 2007 JavaOne SM Conference | Session BOF-4181 | 34 Declarative Transactions EJB 3.0 CMT JTA Global transactions only Spring transactions-management-framework Pluggable transaction-managers Global & local transactions

35 2007 JavaOne SM Conference | Session BOF-4181 | 35 Inversion of Control EJB 3 Resource Injection Defined injectable types and targets Configuration via annotations (and XML) Spring IoC Fully configurable Dependency Injection Configuration is done via XML-configuartion

36 2007 JavaOne SM Conference | Session BOF-4181 | 36 Agenda Introduction Hibernate vs. JPA Migrating Persistence Spring Beans vs. EJB 3 Migrating Business Logic

37 2007 JavaOne SM Conference | Session BOF-4181 | 37 Migration Step 4: Business Layer Mapping: JPA Data Access API: JPA Resources/Transactions: Java EE/JTA Integration Technology: EJB 3 pure Web Application -> EAR Spring Beans -> EJB 3 Session Beans

38 2007 JavaOne SM Conference | Session BOF-4181 | 38 Spring Beans to EJB Session Beans Prerequisites Prerequisite Java EE 5 compatible dependencies only Beans don‘t depend on any Spring API JPA-DAOs should use plain JPA Beans without Spring dependecies can be directly used as EJB 3 sessionbeans. Spring transaction-declaration has to be migrated to EJBs counterpart. @Transactional --> @Stateless(ful)

39 2007 JavaOne SM Conference | Session BOF-4181 | 39 Spring Beans to EJB Session Beans Repackaging Web Services Data access Entities (JPA) web archive ejb archive Web Services Data access Entities (JPA) ear archive Libraries

40 2007 JavaOne SM Conference | Session BOF-4181 | 40 Entities DB UI Java EE JPA Tx JTA EJB3 Business Layer DAO Layer EntityManager JPA Annotations Spring Hibernate Java EE Migration Result

41 2007 JavaOne SM Conference | Session BOF-4181 | 41 Conclusion Spring/Hibernate and Java EE 5 address same issues and have many commonalities. An application can be migrated in a controlled fashion with fully functional intermediate steps. Most work is done by adapting configuration and refactoring the data access layer. Give it a try!

42 2007 JavaOne SM Conference | Session BOF-4181 | 42 For More Information Other sessions at JavaOne TS-4902 - Java Persistence API: Best Practices and Tips TS-4568 - Java Persistence API: Portability Do’s and Don’ts TS-4721 - Implementing Java EE Applications, Using Enterprise JavaBeans (EJB) 3 Technology: Real-World Tips, Tricks, and New Design Patterns TS-43350 - Harnessing the Power of Java Platform, Enterprise Edition (Java EE) Technology With Spring TS-4945 - Java Persistence 2.0 Pro EJB 3: Java Persistence API Java Persistence Migration Guide Java Persistence with Hibernate

43 2007 JavaOne SM Conference | Session BOF-4181 | 43 Q&A

44 2007 JavaOne SM Conference | Session BOF-4181 | 44 Spring JPA managed by Spring LocalContainerEMFBean Spring Hibernate Java EE LocalContainer EMFBean JPA Persistence- Provider applicationContext.xml (Spring-Config) applicationContext.xml (Spring-Config) persistence.xml (JPA-Config) persistence.xml (JPA-Config) JPA EMF

45 2007 JavaOne SM Conference | Session BOF-4181 | 45 Hibernate beyond JPA: Tuning Different Fetching Strategies Custom SQL Second Level Caches

46 2007 JavaOne SM Conference | Session BOF-4181 | 46 JPA managed by Java EE 5 Server JPA Persistence- Provider JPA Persistence- Provider JNDI Spring Beans Spring Hibernate Java EE JPA EM/EMF Java EE 5 Spring

47 2007 JavaOne SM Conference | Session BOF-4181 | 47 Spring JPA managed by Spring LocalEMFBean LocalEntiyManager FactoryBean JPA Persistence- Provider JPA EMF applicationContext.xml (Spring-Config) applicationContext.xml (Spring-Config) persistence.xml (JPA-Config) persistence.xml (JPA-Config) Spring Hibernate Java EE javax.persistence. Persistence

48 2007 JavaOne SM Conference | Session BOF-4181 | 48 Hibernate vs. JPA: Mapping (II) JPA-incompatible Mappings in Hibernate Custom value types More id Generators Collections of values Indexed collections (lists, arrays) Association mapping more flexible Nested components...


Download ppt "2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian."

Similar presentations


Ads by Google