Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 31 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
# specification-with-projection
Support Projections with `JpaSpecificationExecutor.findAll(Specification,Pageable)` for Spring Data JPA

>version 2.x.x for Spring Data JPA 2.x (Spring Boot 2.x)
>version 2.x.x for Spring Data JPA 2.x (Spring Boot 2.x)

>version 1.x.x Spring Data JPA 1.x
>version 1.x.x Spring Data JPA 1.x

## How to use
* add dependency to pom
```xml
<!-- for Spring Data 2.x -->
<!-- for Spring Data 3.x -->
<dependency>
<groupId>th.co.geniustree.springdata.jpa</groupId>
<artifactId>specification-with-projections</artifactId>
<version>2.0.1</version>
<groupId>th.co.geniustree.springdata.jpa</groupId>
<artifactId>specification-with-projections</artifactId>
<version>3.0.0</version>
</dependency>
<!-- for Spring Data 2.x -->
<dependency>
<groupId>th.co.geniustree.springdata.jpa</groupId>
<artifactId>specification-with-projections</artifactId>
<version>2.0.1</version>
</dependency>
```
```xml
<!-- for Spring Data 1.x -->
<dependency>
<groupId>th.co.geniustree.springdata.jpa</groupId>
<artifactId>specification-with-projections</artifactId>
<version>1.0.6</version>
<groupId>th.co.geniustree.springdata.jpa</groupId>
<artifactId>specification-with-projections</artifactId>
<version>1.0.6</version>
</dependency>
```
* add annotation `@EnableJpaRepositories(repositoryBaseClass = JpaSpecificationExecutorWithProjectionImpl.class)` on Application class (Spring Boot)
* create your repository and extends `JpaSpecificationExecutorWithProjection`
```java
public interface DocumentRepository extends JpaRepository<Document,Integer>,JpaSpecificationExecutorWithProjection<Document,Integer> {
/**
* projection interface
**/
public static interface DocumentWithoutParent{
Long getId();
String getDescription();
String getDocumentType();
String getDocumentCategory();
List<DocumentWithoutParent> getChild();
}
/**
* projection interface
**/
public static interface DocumentWithoutParent{
Long getId();
String getDescription();
String getDocumentType();
String getDocumentCategory();
List<DocumentWithoutParent> getChild();
}
}
```
* use it
Expand All @@ -50,12 +56,12 @@ public interface DocumentRepository extends JpaRepository<Document,Integer>,JpaS
}
```
* version 1.0.3 RELEASE add support for @NamedEntityGraph and AD-HOC entity graph (via JpaEntityGraph)
http://docs.spring.io/spring-data/jpa/docs/1.10.4.RELEASE/reference/html/#jpa.entity-graph
http://docs.spring.io/spring-data/jpa/docs/1.10.4.RELEASE/reference/html/#jpa.entity-graph
```java
JpaEntityGraph jpaEntityGraph = new JpaEntityGraph(
"birth.sistRecvTm",
EntityGraph.EntityGraphType.FETCH,
new String[]{"sistRecvTm","birth","transfer","merger"}
);
BirthWithoutChild birth = birthRepository.findAll(createSpecBirth(searchData, type.toUpperCase()), BirthWithoutChild.class,jpaEntityGraph,pageable);
"birth.sistRecvTm",
EntityGraph.EntityGraphType.FETCH,
new String[]{"sistRecvTm","birth","transfer","merger"}
);
BirthWithoutChild birth = birthRepository.findAll(createSpecBirth(searchData, type.toUpperCase()), BirthWithoutChild.class,jpaEntityGraph,pageable);
```
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<version>3.0.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

Expand Down Expand Up @@ -74,11 +74,11 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
<version>6.1.7.Final</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand All @@ -87,7 +87,7 @@
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<version>3.8.1</version>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import javax.persistence.Tuple;
import javax.persistence.TupleElement;
import jakarta.persistence.Tuple;
import jakarta.persistence.TupleElement;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -48,7 +48,7 @@ public Object convert(Object source) {
}
}

return new TupleConverter.TupleBackedMap(tuple);
return new TupleBackedMap(tuple);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.springframework.data.util.Optionals;
import org.springframework.util.Assert;

import javax.persistence.EntityManager;
import jakarta.persistence.EntityManager;
import java.util.*;
import java.util.function.BiConsumer;

Expand Down Expand Up @@ -109,7 +109,7 @@ private QueryHints combineHints() {
return QueryHints.from(forCounts ? metadata.getQueryHintsForCount() : metadata.getQueryHints(), getFetchGraphs());
}

private org.springframework.data.jpa.repository.support.QueryHints getFetchGraphs() {
private QueryHints getFetchGraphs() {
return Optionals
.mapIfAllPresent(entityManager, metadata.getEntityGraph(),
(em, graph) -> Jpa21Utils.getFetchGraphHint(em, getEntityGraph(graph), information.getJavaType()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@
import org.springframework.data.repository.query.ReturnTypeWarpper;
import org.springframework.data.repository.query.ReturnedType;
import org.springframework.data.repository.query.TupleConverter;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import th.co.geniustree.springdata.jpa.repository.JpaSpecificationExecutorWithProjection;

import javax.persistence.*;
import javax.persistence.criteria.*;
import javax.persistence.metamodel.Attribute;
import javax.persistence.metamodel.Bindable;
import javax.persistence.metamodel.ManagedType;
import javax.persistence.metamodel.PluralAttribute;
import jakarta.persistence.*;
import jakarta.persistence.criteria.*;
import jakarta.persistence.metamodel.Attribute;
import jakarta.persistence.metamodel.Bindable;
import jakarta.persistence.metamodel.ManagedType;
import jakarta.persistence.metamodel.PluralAttribute;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Member;
import java.util.*;

import static javax.persistence.metamodel.Attribute.PersistentAttributeType.*;
import static jakarta.persistence.metamodel.Attribute.PersistentAttributeType.*;


/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package th.co.geniustree.springdata.jpa.domain;

import javax.persistence.*;
import jakarta.persistence.*;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package th.co.geniustree.springdata.jpa.domain;

import javax.persistence.*;
import jakarta.persistence.*;
import java.io.Serializable;

/**
Expand Down