1 / 30

Object/Relational Mapping with Spring and Hibernate

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

chacha
Download Presentation

Object/Relational Mapping with Spring and Hibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object/Relational Mapping with Spring and Hibernate

  2. Topics in this session Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs

  3. Topics in this session Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs

  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)

  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

  6. Mapping simple properties public class Customer { private Long id; private String firstName; public void setFirstName(String firstName) { this.firstName = firstName; } ... ‘property’ access (default) uses setter ‘field’ access <hibernate-mappingpackage=“org.xyz.customer”> <classname=“Customer”table= “T_CUSTOMER”> <idname=“id”column=“cust_id”access=“field”/> <propertyname=“firstName”column=“first_name”/> </class> </hibernate-mapping>

  7. Mapping collections public class Customer { private Long id; private Set addresses; … delete from database when removed from the collection <classname=“Customer”table=“T_CUSTOMER”> ... <setname=“addresses”table=“T_ADDRESS” cascade=“all,delete-orphan”access=“field”> <keycolumn=“CUST_ID”/> <one-to-manyclass=“Address”/> </set> </class>

  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

  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 returns null if no object exists for the identifier Long custId = new Long(123); Customer customer = (Customer) session.get(Customer.class, custId);

  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 result Customer customer = (Customer) query.uniqueResult();

  11. Topics in this session Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs

  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

  13. Configuring a SessionFactory (2) OrderServiceImpl Hibernate OrderRepository LocalSession FactoryBean creates SessionFactory DataSource Mapping Metadata

  14. Configuring a SessionFactory (3) <beanid=“orderService”class=“example.OrderServiceImpl”> <constructor-arg ref=“orderRepository”/> </bean> <beanid=“orderRepository” class=“example.HibernateOrderRepository”> <property name=“sessionFactory” ref=“sessionFactory”/> </bean> <beanid=“sessionFactory” class=“org.springframework.orm.hibernate3.LocalSessionFactoryBean”> <property name=“dataSource” ref=“dataSource”/> <propertyname=“mappingLocations”> <list> <value>classpath:example/order/Order.hbm.xml</value> </list> </property> </bean> <jee:jndi-lookupid=“dataSource”jndi-name=“java:comp/env/jdbc/orders”/>

  15. Topics in this session Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs

  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

  17. Configuring HibernateTemplate Simply provide a reference to the SessionFactory HibernateTemplate hibernateTemplate = newHibernateTemplate(sessionFactory);

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

  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 }

  20. Configuring a subclass of HibernateDaoSupport <beanid=“customerDao” class=“example.customer.HibernateCustomerDao”> <property name=“sessionFactory” ref=“sessionFactory”/> </bean> <beanid=“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>

  21. Topics in this session Introduction to Hibernate Mapping Querying Configuring a Hibernate SessionFactory Spring’s HibernateTemplate Implementing Native Hibernate DAOs

  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

  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

  24. Spring-managed Transactions and Sessions (2) The code – with no Spring dependencies public class HibernateOrderRepository implements OrderRepository { private SessionFactory sessionFactory; public voidsetSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } ... public void someDaoOperation) { Session sess = this.sessionFactory.getCurrentSession(); ... // now use the Spring-managed Session } }

  25. Spring-managed Transactions (3) The configuration could use JtaTransactionManager if needed <beans> <beanid=“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>

  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

  27. Transparent Exception Translation (2) Spring provides this capability out of the box Java 5+ Annotate with @Repository or your own custom annotation Define a Spring-provided BeanPostProcessor Java 1.3+ Use pointcut matching your own (base or marker) repository interface to apply Spring-provided PersistenceExceptionTranslationInterceptor

  28. Transparent Exception Translation (3): Java 5+ Environment the code the configuration @Repository public class HibernateOrderRepository implements OrderRepository { ... } <beanclass=“org.springframework.dao.annotation. PersistenceExceptionTranslationPostProcessor”/>

  29. Transparent Exception Translation (4): Java 1.3+ Environment the code the configuration public class HibernateOrderRepository implements MyRepository { ... } <beanid=“persistenceExceptionInterceptor” class=“org.springframework.dao.support. PersistenceExceptionTranslationInterceptor”/> <aop:config> <aop:advisorpointcut=“execution(* *..MyRepository+.*(..))” advice-ref=“persistenceExceptionInterceptor” /> </aop:config>

  30. LAB Using Hibernate with Spring

More Related