Mandroid
Mandroid

Reputation: 7494

A query on Spring Data API documentation

From Spring Data JPA documentation page:

Spring Data JPA provides repository support for Java Persistence API(JPA). It simplifies development of applications which need to access JPA data sources.

I understand what is JPA API, and have worked with Hibernate. I understand that JPA provides specification for an ORM library on how to map a Java object to a record in a RDBMS table.

But I am not able to understand what does 'repository support' here means. And what exactly is a JPA data source? Isn't JPA a spec which a ORM library can implement, while a data source may be something like a RDBMS database?

Upvotes: 1

Views: 90

Answers (1)

davidxxx
davidxxx

Reputation: 131356

And what exactly is a JPA data source? Isn't JPA a spec which a ORM library can implement, while a data source may be something like a RDBMS database?

Not really, JPA datasource is a JPA concept.
You have two kinds of them : JTA datasources and no JTA datasources.
The first one is provided generally by Java EE Servers (JBoss, Weblogic...).
If you use a lightweight container (Tomcat, Jetty, ...) such as these provided by Spring Boot, by default you will not rely on JTA, so you will use a no JTA datasource.

JPA implementations (Hibernate, EclipseLink, ...) provide support for the two datasource modes.

But I am not able to understand what does 'repository support' here means.

The Spring Data repository is a very wide concept with many features but we can summarize it as the doc states :

The goal of the Spring Data repository abstraction is to significantly reduce the amount of boilerplate code required to implement data access layers for various persistence stores.

Repository takes its origin from the DDD approach. You can find a summarize of its purpose and how to use it here.
In Spring, Repository has several flavors. I use generally :

  • Repository : a minimal support (free to you to declare crud methods that you want to use).
  • CrudRepository : a generic DAO with all crud methods provided out of the box.
  • JpaRepository that includes CrudRepository features and adds specifications coming from the DDD approach and pagination support.

And at last repositories also benefit from methods automatically created from declared methods in the interface of your repositories.

That is a simple example from the documentation :

import org.springframework.data.repository.*;

interface UserRepository extends CrudRepository<User, Long> {    
  long deleteByLastname(String lastname);    
  List<User> removeByLastname(String lastname);
}

With just this simple code and the correct Spring configuration, when a Spring container is started, a userRepository bean is instantiated with all crud methods implemented (those declared in CrudRepository) and deleteByLastname() and removeByLastname() would have a generated body with the query implementation that performs what their naming, their parameters and their return type expose(some convention are so to respect).

You can so use the bean such as :

@Autowired
UserRepository repository;

public void doSomeThings(User userToInsert, String lastnameTodelete){
   repository.save(user); // provided crud method
   repository.deleteByLastname(lastnameTodelete); // generated method thanks to convention
}

Upvotes: 1

Related Questions