Module 00 - Course Intro
Module 00 - Course Intro
© 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
DB / Model 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
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
Business Business
BAD!
M M
DB
© 2014 Time2Master 8
Service Oriented Architecture
View
V V V
V
Model M M
DB
© 2014 Time2Master 9
Course Introduction:
PRINCIPLES
© 2014 Time2Master 10
Domain-Driven Design (DDD)
persistency
remoting
business
logic
Domain
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
© 2014 Time2Master 15
Separation of Concerns
§ Different Architectural Layers
© 2014 Time2Master 16
Course Introduction:
HIBERNATE
© 2014 Time2Master 17
Framework for the persistence layer
View
V V V
V
Domain
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
© 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
© 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
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
© 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
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.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;
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();
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();
} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
if (session != null)
session.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?
© 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
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
© 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)
© 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
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
© 2014 Time2Master 46
46
The power of Spring
Aspect-Oriented
Programming
(AOP)
© 2014 Time2Master 47
47
Spring Portfolio
Spring Core Spring MVC
Spring Batch
AOP
Spring security
Spring LDAP
Enterprise Service
Spring webservices
Templates
Spring Rich Client Platform
Spring Integration
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
© 2014 Time2Master 50
Active Learning
§ What are the 3 main components of Spring?
© 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