DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Simpler JPA with Spring Data-JPA

Simpler JPA with Spring Data-JPA

Gordon Dickens user avatar by
Gordon Dickens
·
Aug. 08, 11 · Java Zone · Interview
Like (0)
Save
Tweet
17.70K Views

Join the DZone community and get the full member experience.

Join For Free

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

<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-jpa</artifactId>
   <version>1.0.0.RELEASE</version>
</dependency>

2. Configure JPA Entity

...
 
@Entity
public class Product {
 
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "id")
  private Long id;
 
  @NotNull
  @Column(unique = true)
  private String productId;
 
  @NotNull
  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 -->
<jpa:repositories
   base-package="com.gordondickens.myapp.repository" />
 
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
  id="transactionManager">
  <property name="entityManagerFactory"
      ref="entityManagerFactory" />
  <property name="jpaDialect">
    <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
  </property>
</bean>
 
<bean id="entityManagerFactory"
  class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
  <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" />
    </bean>
  </property>
</bean>

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 {
 
   @Autowired
   ProductRepository productRepository;
 
  ...
}

Summary

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/

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 8 Must-Have Project Reports You Can Use Today
  • Troubleshooting HTTP 502 Bad Gateway in AWS EBS
  • C++ Creator Bjarne Stroustrup Interview
  • What Is High Cardinality?

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo