Enterprise Edition (Java EE) 5

treeexcellentΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 6 μήνες)

155 εμφανίσεις

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
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
3
Agenda
Introduction
Hibernate vs. JPA
Migrating Persistence
Spring Beans vs. EJB 3
Migrating Business Logic
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
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.
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
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
8
Agenda
Introduction
Hibernate vs. JPA
Migrating Persistence
Spring Beans vs. EJB 3
Migration Business Logic
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
10
Roots of Java Persistence API
Features and Restrictions
Hibernate
JPA
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
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
JPA
Hibernate
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, ...
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
15
Agenda
Introduction
Hibernate vs. JPA
Migrating Persistence
Spring Beans vs. EJB 3
Migrating Business Logic
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
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
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
19
Hibernate/Annotations
-
Configure SessionFactory for Annotations
application context xml:
<bean id="efsSessionFactory“
class="
AnnotationSessionFactoryBean
">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="
annotatedClasses
">
<list>
<value>com.sap...Organization</value>
...
</list>
</property>
<property name="
annotatedPackages
">
<list>
<value>com.sap.efs.model</value>
</list>
</property>
...
</bean>
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
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

Hibernate
Template
-
>
JPA

Resources/Transactions :
Spring

Integration Technology:
Spring
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)
}
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
23
Spring/JPA
-
Declare the Persistence Unit

persistence.xml:
<persistence ... >
<persistence
-
unit name="
EFSApplication

transaction
-
type="RESOURCE_LOCAL"
>
<properties>
<property name="hibernate.dialect“
value=
"org.hibernate.dialect.SAPDBDialect"/>
</properties>
</persistence
-
unit>
</persistence>
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
25
Spring/JPA
-
Configure Spring for JPA
<bean id="entityManagerFactory"
class="
LocalContainerEntityManagerFactoryBean
">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName“
value="
EFSApplication
"/>
...
</bean>
<bean id="transactionManager“
class="
JpaTransactionManager
">
<property name="entityManagerFactory“
ref="entityManagerFactory" />
</bean>
<bean
class="PersistenceAnnotationBeanPostProcessor"/>
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
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
28
Spring/JPA

Bind/Lookup EntityManager in JNDI

web.xml:
<persistence
-
context
-
ref>
<persistence
-
context
-
ref
-
name>
em/EFS
</persistence
-
context
-
ref
-
name>
<persistence
-
unit
-
name>
EFSApplication
</persistence
-
unit
-
name>
</persistence
-
context
-
ref>

application context xml:
<bean
class="
PersistenceAnnotationBeanPostProcessor
">
<property name="persistenceContexts">
<map>
<entry key="
EFSApplication
" value="
em/EFS
"/>
</map>
</property>
</bean>
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
29
Spring/JPA
-
Switch Transaction Type to JTA

persistence.xml:
<persistence ... >
<persistence
-
unit name="EFSApplication“
transaction
-
type="JTA"
>
...
</persistence
-
unit>
</persistence>

application context xml:
<bean id="transactionManager"
class="
JtaTransactionManager
">
<property name="transactionManagerName">
<value>TransactionManager</value>
</property>
</bean>
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
31
Agenda
Introduction
Hibernate vs. JPA
Migrating Persistence
Spring Beans vs. EJB 3
Migrating Business Logic
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! ;
-
)
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
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
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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
36
Agenda
Introduction
Hibernate vs. JPA
Migrating Persistence
Spring Beans vs. EJB 3
Migrating Business Logic
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
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)
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
39
Spring Beans to EJB Session Beans
Repackaging
Web
Services
Data access
Entities (JPA)
web archive
web archive
ejb archive
Web
Services
Data access
Entities (JPA)
ear archive
Libraries
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
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!
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
42
For More Information

Other sessions at
Java
One

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
2007 JavaOne
SM
Conference | Session BOF
-
4181 |
43
Q&A