Skip to content

Commit 9b41403

Browse files
authored
Spring data jdbc module (#7)
* experimentation with spring data jdbc * fixed database in pom.xml for jdbc module * finished implementation of QuerydslJdbcRepository * added query type as a generic parameter to QuerydslJdbcRepository added reflective resolution of parameter * changed constructor resolution to use spring data jdbc infrastructure * added javadoc to QuerydslJdbcRepository * refactoring of jdbc tests * updated README.md * updated README.md * updated README.md * bumped to 3.0.0-SNAPSHOT * updated README.md breaking changes section * renamed CustomNamingStrategy to PascalCaseNamingStrategy
1 parent ac37cf9 commit 9b41403

37 files changed

+1190
-188
lines changed

README.md

Lines changed: 130 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,149 @@
1-
# Infobip Spring Data JPA Querydsl
1+
# Infobip Spring Data Querydsl
22

33
[![Build Status](https://travis-ci.org/infobip/infobip-spring-data-jpa-querydsl.svg?branch=master)](https://travis-ci.org/infobip/infobip-spring-data-jpa-querydsl)
44
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.infobip/infobip-spring-data-jpa-querydsl/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.infobip/infobip-spring-data-jpa-querydsl)
55
[![Coverage Status](https://coveralls.io/repos/github/infobip/infobip-spring-data-jpa-querydsl/badge.svg?branch=master)](https://coveralls.io/github/infobip/infobip-spring-data-jpa-querydsl?branch=master)
66

7-
Infobip Spring Data JPA Querydsl provides new functionality that enables the user to leverage the full power of Querydsl API on top of Spring Data repository infrastructure.
7+
Infobip Spring Data Querydsl provides new functionality that enables the user to leverage the full power of Querydsl API on top of Spring Data repository infrastructure.
8+
9+
The project is divided into 2 modules: infobip-spring-data-jdbc-querydsl and infobip-spring-data-jpa-querydsl.
810

911
## Contents
1012

1113
1. [News](#News)
12-
2. [Features and examples](#FeaturesAndExamples)
13-
* [Native queries with Querydsl](#NativeQueriesWithQuerydsl)
14-
* [Projections](#Projections)
15-
* [Query](#Query)
16-
* [Update](#Update)
17-
* [Delete](#Delete)
18-
* [List instead of Iterable return type](#ListInsteadOfIterableReturnType)
19-
* [Transactional support](#TransactionalSupport)
20-
* [Stored procedure builder](#StoredProcedureBuilder)
21-
3. [Setup](#Setup)
14+
2. [JDBC module:](#JDBC)
15+
* [Requirements](#JDBCRequirements)
16+
* [Setup](#JDBCSetup)
17+
3. [JPA module:](#JPA)
18+
* [Requirements](#JPARequirements)
19+
* [Setup](#JPASetup)
20+
* [Features and examples:](#JPAFeaturesAndExamples)
21+
* [Native queries with Querydsl](#JPANativeQueriesWithQuerydsl)
22+
* [Projections](#JPAProjections)
23+
* [Query](#JPAQuery)
24+
* [Update](#JPAUpdate)
25+
* [Delete](#JPADelete)
26+
* [List instead of Iterable return type](#JPAListInsteadOfIterableReturnType)
27+
* [Transactional support](#JPATransactionalSupport)
28+
* [Stored procedure builder](#JPAStoredProcedureBuilder)
2229
4. [Domain Driven Design concerns](#DomainDrivenDesignConcerns)
23-
5. [Requirements](#Requirements)
24-
6. [Further reading](#FurtherReading)
25-
7. [Running tests](#RunningTests)
26-
8. [Contributing](#Contributing)
27-
9. [License](#License)
30+
5. [Further reading](#FurtherReading)
31+
6. [Running tests](#RunningTests)
32+
7. [Contributing](#Contributing)
33+
8. [License](#License)
2834

2935
## <a name="News"></a> News
3036

31-
### 2.0.1
37+
### 3.0.0
38+
39+
* Breaking changes:
40+
* renamed `@EnableExtendedRepositories` to `@EnableExtendedJpaRepositories`
41+
* renamed `ExtendedQueryDslJpaRepository` to `ExtendedQuerydslJpaRepository`
42+
* Added new module - infobip-spring-data-jdbc-querydsl.
43+
44+
## <a name="JDBC"></a> JDBC module:
45+
46+
## <a name="JDBCRequirements"></a> Requirements:
47+
48+
- Java 8 with [parameter names preserved in byte code](https://stackoverflow.com/a/20594685/607767) (used to map columns to constructor parameters)
49+
- Spring Data JDBC
50+
- Querydsl
51+
52+
### <a name="JDBCSetup"></a> Setup:
53+
54+
1. Generate [querydsl Q (query) classes](http://www.querydsl.com/static/querydsl/4.1.3/reference/html_single/#d0e725).
55+
As an example how to do this check out infobip-spring-data-jdbc-querydsl pom.xml and test code.
56+
57+
2. Dependency:
58+
59+
```xml
60+
<dependency>
61+
<groupId>com.infobip</groupId>
62+
<artifactId>infobip-spring-data-jdbc-querydsl</artifactId>
63+
<version>${infobip-spring-data-jdbc-querydsl.version}</version>
64+
</dependency>
65+
```
66+
67+
3. Add @EnableQuerydslJdbcRepositories to your Main class:
68+
69+
```java
70+
@EnableQuerydslJdbcRepositories // replaces @EnableJpaRepositories
71+
@SpringBootApplication
72+
public class Main {
73+
74+
public static void main(String[] args) {
75+
new SpringApplicationBuilder(Main.class).run(args);
76+
}
77+
}
78+
```
79+
80+
3. Refactor repository interfaces to use `QuerydslJdbcRepository` instead of `CrudRepository`:
81+
82+
```java
83+
interface FooRepository extends QuerydslJdbcRepository<Foo, QFoo, ID> {
84+
}
85+
```
86+
87+
4. Done
88+
89+
## <a name="JPA"></a> JPA module:
90+
91+
## <a name="JPARequirements"></a> Requirements:
92+
93+
- Java 8
94+
- Hibernate (if you need support for other JPA implementors please open an issue)
95+
- Spring Data JPA
96+
- Querydsl
97+
98+
### <a name="JPASetup"></a> Setup:
99+
100+
1. Dependency:
101+
102+
```xml
103+
<dependency>
104+
<groupId>com.infobip</groupId>
105+
<artifactId>infobip-spring-data-jpa-querydsl</artifactId>
106+
<version>${infobip-spring-data-jpa-querydsl.version}</version>
107+
</dependency>
108+
```
109+
110+
As this project depends on querydsl-apt with jpa classifier you don't need to set up explicit Maven build phase for Q classes generation.
111+
For building Q classes without Maven, make sure your IDE has Annotation processing enabled.
32112

33-
Changed scope of SimpleExtendedQueryDslJpaRepository to public.
113+
2. Add @EnableExtendedJpaRepositories to your Main class:
114+
115+
```java
116+
@EnableExtendedJpaRepositories // replaces @EnableJpaRepositories
117+
@SpringBootApplication
118+
public class Main {
119+
120+
public static void main(String[] args) {
121+
new SpringApplicationBuilder(Main.class).run(args);
122+
}
123+
}
124+
```
34125

126+
3. Refactor repository interfaces to use `ExtendedQueryDslJpaRepository` instead of `JpaRepository` and `QueryDslPredicateExecutor` (note that ExtendedQueryDslJpaRepository extends and provides the API of both):
35127

36-
### 2.0.0
128+
```java
129+
// ExtendedQueryDslJpaRepository replaces both JpaRepository and QueryDslPredicateExecutor
130+
interface FooRepository extends ExtendedQueryDslJpaRepository<Foo, ID> {
131+
}
132+
```
37133

38-
Upgrade to Spring Data 2 (Spring Boot 2).
134+
4. Done
39135

40-
Breaking change: new repository methods introduced in Spring Data 2 `CrudRepository#findById(Object)` and
41-
`QuerydslPredicateExecutor#findOne(Predicate)` replace old `ExtendedQueryDslJpaRepository.findOneById(Object)`
42-
and `ExtendedQueryDslJpaRepository.findOneByPredicate(Predicate)` (they were removed).
136+
If you need other features from `@EnableJpaRepositories` you can use:
43137

138+
```
139+
@EnableJpaRepositories(repositoryBaseClass = SimpleExtendedQueryDslJpaRepository.class)
140+
```
44141

45-
## <a name="FeaturesAndExamples"></a> Features and examples:
142+
### <a name="JPAFeaturesAndExamples"></a> Features and examples:
46143

47144
All examples have corresponding tests in the project and can be found [here](https://github.com/infobip/infobip-spring-data-jpa-querydsl/blob/master/src/test/java/com/infobip/spring/data/SqlServerQueryDslJpaRepositoryTest.java).
48145

49-
### <a name="NativeQueriesWithQuerydsl"></a> Native queries with Querydsl:
146+
#### <a name="JPANativeQueriesWithQuerydsl"></a> Native queries with Querydsl:
50147

51148
Example which uses union clause (unions aren't available in JPA):
52149

@@ -67,7 +164,7 @@ List<Person> actual = repository.jpaSqlQuery(query -> query
67164
);
68165
```
69166

70-
### <a name="Projections"></a> Projections
167+
#### <a name="JPAProjections"></a> Projections
71168

72169
For examples how to construct projections refer to the official documentation - [section result handling](http://www.querydsl.com/static/querydsl/latest/reference/html_single/#result_handling).
73170

@@ -87,7 +184,7 @@ List<PersonProjection> actual = repository.query(query -> query
87184
.fetch());
88185
```
89186

90-
### <a name="Query"></a> Query
187+
#### <a name="JPAQuery"></a> Query
91188

92189
Query exposes full API of JPAQuery ([QueryDslPredicateExecutor](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/querydsl/QuerydslPredicateExecutor.html)
93190
only exposes where clause (Predicate) and order clause (OrderSpecifier)).
@@ -105,7 +202,7 @@ List<Person> actual = repository.query(query -> query
105202
.fetch());
106203
```
107204

108-
### <a name="Update"></a> Update
205+
#### <a name="JPAUpdate"></a> Update
109206

110207
```
111208
repository.update(query -> query
@@ -114,23 +211,23 @@ repository.update(query -> query
114211
.execute());
115212
```
116213

117-
### <a name="Delete"></a> Delete
214+
#### <a name="JPADelete"></a> Delete
118215

119216
```
120217
long numberOfAffectedRows = repository.deleteWhere(person.firstName.like("John%"));
121218
```
122219

123-
### <a name="ListInsteadOfIterableReturnType"></a> List instead of Iterable return type
220+
#### <a name="JPAListInsteadOfIterableReturnType"></a> List instead of Iterable return type
124221

125222
[QueryDslPredicateExecutor](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/querydsl/QuerydslPredicateExecutor.html)#findAll methods return Iterable which can be cumbersome to use.
126223
Those methods were overridden and now return a List which is easier to use and is easier to convert to Stream.
127224

128-
### <a name="TransactionalSupport"></a> Transactional support
225+
#### <a name="JPATransactionalSupport"></a> Transactional support
129226

130227
Query execution is always done inside the repository implementation (loan pattern) in a transaction so transactions don't have to be
131228
handled manually (like they do if you are manually managing JPAQuery and other Querydsl constructs).
132229

133-
### <a name="StoredProcedureBuilder"></a> Stored procedure builder
230+
#### <a name="JPAStoredProcedureBuilder"></a> Stored procedure builder
134231

135232
JPA support for stored procedures is quite cumbersome and it also requires a reference to EntityManager which leads to code like this:
136233

@@ -163,50 +260,6 @@ public List<Person> delete(Person personToDelete) {
163260
}
164261
```
165262

166-
## <a name="Setup"></a> Setup:
167-
168-
1. Dependency:
169-
170-
```xml
171-
<dependency>
172-
<groupId>com.infobip</groupId>
173-
<artifactId>infobip-spring-data-jpa-querydsl</artifactId>
174-
<version>${infobip-spring-data-jpa-querydsl.version}</version>
175-
</dependency>
176-
```
177-
178-
As this project depends on querydsl-apt with jpa classifier you don't need to set up explicit Maven build phase for Q classes generation.
179-
For building Q classes without Maven, make sure your IDE has Annotation processing enabled.
180-
181-
2. Add @EnableExtendedRepositories to your Main class:
182-
183-
```java
184-
@EnableExtendedRepositories // replaces @EnableJpaRepositories
185-
@SpringBootApplication
186-
public class Main {
187-
188-
public static void main(String[] args) {
189-
new SpringApplicationBuilder(Main.class).run(args);
190-
}
191-
}
192-
```
193-
194-
3. Refactor repository interfaces to use `ExtendedQueryDslJpaRepository` instead of `JpaRepository` and `QueryDslPredicateExecutor` (note that ExtendedQueryDslJpaRepository extends and provides the API of both):
195-
196-
```java
197-
// ExtendedQueryDslJpaRepository replaces both JpaRepository and QueryDslPredicateExecutor
198-
interface FooRepository extends ExtendedQueryDslJpaRepository<Foo, ID> {
199-
}
200-
```
201-
202-
4. Done
203-
204-
If you need other features from `@EnableJpaRepositories` you can use:
205-
206-
```
207-
@EnableJpaRepositories(repositoryBaseClass = SimpleExtendedQueryDslJpaRepository.class)
208-
```
209-
210263
## <a name="DomainDrivenDesignConcerns"></a> Domain Driven Design concerns
211264

212265
In following example one could argue that database related logic has leaked from repository to service layer:
@@ -291,13 +344,6 @@ class FooService {
291344
}
292345
```
293346

294-
## <a name="Requirements"></a> Requirements:
295-
296-
- Java 8
297-
- Hibernate (if you need support for other JPA implementors please open an issue)
298-
- Spring Data
299-
- Querydsl
300-
301347
## <a name="FurtherReading"></a> Further reading
302348

303349
- [Querydsl documentation](http://www.querydsl.com/static/querydsl/latest/reference/html_single/)

0 commit comments

Comments
 (0)