Object/Relational Mapping with Spring and Hibernate

30
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Object/Relational Mapping with Spring and Hibernate

description

Object/Relational Mapping with Spring and Hibernate. Topics in this session. Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs. Topics in this session. Introduction to Hibernate Mapping - PowerPoint PPT Presentation

Transcript of Object/Relational Mapping with Spring and Hibernate

Page 1: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

Object/Relational Mapping with Spring and Hibernate

Page 2: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2

Topics in this session

• Introduction to Hibernate– Mapping– Querying

• Configuring a Hibernate SessionFactory• Spring’s HibernateTemplate• Implementing Native Hibernate DAOs

Page 3: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3

Topics in this session

• Introduction to Hibernate– Mapping– Querying

• Configuring a Hibernate SessionFactory• Spring’s HibernateTemplate• Implementing Native Hibernate DAOs

Page 4: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4

Introduction to Hibernate

• Hibernate’s Session is a stateful object representing a unit-of-work– Corresponds at a higher-level to a Connection– Manages persistent objects within the unit-of-work– Acts as a transaction-scoped cache (1LC)

• A SessionFactory is a thread-safe, shareable object that represents a single data source– Provides access to a transactional Session– Manages the globally-scoped cache (2LC)

Page 5: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5

Hibernate Mapping

• Hibernate requires metadata for mapping classes and their properties to database tables and their columns

• Metadata can be provided as XML or annotations

• This session will focus on XML

Page 6: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6

Mapping simple properties

