0% found this document useful (0 votes)
11 views

Module 00 - Course Intro

ea lect5

Uploaded by

surafelbehailu90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module 00 - Course Intro

ea lect5

Uploaded by

surafelbehailu90
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Module 00: Course Introduction

CS544: Enterprise Architecture

© 2014 Time2Master 1
Wholeness
§ We will look at the architectural requirements
of enterprise applications and how this relates
to the technologies that we will be using for
this course.
§ We will look at what the different logical
layers are as an application grows
§ We will look at what Spring is and how it
relates to the different layers
§ We will look at what Hibernate is and how it
relates to the different layers

© 2014 Time2Master 2
Course Introduction:

ARCHITECTURE

© 2014 Time2Master 3
Model 1
Single File

Business & Control Code

DB / Model Code

Display / View Code

© 2014 Time2Master 4
Classic MVC (Model 2)

View

Works well
In a desktop
environment

Control Model

© 2014 Time2Master 5
Model 1 vs Model 2 Architecture

Read more about model 1 vs. model 2 here:

http://download.oracle.com/otn_hosted_doc/jdeveloper/1012/developing_mvc_applications/adf_aboutmvc2.html

© 2014 Time2Master 6
Three Tier / Web MVC

Response V V V V View

Request C C Control

M M
Model

DB

© 2014 Time2Master 7
Multiple Types of Clients

Web Response V V
Desktop
(Swing) WS
??
Web Request

Web Desk WS ?? Web Desk WS ??

Business Business
BAD!

M M

DB

© 2014 Time2Master 8
Service Oriented Architecture
View
V V V
V

Control Web Desk WS ??

Service Business Business

Model M M

DB

© 2014 Time2Master 9
Course Introduction:

PRINCIPLES

© 2014 Time2Master 10
Domain-Driven Design (DDD)

persistency

remoting
business
logic

1. All business logic is captured in the messaging


domain model that reflects the real
world domain.
2. The business logic objects are
independend of the enterprise service logging
objects
© 2014 Time2Master 11
11
Service Oriented Architecture
View
V V V
V

Control Web Desk WS ??

Service Business Business

Domain

DAO DAO DAO DAO


Persistence

DB
© 2014 Time2Master 12
Advantages of DDD

persistency

remoting
business
logic
messaging
§ Business logic is independend of
technology changes
logging
§ Switching between technology is easy
§ Business logic is easy to understand
§ Easy to write, test, modify

© 2014 Time2Master 13
13
Frameworks / Inversion of Control
§ The Hollywood Principle:
§ Don’t call us, we’ll call you

Our Code
Framework

Our
Code Our
Code
Lib
Lib Lib Our
Code

© 2014 Time2Master 14
Declarative Programming
- Annotations or XML -
§ Service Helpers
§ Transactions
§ Security
§ Logging
§ AOP

§ Object Relational Mapping


§ Identity
§ Attributes
§ Associations
§ Meta Data

© 2014 Time2Master 15
Separation of Concerns
§ Different Architectural Layers

§ Plain Old Java Objects


§ Java Bean Standard

§ In Summary everything is about SoC:


§ Separate Business from Technology

© 2014 Time2Master 16
Course Introduction:

HIBERNATE

© 2014 Time2Master 17
Framework for the persistence layer
View
V V V
V

Control Web Desk WS ??

Service Business Business

Domain

DAO DAO DAO DAO


Hibernate
Persistence
Framework

DB
© 2014 Time2Master 18
Object-Relational Mismatch
Object Oriented Relational Database
Objects are instantiations of classes and In the relational model the table name
have identity (object1 == object2) and primary key are used to identity a
row in a table
Objects have associations (one-to-one, Relational model has foreign keys and link
many-to-one, …) tables
OO has inheritance Relational model has no such thing
Data can be accessed by following object Data can be accessed using queries and
associations joins

Object Model Relational Schema

© 2014 Time2Master 19
Java Persistence Possibilities
Possibility Example
Stored Procedures Stored PL/SQL or Transact-SQL procedures
SQL in the Application Putting SQL in strings inside the application, using the

