Skip to content

SR – Chapter 9

April 29, 2010
tags:

ORM

When using an ORM framework, you have to configure its resource factory (e.g., a session
factory in Hibernate and an entitymanager factory in JPA) with its API. This resource
factory can also take advantage of Spring’s data access facilities, such as data sources and
transaction managers.

JPA with Hibernate as the underlying engine. JPA can have any engine inside doing real persistence.
Usually, when using ORM for persisting objects, the insert and update operations are
combined into a single operation (e.g., store). This is to let the ORMframework (not you)
decide whether an object should be inserted or updated.

Table 9-1. Core Programming Elements for Different Data Access Strategies
Concept JDBC Hibernate JPA
Resource Connection Session EntityManager
————————————————————————————
Resource factory DataSource SessionFactory EntityManagerFactory
Exception SQLException HibernateException PersistenceException

Can use: Hib api + JPA Annotations (Mixture).

JPA is NOT a persistence implementation itself.
You have to pick up a JPA-compliant engine to provide persistence services.
Hibernate can be JPA-compliant using Hibernate EntityManager.

JARS: persistence.jar ( lib/j2ee ), hibernate-annotations.jar, andhibernate-commons-annotations.jar (

lib/hibernate ) + Lots of other jars.

Steps to use it: MIXTURE::::
——————————————

1. mapping class=”com.apress.springrecipes.course.Course
2. USe class AnnotationConfiguration instead of Configuration
3. JPA configuration file persistence.xml – persistence unit defines a set of persistent classes and how they
should be persisted

EntityManager manager = entityManagerFactory.createEntityManager();
EntityTransaction tx = manager.getTransaction();
try {
tx.begin();
Course course = manager.find(Course.class, courseId);
manager.remove(course);

Hibernate: 1 global configuration file : hibernate.cfg.xml/ Class specific file Course.hbm.xml.

Transaction tx = session.getTransaction();
try {
tx.begin();
Course course = (Course) session.get(Course.class, courseId);
session.delete(course);
tx.commit();
Query query = session.createQuery("from Course");
 query.list();
session.saveOrUpdate(course);
return (Course) session.get(Course.class, courseId);

Must remember to close a session to release the resources held by this session.

	} finally {
		session.close();
	}

##################################

As of now – you build a session factory and an entitymanager factory yourself.
Spring assists in that and in dao utils as well..

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>

Can ignore the Hibernate configuration file bymerging all the configurations into LocalSessionFactoryBean.

<property name="hibernateProperties">
<props>
<prop key="hiber"/>
<property name="mappingLocations">
<list>
<value>classpath:com/apress/springrecipes/course/Course.hbm.xml</value>
</list>
</property>

** Pure JPA: org.springframework.orm.jpa.LocalEntityManagerFactoryBean
** JPA+ Hib MIX : If using mix of JPA + Hibernate use the class: AnnotationSessionFactoryBean and it’s property annotatedClasses for mentioning which class is annotated.

Support JDBC Hibernate JPA
————————————————————————————
Template class JdbcTemplate HibernateTemplate JpaTemplate
DAO support JdbcDaoSupport HibernateDaoSupport JpaDaoSupport
Transaction DataSourceTxMgr HibernateTxMgr JpaTXManager

DaoSupport classes have setters for Template inside them – so help usage if extended.
Hib Session factory is able tomanage contextual sessions for you and allows you to
retrieve themby the getCurrentSession() method. This ensures that there will be only
one Hibernate session per transaction, so it works nicely with Spring’s transactionmanagement
support.

Using HibernateTemplatemeans your DAO has to depend on Spring’s API. To avoid that,
Use session factory . getCurrSessn()

To translate Hibernate exceptions to Spring’s DataAccessException :
1. Apply the @Repository annotation DAO class that requires exception translation.
2. Register a PersistenceExceptionTranslationPostProcessor – interprets @resource annotation.

@Repository is a stereotype annotation – get session factory auto-wired:
@Repository(“courseDao”) & @AutoWired for setSessionFactory method.

Similarly for JPA:~~~~~~ Clarify Later?….
@PersistenceContext
private EntityManager entityManager;

@PersistenceUnit
private EntityManagerFactory entityManagerFactory;

Translate the JPA exceptions into Spring:
1.@Repository(“coarseDAO”)
2. context:component-scan base-package=”com.apress.springrecipes.course.jpa

Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: