KEMBAR78
Spring & Hibernate | PPTX
Spring & Hibernate
Reporter:Jiayun Zhou
Date :2011/05/16
Outline
Hibernate
Spring
RIS3 AW Sample
Spring Hibernate Integration
Spring Security
Conclusion
Copyright by IISI. All rights reserved 2
Hibernate
ORM
Object/Relational Mapping
Copyright by IISI. All rights reserved 4
Java
Object-oriented Programming
Language
Copyright by IISI. All rights reserved 5
Relational Database
Copyright by IISI. All rights reserved 6
Why Mappings?
Copyright by IISI. All rights reserved 7
JDBC
Copyright by IISI. All rights reserved 8
Hibernate
Copyright by IISI. All rights reserved 9
Mapping Declaration
• Using Java 5 annotations (via JPA 2 annotations)
• Using JPA 2 XML deployment descriptors
• Using the Hibernate legacy XML files approach
known as hbm.xml
Copyright by IISI. All rights reserved 10
Mapping Declaration
• Using Java 5 annotations (via JPA 2 annotations)
• Using JPA 2 XML deployment descriptors
• Using the Hibernate legacy XML files approach
known as hbm.xml
Copyright by IISI. All rights reserved 11
Mapping Declaration - table
Copyright by IISI. All rights reserved 12
Mapping Declaration - class
@Entity
@Table(name = "bulletin")
public class Bulletin implements
Serializable {
Copyright by IISI. All rights reserved 13
Mapping Declaration - PK
private Long id;
@Id
@GeneratedValue
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Copyright by IISI. All rights reserved 14
Mapping Declaration - field
/** 標題 */
private String title;
@Column(name = "title")
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
Copyright by IISI. All rights reserved 15
@Transient
@Transient
public String getDefultDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
String defApplyDate = df.format(new Date());
return (Integer.parseInt(defApplyDate.substring(0, 4)) - 1911) + "/"
+ defApplyDate.substring(5, 7) + "/"
+ defApplyDate.substring(8, 10);
}
Copyright by IISI. All rights reserved 16
Working with Object - save
DomesticCat fritz = new DomesticCat();
fritz.setColor(Color.GINGER);
fritz.setSex('M');
fritz.setName("Fritz");
Long generatedId = (Long) sess.save(fritz);
Copyright by IISI. All rights reserved 17
Working with Object - load
Cat fritz = (Cat) sess.load(Cat.class, generatedId);
Copyright by IISI. All rights reserved 18
Working with Object - update
// in the first session
Cat cat = (Cat) firstSession.load(Cat.class, catID);
// in a higher tier of the application
Cat mate = new Cat();
cat.setMate(mate);
// later, in a new session
secondSession.saveOrUpdate(cat);
secondSession.saveOrUpdate(mate);
Copyright by IISI. All rights reserved 19
Working with Object - delete
sess.delete(cat);
Copyright by IISI. All rights reserved 20
3 Query Way
• Criteria
• HQL
• Native SQL
Copyright by IISI. All rights reserved 21
Criteria
List cats = sess.createCriteria(Cat.class)
.add( Restrictions.like("name", "Fritz%") )
.add( Restrictions.or(
Restrictions.eq( "age", new Integer(0) ),
Restrictions.isNull("age")
) )
.list();
Copyright by IISI. All rights reserved 22
Criteria - QBE
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
.add( Example.create(cat) )
.list();
Copyright by IISI. All rights reserved 23
HQL
Query q = s.createQuery("from foo Foo as foo
where foo.name=:name and foo.size=:size");
q.setProperties(fooBean); // fooBean has getName()
and getSize()
List foos = q.list();
Copyright by IISI. All rights reserved 24
HQL - join
from Cat as cat
left join cat.kittens as kitten
with kitten.bodyWeight > 10.0
Copyright by IISI. All rights reserved 25
Native SQL
sess.createSQLQuery("SELECT * FROM CATS").list();
sess.createSQLQuery("SELECT ID, NAME,
BIRTHDATE FROM CATS").list();
Copyright by IISI. All rights reserved 26
Spring
Copyright by IISI. All rights reserved 28
Copyright by IISI. All rights reserved 29
Spring Modules
• Inversion of Control container
• Aspect-oriented programming
• Data access
• Transaction management
• Model-view-controller
• Remote Access framework
• Convention-over-configuration
• Batch processing
• Authentication and authorization
• Remote Management
• Messaging
• Testing
Copyright by IISI. All rights reserved 30
IoC
Inversion of Control
Copyright by IISI. All rights reserved 31
IoC
Inversion of Control Flow
Copyright by IISI. All rights reserved 32
Martin Fowler
Copyright by IISI. All rights reserved 33
Copyright by IISI. All rights reserved 34
Command Line
#ruby
puts 'What is your name?'
name = gets
process_name(name)
puts 'What is your quest?'
quest = gets
process_quest(quest)
Copyright by IISI. All rights reserved 35
GUI
require 'tk'
root = TkRoot.new()
name_label = TkLabel.new() {text "What is Your Name?"}
name_label.pack
name = TkEntry.new(root).pack
name.bind("FocusOut") {process_name(name)}
quest_label = TkLabel.new() {text "What is Your Quest?"}
quest_label.pack
quest = TkEntry.new(root).pack
quest.bind("FocusOut") {process_quest(quest)}
Tk.mainloop()
Copyright by IISI. All rights reserved 36
Hollywood Principle
Don’t call us, we’ll call you.
Copyright by IISI. All rights reserved 37
Ex. HttpSessionListener
• sessionCreated()
• sessionDestroyed()
Copyright by IISI. All rights reserved 38
Ex. Template Method Pattern
Copyright by IISI. All rights reserved 39
Dependency Injection
One Form of IoC
Copyright by IISI. All rights reserved 40
class MovieLister...
public Movie[] moviesDirectedBy(String arg) {
List allMovies = finder.findAll();
for (Iterator it = allMovies.iterator(); it.hasNext();) {
Movie movie = (Movie) it.next();
if (!movie.getDirector().equals(arg)) it.remove();
}
return (Movie[]) allMovies.toArray(new
Movie[allMovies.size()]);
}
Copyright by IISI. All rights reserved 41
public interface MovieFinder {
List findAll();
}
Copyright by IISI. All rights reserved 42
class MovieLister...
private MovieFinder finder;
public MovieLister() {
finder = new
ColonDelimitedMovieFinder("movies1.txt");
}
Copyright by IISI. All rights reserved 43
Copyright by IISI. All rights reserved 44
Copyright by IISI. All rights reserved 45
Spring Stereotype
• @Component
• @Repository
• @Service
• @Controller
Copyright by IISI. All rights reserved 46
@Repository
class ColonMovieFinder...
public void setFilename(String filename) {
this.filename = filename;
}
Copyright by IISI. All rights reserved 47
Spring @Autowired
class MovieLister...
private MovieFinder finder;
@Autowired
public void setFinder(MovieFinder finder) {
this.finder = finder;
}
Copyright by IISI. All rights reserved 48
RIS3 AW Sample
Spring Hibernate Integration
Configuration
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/dbaw" />
<tx:annotation-driven />
<tx:jta-transaction-manager />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationS
essionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan"
value="com.iisigroup.ris.aw.domain" />
<property name="hibernateProperties">
<props>
<prop
key="hibernate.dialect">org.hibernate.dialect.InformixDialect</prop>
</props>
</property>
</bean>
Copyright by IISI. All rights reserved 51
Transaction Management
@Override
@Transactional(readOnly = true)
public List<Bulletin> findAll() {
return bulletinDAO.findAll();
}
@Override
@Transactional
public void save(Bulletin bulletin) {
bulletinDAO.save(bulletin);
}
Copyright by IISI. All rights reserved 52
Spring Security
Authentication Integration
• HTTP BASIC authentication headers
• HTTP Digest authentication headers
• HTTP X.509 client certificate exchange
• LDAP
• Form-based authentication
• OpenID authentication
• Authentication based on pre-established
request headers
• JA-SIG Central Authentication Service
Copyright by IISI. All rights reserved 54
Authentication Integration –
cont.
• Transparent authentication context propagation
for Remote Method Invocation (RMI) and
HttpInvoker (a Spring remoting protocol)
• Automatic "remember-me" authentication
• Anonymous authentication
• Run-as authentication
• Java Authentication and Authorization Service
(JAAS)
• JEE container autentication
• Kerberos
Copyright by IISI. All rights reserved 55
Configuration
<http use-expressions="true" auto-config="true">
<intercept-url pattern="/login.jsp*" access="isAnonymous()" />
<intercept-url pattern="/**" access="isAuthenticated()" />
<form-login login-page="/login.jsp" default-target-url="/faces/index.xhtml"
always-use-default-target="true" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="jimi" password="jimi" authorities="admin, System" />
<user name="bob" password="bob" authorities="System" />
</user-service>
</authentication-provider>
</authentication-manager>
Copyright by IISI. All rights reserved 56
Conclusion
• Entity
• DAO, DAOImpl
• Service, ServiceImpl
• Managed Bean, XHTML
Copyright by IISI. All rights reserved 58
Headquarter
6F., No.7, Sec. 2, Xianmin Blvd., Banqiao Dist., New Taipei City 22041, Taiwan(R.O.C)
TEL : +886-2-8969-1969 FAX : +886-2-8969-3359
- Thank You -

Spring & Hibernate

  • 1.
    Spring & Hibernate Reporter:JiayunZhou Date :2011/05/16
  • 2.
    Outline Hibernate Spring RIS3 AW Sample SpringHibernate Integration Spring Security Conclusion Copyright by IISI. All rights reserved 2
  • 3.
  • 4.
    ORM Object/Relational Mapping Copyright byIISI. All rights reserved 4
  • 5.
  • 6.
    Relational Database Copyright byIISI. All rights reserved 6
  • 7.
    Why Mappings? Copyright byIISI. All rights reserved 7
  • 8.
    JDBC Copyright by IISI.All rights reserved 8
  • 9.
    Hibernate Copyright by IISI.All rights reserved 9
  • 10.
    Mapping Declaration • UsingJava 5 annotations (via JPA 2 annotations) • Using JPA 2 XML deployment descriptors • Using the Hibernate legacy XML files approach known as hbm.xml Copyright by IISI. All rights reserved 10
  • 11.
    Mapping Declaration • UsingJava 5 annotations (via JPA 2 annotations) • Using JPA 2 XML deployment descriptors • Using the Hibernate legacy XML files approach known as hbm.xml Copyright by IISI. All rights reserved 11
  • 12.
    Mapping Declaration -table Copyright by IISI. All rights reserved 12
  • 13.
    Mapping Declaration -class @Entity @Table(name = "bulletin") public class Bulletin implements Serializable { Copyright by IISI. All rights reserved 13
  • 14.
    Mapping Declaration -PK private Long id; @Id @GeneratedValue @Column(name = "id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } Copyright by IISI. All rights reserved 14
  • 15.
    Mapping Declaration -field /** 標題 */ private String title; @Column(name = "title") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } Copyright by IISI. All rights reserved 15
  • 16.
    @Transient @Transient public String getDefultDate(){ SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); String defApplyDate = df.format(new Date()); return (Integer.parseInt(defApplyDate.substring(0, 4)) - 1911) + "/" + defApplyDate.substring(5, 7) + "/" + defApplyDate.substring(8, 10); } Copyright by IISI. All rights reserved 16
  • 17.
    Working with Object- save DomesticCat fritz = new DomesticCat(); fritz.setColor(Color.GINGER); fritz.setSex('M'); fritz.setName("Fritz"); Long generatedId = (Long) sess.save(fritz); Copyright by IISI. All rights reserved 17
  • 18.
    Working with Object- load Cat fritz = (Cat) sess.load(Cat.class, generatedId); Copyright by IISI. All rights reserved 18
  • 19.
    Working with Object- update // in the first session Cat cat = (Cat) firstSession.load(Cat.class, catID); // in a higher tier of the application Cat mate = new Cat(); cat.setMate(mate); // later, in a new session secondSession.saveOrUpdate(cat); secondSession.saveOrUpdate(mate); Copyright by IISI. All rights reserved 19
  • 20.
    Working with Object- delete sess.delete(cat); Copyright by IISI. All rights reserved 20
  • 21.
    3 Query Way •Criteria • HQL • Native SQL Copyright by IISI. All rights reserved 21
  • 22.
    Criteria List cats =sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .add( Restrictions.or( Restrictions.eq( "age", new Integer(0) ), Restrictions.isNull("age") ) ) .list(); Copyright by IISI. All rights reserved 22
  • 23.
    Criteria - QBE Catcat = new Cat(); cat.setSex('F'); cat.setColor(Color.BLACK); List results = session.createCriteria(Cat.class) .add( Example.create(cat) ) .list(); Copyright by IISI. All rights reserved 23
  • 24.
    HQL Query q =s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size"); q.setProperties(fooBean); // fooBean has getName() and getSize() List foos = q.list(); Copyright by IISI. All rights reserved 24
  • 25.
    HQL - join fromCat as cat left join cat.kittens as kitten with kitten.bodyWeight > 10.0 Copyright by IISI. All rights reserved 25
  • 26.
    Native SQL sess.createSQLQuery("SELECT *FROM CATS").list(); sess.createSQLQuery("SELECT ID, NAME, BIRTHDATE FROM CATS").list(); Copyright by IISI. All rights reserved 26
  • 27.
  • 28.
    Copyright by IISI.All rights reserved 28
  • 29.
    Copyright by IISI.All rights reserved 29
  • 30.
    Spring Modules • Inversionof Control container • Aspect-oriented programming • Data access • Transaction management • Model-view-controller • Remote Access framework • Convention-over-configuration • Batch processing • Authentication and authorization • Remote Management • Messaging • Testing Copyright by IISI. All rights reserved 30
  • 31.
    IoC Inversion of Control Copyrightby IISI. All rights reserved 31
  • 32.
    IoC Inversion of ControlFlow Copyright by IISI. All rights reserved 32
  • 33.
    Martin Fowler Copyright byIISI. All rights reserved 33
  • 34.
    Copyright by IISI.All rights reserved 34
  • 35.
    Command Line #ruby puts 'Whatis your name?' name = gets process_name(name) puts 'What is your quest?' quest = gets process_quest(quest) Copyright by IISI. All rights reserved 35
  • 36.
    GUI require 'tk' root =TkRoot.new() name_label = TkLabel.new() {text "What is Your Name?"} name_label.pack name = TkEntry.new(root).pack name.bind("FocusOut") {process_name(name)} quest_label = TkLabel.new() {text "What is Your Quest?"} quest_label.pack quest = TkEntry.new(root).pack quest.bind("FocusOut") {process_quest(quest)} Tk.mainloop() Copyright by IISI. All rights reserved 36
  • 37.
    Hollywood Principle Don’t callus, we’ll call you. Copyright by IISI. All rights reserved 37
  • 38.
    Ex. HttpSessionListener • sessionCreated() •sessionDestroyed() Copyright by IISI. All rights reserved 38
  • 39.
    Ex. Template MethodPattern Copyright by IISI. All rights reserved 39
  • 40.
    Dependency Injection One Formof IoC Copyright by IISI. All rights reserved 40
  • 41.
    class MovieLister... public Movie[]moviesDirectedBy(String arg) { List allMovies = finder.findAll(); for (Iterator it = allMovies.iterator(); it.hasNext();) { Movie movie = (Movie) it.next(); if (!movie.getDirector().equals(arg)) it.remove(); } return (Movie[]) allMovies.toArray(new Movie[allMovies.size()]); } Copyright by IISI. All rights reserved 41
  • 42.
    public interface MovieFinder{ List findAll(); } Copyright by IISI. All rights reserved 42
  • 43.
    class MovieLister... private MovieFinderfinder; public MovieLister() { finder = new ColonDelimitedMovieFinder("movies1.txt"); } Copyright by IISI. All rights reserved 43
  • 44.
    Copyright by IISI.All rights reserved 44
  • 45.
    Copyright by IISI.All rights reserved 45
  • 46.
    Spring Stereotype • @Component •@Repository • @Service • @Controller Copyright by IISI. All rights reserved 46
  • 47.
    @Repository class ColonMovieFinder... public voidsetFilename(String filename) { this.filename = filename; } Copyright by IISI. All rights reserved 47
  • 48.
    Spring @Autowired class MovieLister... privateMovieFinder finder; @Autowired public void setFinder(MovieFinder finder) { this.finder = finder; } Copyright by IISI. All rights reserved 48
  • 49.
  • 50.
  • 51.
    Configuration <jee:jndi-lookup id="dataSource" jndi-name="jdbc/dbaw"/> <tx:annotation-driven /> <tx:jta-transaction-manager /> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationS essionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan" value="com.iisigroup.ris.aw.domain" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.InformixDialect</prop> </props> </property> </bean> Copyright by IISI. All rights reserved 51
  • 52.
    Transaction Management @Override @Transactional(readOnly =true) public List<Bulletin> findAll() { return bulletinDAO.findAll(); } @Override @Transactional public void save(Bulletin bulletin) { bulletinDAO.save(bulletin); } Copyright by IISI. All rights reserved 52
  • 53.
  • 54.
    Authentication Integration • HTTPBASIC authentication headers • HTTP Digest authentication headers • HTTP X.509 client certificate exchange • LDAP • Form-based authentication • OpenID authentication • Authentication based on pre-established request headers • JA-SIG Central Authentication Service Copyright by IISI. All rights reserved 54
  • 55.
    Authentication Integration – cont. •Transparent authentication context propagation for Remote Method Invocation (RMI) and HttpInvoker (a Spring remoting protocol) • Automatic "remember-me" authentication • Anonymous authentication • Run-as authentication • Java Authentication and Authorization Service (JAAS) • JEE container autentication • Kerberos Copyright by IISI. All rights reserved 55
  • 56.
    Configuration <http use-expressions="true" auto-config="true"> <intercept-urlpattern="/login.jsp*" access="isAnonymous()" /> <intercept-url pattern="/**" access="isAuthenticated()" /> <form-login login-page="/login.jsp" default-target-url="/faces/index.xhtml" always-use-default-target="true" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name="jimi" password="jimi" authorities="admin, System" /> <user name="bob" password="bob" authorities="System" /> </user-service> </authentication-provider> </authentication-manager> Copyright by IISI. All rights reserved 56
  • 57.
  • 58.
    • Entity • DAO,DAOImpl • Service, ServiceImpl • Managed Bean, XHTML Copyright by IISI. All rights reserved 58
  • 59.
    Headquarter 6F., No.7, Sec.2, Xianmin Blvd., Banqiao Dist., New Taipei City 22041, Taiwan(R.O.C) TEL : +886-2-8969-1969 FAX : +886-2-8969-3359 - Thank You -

Editor's Notes

  • #4 http://www.hibernate.org/
  • #14 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/types.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/associations.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/components.html http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/inheritance.html
  • #18 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/objectstate.html
  • #23 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querycriteria.html
  • #24 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querycriteria.html#querycriteria-examples
  • #25 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html
  • #26 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/queryhql.html
  • #27 http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/querysql.html
  • #34 http://martinfowler.com/
  • #36 http://martinfowler.com/bliki/InversionOfControl.html
  • #41 http://martinfowler.com/articles/injection.html
  • #47 http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/stereotype/package-summary.html
  • #51 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-hibernate
  • #53 http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#transaction-declarative-annotations
  • #54 http://static.springsource.org/spring-security/site/index.html