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.2.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;
...
}
See Part 2 – Adding Queries to Spring Data-JPA
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.
Want to Learn More?
Learn more about Spring by attending a Spring Training class at Chariot, or contact us for onsite training.
Pingback: Technophile Blog » Adding Queries to Spring Data-JPA
Where is the class definition for ProductService? Am I missing something?
You simply create the Service class as you would any other… in src/main/java under your package for example com.chariotsolutions.myproject.services
ProductService.java interface and create the subclass ProductServiceImpl.java implementing ProductService.java.
Regards,
Gordon Dickens
Is the source code available? Would be nice to have it on github or any other place.