public class Customer { private Long id; private String firstName; public void setFirstName(String firstName) { this.firstName = firstName; }...

public class Customer { private Long id; private String firstName; public void setFirstName(String firstName) { this.firstName = firstName; }...

<hibernate-mapping package=“org.xyz.customer”> <class name=“Customer” table= “T_CUSTOMER”> <id name=“id” column=“cust_id” access=“field”/> <property name=“firstName” column=“first_name”/> </class></hibernate-mapping>

<hibernate-mapping package=“org.xyz.customer”> <class name=“Customer” table= “T_CUSTOMER”> <id name=“id” column=“cust_id” access=“field”/> <property name=“firstName” column=“first_name”/> </class></hibernate-mapping>

‘field’ access

‘property’ access (default) uses setter

Page 7: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7

Mapping collections

public class Customer { private Long id; private Set addresses;…

public class Customer { private Long id; private Set addresses;…

<class name=“Customer” table=“T_CUSTOMER”> ... <set name=“addresses” table=“T_ADDRESS” cascade=“all,delete-orphan” access=“field”> <key column=“CUST_ID”/> <one-to-many class=“Address”/> </set></class>

<class name=“Customer” table=“T_CUSTOMER”> ... <set name=“addresses” table=“T_ADDRESS” cascade=“all,delete-orphan” access=“field”> <key column=“CUST_ID”/> <one-to-many class=“Address”/> </set></class>

delete from database when removed from the collection

Page 8: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8

Hibernate Querying

• Hibernate provides several options for accessing data– Retrieve an object by primary key– Query for objects with the Hibernate Query Language

(HQL)– Query for objects using Criteria Queries– Execute standard SQL

Page 9: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9

Hibernate Querying: by primary key

• To retrieve an object by its database identifier simply call get(..) on the Session

• This will first check the transactional cache

Long custId = new Long(123);Customer customer = (Customer) session.get(Customer.class, custId); Long custId = new Long(123);Customer customer = (Customer) session.get(Customer.class, custId);

returns null if no object exists for the identifier

Page 10: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10

Hibernate Querying: HQL

• To query for objects based on properties or associations use HQL

String city = “Chicago”;Query query = session.createQuery( “from Customer c where c.address.city = :city”);query.setString(“city”, city);List customers = query.list();

// or if expecting a single resultCustomer customer = (Customer) query.uniqueResult();

String city = “Chicago”;Query query = session.createQuery( “from Customer c where c.address.city = :city”);query.setString(“city”, city);List customers = query.list();

// or if expecting a single resultCustomer customer = (Customer) query.uniqueResult();

Page 11: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11

Topics in this session

• Introduction to Hibernate– Mapping– Querying

• Configuring a Hibernate SessionFactory• Spring’s HibernateTemplate• Implementing Native Hibernate DAOs

Page 12: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12

Configuring a SessionFactory (1)

• Hibernate implementations of data access code require access to the SessionFactory

• The SessionFactory requires– DataSource (local or container-managed)– Mapping metadata

• Spring provides a FactoryBean for configuring a shareable SessionFactory

Page 13: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13

Configuring a SessionFactory (2)

OrderServiceImpl

HibernateOrderRepository

SessionFactory DataSourceLocalSessionFactoryBean

creates

MappingMetadata

Page 14: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14

Configuring a SessionFactory (3)

<bean id=“orderService” class=“example.OrderServiceImpl”> <constructor-arg ref=“orderRepository”/></bean><bean id=“orderRepository” class=“example.HibernateOrderRepository”> <property name=“sessionFactory” ref=“sessionFactory”/></bean> <bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource” ref=“dataSource”/> <property name=“mappingLocations”> <list> <value>classpath:example/order/Order.hbm.xml</value> </list> </property></bean><jee:jndi-lookup id=“dataSource” jndi-name=“java:comp/env/jdbc/orders”/>

<bean id=“orderService” class=“example.OrderServiceImpl”> <constructor-arg ref=“orderRepository”/></bean><bean id=“orderRepository” class=“example.HibernateOrderRepository”> <property name=“sessionFactory” ref=“sessionFactory”/></bean> <bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource” ref=“dataSource”/> <property name=“mappingLocations”> <list> <value>classpath:example/order/Order.hbm.xml</value> </list> </property></bean><jee:jndi-lookup id=“dataSource” jndi-name=“java:comp/env/jdbc/orders”/>

Page 15: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15

Topics in this session

• Introduction to Hibernate– Mapping– Querying

• Configuring a Hibernate SessionFactory• Spring’s HibernateTemplate• Implementing Native Hibernate DAOs

Page 16: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16

HibernateTemplate

• Consistent with Spring’s general DAO support– Manages resources (acquiring/releasing Sessions)– Translates Exceptions to the DataAccessException

hierarchy– Participates in Spring-managed transactions

automatically– Provides convenience methods

Page 17: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17

Configuring HibernateTemplate

• Simply provide a reference to the SessionFactory

HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);

Page 18: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18

HibernateDaoSupport

• Spring also provides this convenience base class that data access objects can extend– Creates a HibernateTemplate– Requires a reference to the SessionFactory

Page 19: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19

Extending HibernateDaoSupport

public class HibernateCustomerDao extends HibernateDaoSupport implements CustomerDao { public List findAllCustomers() { String hql = “from Customer”; return getHibernateTemplate().find(hql); }

// other data access methods}

public class HibernateCustomerDao extends HibernateDaoSupport implements CustomerDao { public List findAllCustomers() { String hql = “from Customer”; return getHibernateTemplate().find(hql); }

// other data access methods}

Page 20: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20

Configuring a subclass of HibernateDaoSupport

<bean id=“customerDao” class=“example.customer.HibernateCustomerDao” > <property name=“sessionFactory” ref=“sessionFactory”/></bean>

<bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource” ref=“dataSource”/> <property name=“mappingLocations”> <list> <value>classpath:example/customer/Customer.hbm.xml</value> </list> </property></bean>

<bean id=“customerDao” class=“example.customer.HibernateCustomerDao” > <property name=“sessionFactory” ref=“sessionFactory”/></bean>

<bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource” ref=“dataSource”/> <property name=“mappingLocations”> <list> <value>classpath:example/customer/Customer.hbm.xml</value> </list> </property></bean>

Page 21: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21

Topics in this session

• Introduction to Hibernate– Mapping– Querying

• Configuring a Hibernate SessionFactory• Spring’s HibernateTemplate• Implementing Native Hibernate DAOs

Page 22: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22

Implementing Native Hibernate DAOs (1)

• As of Hibernate 3.1 there is much less of a case for needing to use HibernateTemplate– Hibernate now provides hooks so Spring can still

manage transactions and Sessions– Use AOP for transparent exception translation to

Spring’s DataAccessException hierarchy

• It is now possible to remove any dependency on Spring in your DAO implementations

Page 23: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23

Spring-managed Transactions and Sessions (1)

• To transparently participate in Spring-driven transactions, simply use one of Spring’s FactoryBeans for building the SessionFactory

• Provide it to the data access code via dependency injection– Avoiding static access is a good thing anyway

• Define a transaction manager– HibernateTransactionManager– JtaTransactionManager

Page 24: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24

Spring-managed Transactions and Sessions (2)

• The code – with no Spring dependencies

public class HibernateOrderRepository implements OrderRepository { private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } ... public void someDaoOperation) { Session sess = this.sessionFactory.getCurrentSession(); ... // now use the Spring-managed Session }}

Page 25: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25

Spring-managed Transactions (3)

• The configuration

<beans> <bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> ... </bean> <bean id=“orderRepository” class=“HibernateOrderRepository”> <property name=“sessionFactory” ref=“sessionFactory”/> </bean> <bean id= “transactionManager” class=“org.springframework.orm.hibernate3.HibernateTransactionManager”> <property name=“sessionFactory” ref=“sessionFactory” /> </bean></beans>

<beans> <bean id=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> ... </bean> <bean id=“orderRepository” class=“HibernateOrderRepository”> <property name=“sessionFactory” ref=“sessionFactory”/> </bean> <bean id= “transactionManager” class=“org.springframework.orm.hibernate3.HibernateTransactionManager”> <property name=“sessionFactory” ref=“sessionFactory” /> </bean></beans> could use JtaTransactionManager if needed

Page 26: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26

Transparent Exception Translation (1)

• Used as-is, the previous DAO implementation will throw native, vendor-specific HibernateExceptions– Not desirable to let these propagate up to the service

layer or other users of the DAOs• Introduces dependency on specific persistence solution

where it should not exist

• AOP allows translation to Spring’s rich, vendor-neutral DataAccessException hierarchy

Page 27: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27

Transparent Exception Translation (2)

• Spring provides this capability out of the box

– Java 5+1. Annotate with @Repository or your own custom

annotation2. Define a Spring-provided BeanPostProcessor

– Java 1.3+1. Use pointcut matching your own (base or marker)

repository interface to apply Spring-provided PersistenceExceptionTranslationInterceptor

Page 28: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28

Transparent Exception Translation (3):Java 5+ Environment

• the code

• the configuration

@Repositorypublic class HibernateOrderRepository implements OrderRepository { ...}

@Repositorypublic class HibernateOrderRepository implements OrderRepository { ...}

<bean class=“org.springframework.dao.annotation. PersistenceExceptionTranslationPostProcessor”/><bean class=“org.springframework.dao.annotation. PersistenceExceptionTranslationPostProcessor”/>

Page 29: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29

Transparent Exception Translation (4):Java 1.3+ Environment

• the code

• the configuration

public class HibernateOrderRepository implements MyRepository { ...}

public class HibernateOrderRepository implements MyRepository { ...}

<bean id=“persistenceExceptionInterceptor” class=“org.springframework.dao.support. PersistenceExceptionTranslationInterceptor”/>

<aop:config> <aop:advisor pointcut=“execution(* *..MyRepository+.*(..))” advice-ref=“persistenceExceptionInterceptor” /></aop:config>

<bean id=“persistenceExceptionInterceptor” class=“org.springframework.dao.support. PersistenceExceptionTranslationInterceptor”/>

<aop:config> <aop:advisor pointcut=“execution(* *..MyRepository+.*(..))” advice-ref=“persistenceExceptionInterceptor” /></aop:config>

Page 30: Object/Relational Mapping with Spring and Hibernate

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

LAB

Using Hibernate with Spring