Over a million developers have joined DZone.

Simpler JPA with Spring Data-JPA

DZone's Guide to

Simpler JPA with Spring Data-JPA

· Java Zone ·
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

How to configure your Spring application with Simple CRUD configuration with Spring Data-JPA.

1. Add Spring Data-JPA to project configuration. In your Maven pom.xml file


2. Configure JPA Entity

public class Product {
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private Long id;
  @Column(unique = true)
  private String productId;
  private Integer quantity;

3. Configure Typed Repository Interface

Spring Data JPA will create the beans for us.

package com.gordondickens.myapp.repository;
import org.springframework.data.repository.CrudRepository;
import com.gordondickens.myapp.entity.Product;
public interface ProductRepository
   extends CrudRepository<Product, Long> {}

4. Configure the application context

Note: this example uses Hibernate and HSQL

<!-- Directory to scan for repository classes -->
   base-package="com.gordondickens.myapp.repository" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
  <property name="entityManagerFactory"
      ref="entityManagerFactory" />
  <property name="jpaDialect">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="entityManagerFactory"
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      <property name="generateDdl" value="true" />
      <property name="database" value="HSQL" />

5. Inject the Repository

In our service class & tests we use the repository to execute our crud methods. Repository Methods: count(), exists(), delete(), deleteAll(), findOne(), findAll(), save().

public class ProductServiceImpl extends ProductService {
   ProductRepository productRepository;


The Spring Data project abstracts away basic data management concepts. In addition to support for traditional relational methodologies, Spring Data provides support for NoSQL Graph, Key-Value and Map-Reduce types. As we saw in the above example configuring CRUD style applications is fairly trivial and Spring provides a convenient configuration for the typical cookie cutter code.

Click here to see my other blog which lists the annotations in “Spring Data-Commons” and “Spring Data-JPA”.


From http://gordondickens.com/wordpress/2011/08/01/simpler-jpa-with-spring-data-jpa/

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}