Enterprise Java Development using JPA, Hibernate and...
Transcript of Enterprise Java Development using JPA, Hibernate and...
Enterprise Java Development using
JPA, Hibernate and Spring
Srini Penchikala
Detroit JUG Developer Day Conference
November 14, 2009
2
About the Speaker
Enterprise Architect
Writer, Speaker, Editor (InfoQ)
Working with Java since 1996, JEE (2000), SOA (2006), & PowerPoint since July 2009
Current: Agile Architectures, Domain-Driven Design,
Architecture Enforcement, Model Driven Development, SOA
Future: Security Architecture, Role of DSL's in
Architecture Enforcement
3
Goals for this Presentation
“What” are the essential ingredients of JEE development recipe and “How” Spring Portfolio helps in implementing a JEE project
4
Format
Interactive
Demos
Duration: ~120 minutes
Discussion oriented tutorial
Not a hands-on workshop
Q & A
6
Agenda (1/2) Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
7
Agenda (2/2) Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
8
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
9
Tutorial Application (1/5)
Loan processing application
Demonstrates how to use JPA, Hibernate and
Spring in developing enterprise Java applications
Covers JEE application concerns that developers
run into in real-world applications
10
Tutorial Application (2/5)
Loan processing application
Demonstrates how to use JPA, Hibernate and
Spring in developing enterprise Java applications
JEE application concerns that developers run into in
real-world applications:
Application Security
Persistence
Transaction Management
Data Binding/ Data Validation
Web Services (REST)
11
Tutorial Application (3/5)
Tools:
JDK 1.6
Maven
Eclipse
Testing:
JUnit 4.4
Mockito
Spring Test Module
12
Tutorial Application (4/5)
Technologies (non-Spring):
JPA
Hibernate
JAXB
Jetty
Dozer
HSQL
JBossCache/EHCache
13
Tutorial Application (5/5)
Technologies (Spring):
Spring Core (IoC)
Spring AOP
Spring JPA
Spring Security
Spring Modules
14
How to build sample application
Checkout the code from Google Project
(https://enterprise-java-
spring.googlecode.com/svn/trunk)
Install JDK, Maven and Eclipse (if needed)
Modify setBaseEnv.bat file
To compile and unit test:
mvn clean compile test
To run the web application:
mvn package jetty:run
15
Agenda (1/2) Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
16
Spring Framework
Light-weight JEE middleware framework
Separation of concerns (business v. infrastructure)
POJO Based Design
Business domain first, infrastructure concerns
second
Agile Development and Testing
18
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
Why Spring (1/3)
Test Driven Development
Unit testing in isolation
Mock objects
System integration testing support
Why Spring (2/3)
Agile Software Development (Scrum)
Iterative Design, Development, Unit Testing and
Refactoring
Modular Application Architecture
POJO Programming Model
22
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
25
Typical J2EE Architecture ModelController
Facade
Data Access Object
Domain Object*
Database
Application
Data Access
Persistence
Data Store
Notes:· No real Domain Layer
· Business Logic in Façade Layer (Stateless
Session Beans)
· Direct Access to Data Access Objects
(CRUD logic is infrastructure not business)
· *Domain objects are really Persistence
classes with getters/setters
· Infrastructure concerns have overtaken/
overshadowed the domain concerns
Transaction Management
Caching
HTTP
Session Management
26
The Problem
Architects/Developers are thinking only about
infrastructure
Most of the development time is still spent on writing
plumbing code instead of real business logic
We have lost real OOD/OOP
27
Domain Driven Architecture
Controller
Facade
Domain
Data Access Object
Database
Application
Domain
Data Access/
Persistence
Data Store
Notes:· All Layers support POJO based design
· Controller and Façade layers are consumers of
Domain Classes
· Business Logic only in Domain Classes
· No direct Access to DAO’s except from Domain
Classes
· Domain First, Infrastructure Second
· Infrastructure concerns are implemented via DI,
AOP, Annotations
Infrastructure
· HTTP
· Session Management
· Remoting
· Persistence
· Caching
· Transaction Management
· Security (Authentication &
Authorization
· Asynchronous Messaging
29
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
30
Domain Driven Design
What:
Domain Driven Design (DDD) is about mapping business
domain concepts into software artifacts.
Why:
To create better software by focusing on a model of the
domain rather than the technology.
31
Advantages
Promotes high cohesion and low coupling in the
application code
Easy to test domain components
Business (domain) logic is isolated from non-
domain and infrastructure code
Adding/changing services does not influence the
domain or other services
32
Domain Elements Domain objects: Instances of real entities which hold knowledge or
activity of business domain
Entities:
Objects defined by their identity, which remains same throughout life of software (Customer, Account)
Value Objects
Immutable objects that have no identity & are used to describe an attribute of another object (Product, State lookup)
(Domain) Services
Objects that have no state & only have a defined behavior that’s not part of any domain object (FundTransfer service, Loan Interest Calculation)
33
Other DDD Elements
Module (Package):
A method of organizing related concepts and tasks in
order to reduce complexity.
Domain object Lifecycle is managed by:
Aggregate
Factory
Repository
34
Design Recipe
Object Oriented Programming (OOP)
Dependency Injection (DI)
Aspect-oriented Programming (AOP)
Annotations
35
Domain Elements and OOP
Domain Element Encapsulation
Entity,Value Object,Aggregate
State and Behavior
Data Transfer Object State only
Service, Repository Behavior only
36
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
37
Dependency Injection (DI)
aka Inversion of Control (IoC)
Based on Hollywood Principle
Decouple and manage the dependencies of the
components in the application
DI Frameworks:
Spring
Google Guice
Pico Container
38
DI Use Cases
Service
Domain Classes
Transaction Manager
Domain
Data Access Object (DAO)
DAO class
Data Source
JPA Entity Manager
39
Domain Object DI
Domain objects not instantiated by Spring IoC
container
@Configurable annotation
41
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
42
Aspect-oriented Programming
Allows developers to add behavior to objects in a
non-obtrusive manner through use of static and
dynamic crosscutting
Main goal is to code cross-cutting concerns in
separate modules and apply them in a declarative
way
43
AOP Use Cases
Spring Framework Built-In Aspects
Transaction Management
Security
Custom Aspects
Profiling
Caching
Rules
Architecture Governance/Policy Enforcement
45
Agenda Introduction
Tutorial Sample Application
Spring Framework
Why Spring?
JEE Application Architecture
Domain-Driven Design
Dependency Injection
Aspect-oriented Programming
Annotations
46
Annotations
Added in Java SE 5.0 as Java Metadata facility
(JSR 175)
Provide a way to add metadata to program
elements
Defined by nearly every recent JSR standard
Also include a mechanism for adding custom
annotations to the Java code
47
Annotations For Domain Elements
Layer Domain Element Annotation
DomainEntity,
Value Object
@Entity (JPA),
@Configurable
Domain Repository @Repository
Domain Service @Service
Application Controller @Controller
All All @Component
48
Custom Annotations
Use Cases:
Caching
Validation
Implementation options:
Reflection
Annotation Processing Tool (APT)
Byte-code Instrumentation (Javassist)
Aspects/AOP (AspectJ and Spring AOP)
51
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
52
Application Concerns
Persistence
Transaction Management
Application Security (AuthN & AuthZ)
Caching
53
Other Application Concerns
Monitoring (JMX, Spring JMX)
Conversational Web Applications (Web Flow)
Asynchronous Messaging (JMS, MDP)
Enterprise Integration (Spring Integration)
Batch Processing (Spring Batch)
* Not covered in this presentation
54
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
55
Persistence – The Spring Way
JDBC (Spring JDBC Template)
JPA w/ Hibernate, OpenJPA, EclipseLink (TopLink)
Spring JPA Utils & Data Source DI
57
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
58
Transaction Management – The
Spring Way
Declarative transaction semantics
Similar to EJB3 transaction model and more
Spring JTA Support
Support for multiple transaction managers
JDBC
Hibernate
JTA
Transactions are managed in Service classes using
“@Transactional” annotation
60
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
61
Application Security
Spring Security Module
Declarative application security support
Fits in very well with Spring Core framework
Authentication
Authorization
URL level
Domain Object
Service Method
JDBC and LDAP Authentication Providers
JSR-250 compliant
65
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
66
Ideal Web Application Framework
Isolation from domain layer so presentation and
domain layers can evolve independently
Simple and Easy Data binding between Domain,
Controller and Presentation layers
Support for data validation that works in all layers
w/o any additional coding
Support for AJAX functionality
Controller methods can be exposed as RESTful
Web Services w/o extra coding or configuration
67
MVC
Spring MVC
Annotation-based Controller Configuration
(@Controller)
@RequestMapping
@ModelAttribute
@RequestParam
@SessionAttributes
69
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
70
Data Binding and Validation
Data binding using @ModelAttribute
@InitBinder
Example: FeeController
Validation:
Declaration Validation Logic
Custom Validation Error Messages
JSR 303 support
72
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
75
Agenda Application Concerns
Persistence
Transaction Management
Application Security
MVC
Validation
Web Services (REST)
Conclusions
76
Conclusions
Domain First, Infrastructure Second
Layered Architecture & Separation of Concerns
POJO’s are fun to work with again
Balance between the “right” vs. the “right now”
Java EE 6 release focuses on development &
deployment simplicity
JPA 2.0, EJB 3.1, Spring 3.0
77
References (1/2) Sample Application Project Site (https://enterprise-java-
spring.googlecode.com/svn/trunk)
Spring Framework Reference Manual (http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/)
Spring In Action 3rd Edition (http://www.manning.com/walls4/)
Domain Driven Design and Development In Practice (http://www.infoq.com/articles/ddd-in-practice)
Domain-Driven Design Website (http://www.domaindrivendesign.org/)
78
References (2/2) Domain-Driven Design by Eric Evans
Applying Domain-Driven Design and Patterns, Jimmy Nilsson
Patterns of Enterprise Application Architecture, Martin Fowler
Can DDD be Adequately Implemented Without DI and AOP (http://www.infoq.com/news/2008/02/ddd-di-aop)
Contact Information
Domain-Driven Design and Enterprise
Architecture articles on InfoQ
website: http://www.infoq.com
http://srinip2007.blogspot.com
@srinip
11/21/2009 Srini Penchikala79