|
| 1 | += Spring Data JPA image:https://img.shields.io/badge/Revved%20up%20by-Develocity-06A0CE?logo=Gradle&labelColor=02303A["Revved up by Develocity", link="https://ge.spring.io/scans?search.rootProjectNames=Spring Data JPA Parent"] |
| 2 | + |
| 3 | +Spring Data JPA, part of the larger https://projects.spring.io/spring-data[Spring Data] family, makes it easy to implement JPA-based repositories. |
| 4 | +This module deals with enhanced support for JPA-based data access layers. |
| 5 | +It makes it easier to build Spring-powered applications that use data access technologies. |
| 6 | + |
| 7 | +Implementing a data access layer of an application has been cumbersome for quite a while. |
| 8 | +Too much boilerplate code has to be written to execute simple queries as well as perform pagination, and auditing. |
| 9 | +Spring Data JPA aims to significantly improve the implementation of data access layers by reducing the effort to the amount that’s actually needed. |
| 10 | +As a developer you write your repository interfaces, including custom finder methods, and Spring will provide the implementation automatically. |
| 11 | + |
| 12 | +== Features |
| 13 | + |
| 14 | +* Implementation of CRUD methods for JPA Entities |
| 15 | +* Dynamic query generation from query method names |
| 16 | +* Transparent triggering of JPA NamedQueries by query methods |
| 17 | +* Implementation domain base classes providing basic properties |
| 18 | +* Support for transparent auditing (created, last changed) |
| 19 | +* Possibility to integrate custom repository code |
| 20 | +* Easy Spring integration with custom namespace |
| 21 | + |
| 22 | +include::https://raw.githubusercontent.com/spring-projects/spring-data-build/refs/heads/main/etc/readme/code-of-conduct.adoc[] |
| 23 | + |
| 24 | +== Getting Started |
| 25 | + |
| 26 | +Here is a quick teaser of an application using Spring Data Repositories in Java: |
| 27 | + |
| 28 | +[source,java] |
| 29 | +---- |
| 30 | +public interface PersonRepository extends CrudRepository<Person, Long> { |
| 31 | +
|
| 32 | + List<Person> findByLastname(String lastname); |
| 33 | +
|
| 34 | + List<Person> findByFirstnameLike(String firstname); |
| 35 | +} |
| 36 | +
|
| 37 | +@Service |
| 38 | +public class MyService { |
| 39 | +
|
| 40 | + private final PersonRepository repository; |
| 41 | +
|
| 42 | + public MyService(PersonRepository repository) { |
| 43 | + this.repository = repository; |
| 44 | + } |
| 45 | +
|
| 46 | + public void doWork() { |
| 47 | +
|
| 48 | + repository.deleteAll(); |
| 49 | +
|
| 50 | + Person person = new Person(); |
| 51 | + person.setFirstname("Oliver"); |
| 52 | + person.setLastname("Gierke"); |
| 53 | + repository.save(person); |
| 54 | +
|
| 55 | + List<Person> lastNameResults = repository.findByLastname("Gierke"); |
| 56 | + List<Person> firstNameResults = repository.findByFirstnameLike("Oli%"); |
| 57 | + } |
| 58 | +} |
| 59 | +
|
| 60 | +@Configuration |
| 61 | +@EnableJpaRepositories("com.acme.repositories") |
| 62 | +class AppConfig { |
| 63 | +
|
| 64 | + @Bean |
| 65 | + public DataSource dataSource() { |
| 66 | + return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build(); |
| 67 | + } |
| 68 | +
|
| 69 | + @Bean |
| 70 | + public JpaTransactionManager transactionManager(EntityManagerFactory emf) { |
| 71 | + return new JpaTransactionManager(emf); |
| 72 | + } |
| 73 | +
|
| 74 | + @Bean |
| 75 | + public JpaVendorAdapter jpaVendorAdapter() { |
| 76 | + HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); |
| 77 | + jpaVendorAdapter.setDatabase(Database.H2); |
| 78 | + jpaVendorAdapter.setGenerateDdl(true); |
| 79 | + return jpaVendorAdapter; |
| 80 | + } |
| 81 | +
|
| 82 | + @Bean |
| 83 | + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { |
| 84 | + LocalContainerEntityManagerFactoryBean lemfb = new LocalContainerEntityManagerFactoryBean(); |
| 85 | + lemfb.setDataSource(dataSource()); |
| 86 | + lemfb.setJpaVendorAdapter(jpaVendorAdapter()); |
| 87 | + lemfb.setPackagesToScan("com.acme"); |
| 88 | + return lemfb; |
| 89 | + } |
| 90 | +} |
| 91 | +---- |
| 92 | + |
| 93 | +include::https://raw.githubusercontent.com/spring-projects/spring-data-build/refs/heads/main/etc/readme/dependencies.adoc[] |
| 94 | + |
| 95 | +include::https://raw.githubusercontent.com/spring-projects/spring-data-build/refs/heads/main/etc/readme/getting-help.adoc[] |
| 96 | + |
| 97 | +include::https://raw.githubusercontent.com/spring-projects/spring-data-build/refs/heads/main/etc/readme/license.adoc[] |
0 commit comments