Java Reference
In-Depth Information
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.
LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="course" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.
HibernateJpaVendorAdapter">
<property name="databasePlatform"
value="org.hibernate.dialect.DerbyDialect" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" />
</bean>
</property>
</bean>
</beans>
In the preceding bean configurations, you inject a data source into this entity manager factory. It
will override the database settings in the JPA configuration file. You can set a JPA vendor adapter to
LocalContainerEntityManagerFactoryBean to specify JPA engine-specific properties. With Hibernate as
the underlying JPA engine, you should choose HibernateJpaVendorAdapter . Other properties that are not
supported by this adapter can be specified in the jpaProperties property.
Now your JPA configuration file (i.e., persistence.xml ) can be simplified as follows because its
configurations have been ported to Spring:
<persistence ...>
<persistence-unit name="course" />
</persistence>
3-9. Persisting Objects with Spring's ORM Templates
Problem
When using an ORM framework on its own, you have to repeat certain routine tasks for each DAO
operation. For example, in a DAO operation implemented with Hibernate or JPA, you have to open and
close a session or an entity manager, and begin, commit, and roll back a transaction with the native API.
Solution
Spring's approach to simplifying an ORM framework's usage is the same as JDBC's—by defining
template classes and DAO support classes. Also, Spring defines an abstract layer on top of different
transaction management APIs. For different ORM frameworks, you only have to pick up a corresponding
transaction manager implementation. Then you can manage transactions for them in a similar way.
In Spring's data access module, the support for different data access strategies is consistent.
Table 3-4 compares the support classes for JDBC, Hibernate, and JPA.
 
Search WWH ::




Custom Search