More OO Friendly
JDBC API straight or wrapped by the Spring JDBC
template
iBatis SQL maps Moving SQL into XML configuration removing JDBC
plumbing code overhead
Entity Beans 2.1 Using a Java Enterprise Edition 2.1 application server
with Entity Beans
Object Relational Mapping Using tools such as Hibernate, Toplink, JDO, and JPA
to map an Object Model onto a Relational Schema

Object Model Relational Schema

© 2014 Time2Master 20
Object Relational Mapping (ORM)
§ Object Relational Mapping lets the
programmer focus on the Object Model
§ Supports Domain Driven Development (DDD)
§ Programmer can just work with objects
§ Once an object has been retrieved any related
objects are automatically loaded as needed
§ Changes to objects can automatically be stored in
the database

find, load, save SQL


Application Database
ORM
Object(s) Resultset
© 2014 Time2Master 21
Advantages of ORM
Advantage Details
Productivity •Fewer lines of persistency code

Maintainability •Fewer lines of persistency code


•Mapping is defined in one place

Performance •Caching
•Higher productivity gives more time for optimization
üProjects under time pressure often don’t have time for
optimization
•The developers of the ORM put a lot of effort in optimizing the
ORM

find, load, save SQL


Application Database
ORM
Object(s) Resultset
© 2014 Time2Master 22
The Java Persistence API (JPA)
§ JPA is a Java standard for ORM persistency

Application Hibernate Hibernate


code

Application Toplink Toplink


code

Application JPA JPA Hibernate


code

Application JPA JPA Toplink


code

© 2014 Time2Master 23
3 ways to use Hibernate
1. Hibernate using XML mapping file(s)
Hibernate
Application
session
application.java Database
employee.java Employee table
hibernate.cfg.xml employee.hbm.xml

2. Hibernate using annotations


Hibernate
Application
session
application.java Database
employee.java Employee table
hibernate.cfg.xml

3. JPA using annotations


Hibernate
Application
entitymanager
application.java Database
employee.java Employee table
persistence.xml
© 2014 Time2Master 24
1. Hibernate using XML mapping file(s)
Application Hibernate

session
Database
application.java

Employee table

employee.java

Hibernate Mapping
properties file
hibernate.cfg.xml employee.hbm.xml

<hibernate-mapping>
<class name="intro.xml.Employee" table="employee">
<id name="id" type="int" column="ID" >
<generator class="increment" />
</id>
<property name="firstname" column="firstname" type="string" />
<property name="lastname" column="lastname" type="string" />
</class>
</hibernate-mapping>

© 2014 Time2Master 25
2. Hibernate using Annotations

Application Hibernate

application.java session
Database

Employee table

employee.java

Hibernate
@Entity
properties
public class Employee {
@Id hibernate.cfg.xml
@GeneratedValue
private int id;
private String firstname;
private String lastname;

...

© 2014 Time2Master 26
3. JPA using Annotations

Application JPA Hibernate

application.java entitymanager
Database

Employee table

employee.java

persistence.xml
@Entity
public class Employee {
@Id
@GeneratedValue
private int id;
private String firstname;
private String lastname;

...

© 2014 Time2Master 27
A simple Hibernate Example
Employee table
public class Employee {
private String firstname; id firstname lastname
private String lastname;
private int id;

public Employee() {
}
… Every entity must have a null
} argument constructor

Employee.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class name="intro.xml.Employee" table="employee">
<id name="id" type="int" column="ID" >
<generator class="increment" />
</id>
<property name="firstname" column="firstname" type="string" />
<property name="lastname" column="lastname" type="string" />
</class>
</hibernate-mapping>

© 2014 Time2Master 28
Hibernate Configuration File

hibernate.cfg.xml
<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- HSQL DB running on localhost -->
<property name="connection.url">jdbc:hsqldb:hsql://localhost/employeedb</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Mapping files -->
<mapping resource="intro/xml/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

© 2014 Time2Master 29
Using Annotations

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity Employee table


public class Employee {
id firstname lastname
@Id
@GeneratedValue
private int id;
private String firstname;
private String lastname;

public Employee() { }


}

© 2014 Time2Master 30
Hibernate Annotations Configuration

hibernate.cfg.xml
<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- HSQL DB running on localhost -->
<property name="connection.url">jdbc:hsqldb:hsql://localhost/employeedb</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Mapping files -->
<mapping class="intro.annotations.Employee"/>
</session-factory>
</hibernate-configuration>

© 2014 Time2Master 31
Hibernate Application Example
public class Application {
private static SessionFactory sessionFactory;
static {
// This step will read hibernate.cfg.xml and prepare hibernate for use
Configuration configuration = new Configuration();
Configuration.configure("intro/annotations/hibernate.cfg.xml");
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(sr);
}
public static void main(String[] args) {
// Hibernate placeholders
Session session = null;
Transaction tx = null;
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();

// Create new instance of Employee and set values in it


Employee employee = new Employee();
employee.setFirstname("Frank");
employee.setLastname("Miller");
// save the employee
session.persist(employee);

tx.commit();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
if (session != null)
session.close();
} © 2014 Time2Master 32
Hibernate Application Continued
try {
session = sessionFactory.openSession();
tx = session.beginTransaction();

// retieve all employees


List<Employee> employeeList = session.createQuery("from Employee").list();
for (Employee emp : employeeList) {
System.out.println("firstname= " + emp.getFirstname()
+ ", lastname= " + emp.getLastname());
}
tx.commit();

} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
if (session != null)
session.close();
}

// Close the SessionFactory (not mandatory)


sessionFactory.close();
}
}

Output:
firstname= Frank, lastname= Miller

© 2014 Time2Master 33
Hibernate configuration :show_sql
hibernate.cfg.xml
<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- HSQL DB running on localhost -->
<property name="connection.url">jdbc:hsqldb:hsql://localhost/employeedb</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Show all SQL DML executed by Hibernate -->
<property name="show_sql">true</property>
<!-- Mapping files --> Show the SQL that Hibernate
<mapping resource="intro/xml/Employee.hbm.xml"/>
sends to the database
</session-factory>
</hibernate-configuration>

Example Output:
Hibernate: insert into Employee (id, firstname, lastname) values (null, ?, ?)
Hibernate: call identity()
Hibernate: select employee0_.id as id0_, employee0_.firstname as firstname0_,
employee0_.lastname as lastname0_ from Employee employee0_
firstname= Frank, lastname= Miller

© 2014 Time2Master 34
Hibernate configuration :hbm2ddl
hibernate.cfg.xml
<?xml version="1.0" encoding="windows-1252" ?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- HSQL DB running on localhost -->
<property name="connection.url">jdbc:hsqldb:hsql://localhost/employeedb</property>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<property name="hbm2ddl.auto">create</property>
Create the database tables
<!-- Show all SQL DML executed by Hibernate -->
during the starttup of the
<property name="show_sql">true</property>
<!-- Mapping files --> application
<mapping resource="Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

© 2014 Time2Master 35
Active Learning
§ In which ways do the OO model and the
Relational model conflict?

§ Why would it be good to use the show_sql


hibernate configuration?

© 2014 Time2Master 36
Hibernate Summary
§ We talked about the object / relational
mismatch and the various Java persistence
possibilities
§ Of the various Java Persistence possibilities
ORM mapping is the most OO friendly
§ We showed a small, although complete
Hibernate application example with both XML
and JPA mapping.
§ We also gave some Hibernate configuration
options that are useful for development

© 2014 Time2Master 37
Course Introduction:

SPRING

© 2014 Time2Master 38
Framework for the Service Layer
View Optional Spring Modules
V V V
V

Control Web Desk WS ?? Optional Spring Modules

Service Business Business


Spring Framework

Optional Spring Modules


Model M M

DB

© 2014 Time2Master 39
History of Spring
§ Started as alternative to EJB 2.1
§ Rod Johnson book: Expert One-on-one J2EE
Design And Development

§ EJB 3 Is like Spring / Hibernate


