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
Showing posts with label hibernate. Show all posts
Showing posts with label hibernate. Show all posts
Wednesday, 6 February 2013
Friday, 25 January 2013
Hibernate JPA sample project
Here's a simple example of a project that uses JPA with Hibernate as default implementation.
We're going to use the embedded Derby database.
We will also need the persistence.xml placed in META-INF directory on our classpath :
We're setting Hibernate as our persistence provider and Derby as JDBC driver.
Entity class Person is also specified as belonging to this persistent unit.
It's defined as:
In order to manage Person we need some kind of DAO. Let's define an interface PersonDao:
For simplicity it has only methods for adding person and finding all persons.
The sample implementation could look like:
And the most important - integration test that checks if everything is glued together correctly:
The whole project can be found at: https://github.com/mjedynak/hibernate-jpa-example
To sum up:
it's quite easy to create JPA with Hibernate project, however every entity class needs it's own DAO (unless we use a generic one).
In the next post I'll take a look at Spring Data Jpa project that solves that impediment.
Let's start with the required dependencies:
We will also need the persistence.xml placed in META-INF directory on our classpath :
We're setting Hibernate as our persistence provider and Derby as JDBC driver.
Entity class Person is also specified as belonging to this persistent unit.
It's defined as:
In order to manage Person we need some kind of DAO. Let's define an interface PersonDao:
For simplicity it has only methods for adding person and finding all persons.
The sample implementation could look like:
And the most important - integration test that checks if everything is glued together correctly:
The whole project can be found at: https://github.com/mjedynak/hibernate-jpa-example
To sum up:
it's quite easy to create JPA with Hibernate project, however every entity class needs it's own DAO (unless we use a generic one).
In the next post I'll take a look at Spring Data Jpa project that solves that impediment.
Subscribe to:
Posts (Atom)