Spring provides an easy way to manage transactions.
Let's see how to make our methods transactional without using any XML configuration.
We will start with Spring Java config for our application:
We have an entity representing a bank account:
We also have a repository from Spring Data JPA for Account objects:
The TransferService allows transferring money from one account to another:
Just for example sake, we are adding money to one account and before subtracting from the second one, we check if it has enough funds.
If the method wasn't transactional, we would introduce a major bug.
However, @Transactional annotation makes it eligible for rollback if the exception is thrown.
It's also important to note that many methods from the repository are transactional with default propagation, so the transaction from our service will be reused.
Let's make sure that it works by writing an integration test:
If we remove @Transactional annotation the test will fail.
Be aware that managing transactions with Spring have some traps that are described in this article.
The whole project can be found at github.
Showing posts with label spring data jpa. Show all posts
Showing posts with label spring data jpa. Show all posts
Saturday, 21 September 2013
Wednesday, 6 February 2013
Spring Data JPA sample project
In previous post I showed how to setup a sample project with JPA and Hibernate.
Even though it wasn't difficult, there was a main disadvantage - we need to do a lot of coding around our DAO objects even if we want only simple operations.
Spring Data JPA helps us reduce data access coding.
Let's start with defining dependencies:
Compared to previous project there are more dependencies because of Spring. spring-test is needed to allow our test use the spring context. And this time we're going o use HSQLDB.
pesistence.xml is much smaller because the persistence configuration will be defined in spring context.
Please note that Hibernate is still the persistence provider. The Person class is exactly the same as before.
The context is defined as:
We need to specify dataSource, entityManagerFactory and transactionManager.
The pl.mjedynak package will be scanned by spring to do autowiring.
Spring Data JPA introduces the concept of repository which is higher level of abstraction than the DAO.
In our context we define a package with the repositories.
The only thing to do to be able to manage our Person class is to create an interface that will extend CrudRepository - it gives us basic operations like save, find, delete etc.
We can of course expand it with more sophisticated methods if we want.
The integration test is almost the same as before, except it needs to use spring context.
The whole project can be found at: https://github.com/mjedynak/spring-data-jpa-example
Even though it wasn't difficult, there was a main disadvantage - we need to do a lot of coding around our DAO objects even if we want only simple operations.
Spring Data JPA helps us reduce data access coding.
Let's start with defining dependencies:
Compared to previous project there are more dependencies because of Spring. spring-test is needed to allow our test use the spring context. And this time we're going o use HSQLDB.
pesistence.xml is much smaller because the persistence configuration will be defined in spring context.
Please note that Hibernate is still the persistence provider. The Person class is exactly the same as before.
The context is defined as:
We need to specify dataSource, entityManagerFactory and transactionManager.
The pl.mjedynak package will be scanned by spring to do autowiring.
Spring Data JPA introduces the concept of repository which is higher level of abstraction than the DAO.
In our context we define a package with the repositories.
The only thing to do to be able to manage our Person class is to create an interface that will extend CrudRepository - it gives us basic operations like save, find, delete etc.
We can of course expand it with more sophisticated methods if we want.
The integration test is almost the same as before, except it needs to use spring context.
The whole project can be found at: https://github.com/mjedynak/spring-data-jpa-example
Subscribe to:
Posts (Atom)