§ Spring moved ahead / not tied down by legacy
§ Spring community expanded beyond EJB

§ Spring becomes another JEE implementation?

© 2014 Time2Master 40
Aim of the Spring framework
§ Make enterprise Java application development
as easy as possible, following good
programming practices
§ POJO-based programming
§ Separation of concerns
§ Flexibility

© 2014 Time2Master 41
41
POJO based programming
§ All code is written in java objects
§ No EJB’s
§ Promotes Object-Oriented principles
§ Simple to understand
§ Simple to refactor
§ Simple to unit test

© 2014 Time2Master 42
42
Core of Spring

Aspect-Oriented
Programming
(AOP)

Dependency Injection (DI) Enterprise Service


Templates

© 2014 Time2Master 43
43
Dependency Injection
§ Spring instantiates objects and wires them
together
public class AccountService {
private AccountDAO accountDAO;
AccountService
public void setAccountDAO(AccountDAO accountDAO) {
this.accountDAO = accountDAO;
}
accountDAO

public Account getAccount(int accountNumber) {


return accountDAO.loadAccount(accountNumber);
}
}

AccountDAO

<bean id="accountService" class="bank.AccountService">


<property name="accountDAO" ref="accountDAO" />
</bean>
<bean id="accountDAO" class="bank.dao.AccountDAO" />

© 2014 Time2Master 44
44
Aspect-Oriented Programming (AOP)
§ Separate the crosscutting concerns (plumbing
code) from the business logic code
§ AOP development
1. Write the business logic without worrying about the enterprise services (security,
transactions, logging, etc)
2. Write the enterprise services
3. Weave them together

Enterprise services
Business security
logic in transactions
POJO’s
persistency
messaging
email
Configuration file
© 2014 Time2Master 45
45
Enterprise Service Templates
§ Makes programming the different enterprise service
API’s simpler.
§ JDBC template
§ JMS template
§ JavaMail template
§ Hibernate template

§ Let the programmer focus on what needs to happen


instead of complexity of the specific API
§ Resource management
§ Exception handling
§ Try-catch-finally-try-catch blocks

© 2014 Time2Master 46
46
The power of Spring

Aspect-Oriented
Programming
(AOP)

Dependency Injection (DI) Enterprise Service


Templates

© 2014 Time2Master 47
47
Spring Portfolio
Spring Core Spring MVC

Spring Web Flow


DI
Spring Dynamic Modules

Spring Batch
AOP
Spring security

Spring LDAP
Enterprise Service
Spring webservices
Templates
Spring Rich Client Platform

Spring Integration

Spring Java Config

Spring IDE
Spring .NET

© 2014 Time2Master 48
Advantages of Spring
§ Spring makes application development simple
§ POJO based programming
§ Simple coding of enterprise java API’s
§ Dependency injection gives flexibility in bean
wiring
§ AOP separates the business logic from the
enterprise service (plumbing) code
Flexibility

Simple Separation of concern


© 2014 Time2Master 49
Disadvantages of Spring
§ Spring is another framework to learn
§ But, if you use another technique or another framework
you have the same problem
§ Spring is not a Java EE standard
§ But, the value of a standard is not that important anymore
§ Spring is much more powerful than EJB 3.0
§ Spring has become an enterprise Java standard
§ The XML file of Spring can become very complex
§ But: …
§ Spring also supports annotations
§ The Spring XML file is not that complex once you get used to it
§ The Spring XML file can be separated into multiple XML files
§ Spring also supports Java configuration

© 2014 Time2Master 50
Active Learning
§ What are the 3 main components of Spring?

§ Why would you want to use Spring over


standardized JavaEE EJBs?

© 2014 Time2Master 51
Spring Summary
§ Spring makes developing enterprise Java
applications simpler.
§ Spring started as a replacement for EJB’s, but
has evolved to a framework that supports all
different application layers
§ The core of Spring consists of DI, AOP and
enterprise service templates
§ There are many additional projects in the
Spring eco-system that easily integrate with
Spring in a modular fashion.

© 2014 Time2Master 52

You might also like