Mind is Software

Ying’s thoughts about software and business

Spring Data JPA

This article is a note of Spring Data Access.

1 Transaction Management

Spring provides a consistent declarative and pgrogrammatic programming model across different transaction APIs including JTA, Hibernate, JDBC and JPA.

A transaction stragegy is defined by the following service provider interface(SPI):

public interface PlatformTransactionManager {

    TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;

    void commit(TransactionStatus status) throws TransactionException;

    void rollback(TransactionStatus status) throws TransactionException;
}

A TransactionStatus is associated with a thread of execution. It is stored in ThreadLocal. The TransactionDefinition interface specifies propagation, isolation, timeout, and read-only status (useful optimization in Hibernate).

The JDBC transaction manager is DataSourceTransactionManager that need a reference to a data source. To use Hibernate local transactions HibernateTransactionManager, you need to define a LocalSessionFactoryBean that needs a reference to the SessionFactory and a reference to DataSource.

@Transactional only works with a class (better a concrete class) or a public method. in proxy mode, only external method calls are inercepted. Self-invocation needs AspectJ mode to use transaction.

You can also use the Spring Framework’s @Transactional support outside of a Spring container by means of an AspectJ aspect. To do so, first annotate your classes (and optionally your classes’ methods) with the @Transactional annotation, and then link (weave) your application with the AnnotationTransactionAspect. To weave your applications with the AnnotationTransactionAspect, you must either build your application with AspectJ or use load-time weaving.

You can use either TransactionTemplate or a PlatformTransactionManager. Using the TransactionTemplate absolutely couples you to Spring’s transaction infrastructure and APIs.

2 DAO and JDBC

The @Repository annotation provides exception translation and configuration. Any DAO needs access to a persistence resource such as JDBC DataSource, Hibernate SessionFactory, JPA EntityManger. Use @Autowired, @Inject, @Resource or @PersistenceContext (for EntityManager) annontation to inject it.

There are several approaches to use JDBC.

  • JdbcTemplate: can run SQL statements and perform iteration and call a callback function. It supports batch operations.
  • NamedParameterJdbcTemplate: support named parameters with the SqlParameterSource interface.
  • SimpleJdbcInsert and SimpleJdbcCall: use database metadata to simplify access.
  • MappingSqlQuery, SqlUpdate, and StoredProcedure: access database in a more OO manner.

3 Spring Data JPA

The Repository is a marker interface that takes the domain class to manage as well as the ID type of the domain class as type arguments. The CrudRepository provides CRUD functions. The PagingAndSortingRepository adds additinal paging functions.


Share