Skip to content

Commit

Permalink
Merge pull request #25 from Scalified/#24
Browse files Browse the repository at this point in the history
#24 Entities Find By Type API
  • Loading branch information
vbaidak committed May 29, 2019
2 parents 3e8ebe0 + fcc2cf5 commit e59e832
Show file tree
Hide file tree
Showing 35 changed files with 610 additions and 232 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.2.0

* [**24** Entities Find By Type API](https://github.com/Scalified/jpa/issues/24)

# 1.1.1

* [**#22** Unexpected CROSS JOIN on count with function call](https://github.com/Scalified/jpa/issues/22)
Expand Down
44 changes: 39 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,28 @@ class Person {

}

// Finding entity by key
// Finding entity by type and key
Person personJohn = jpa.find(Person.class).one("John");

// Finding entities by type
List<Person> personList = jpa.find(Person.class).list();

// Finding entities by type and mapping results to set
List<Person> personSet = jpa.find(Person.class).set();

// Finding entities by type and mapping results
List<Person> personList = jpa.find(Person.class).some(query -> query.setMaxResults(100).getResultList());

// Streaming entities by type
Stream<Person> personStream = jpa.find(Person.class).stream();

// Streaming entities by type specifying chunk size
Stream<Person> personStream = jpa.find(Person.class).stream(100);

// Finding first optional entity by type
Optional<Person> personList = jpa.find(Person.class).first();


// Finding entities by criteria function and mapping results to list
List<Person> personList = jpa.find(builder -> {
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Expand All @@ -98,16 +117,28 @@ Set<Person> personSet = jpa.find(builder -> {
return criteriaQuery.select(root);
}).set();

// Finding entities by criteria function and mapping results to stream
// Finding entities by criteria function and mapping results
Set<Person> personSet = jpa.find(builder -> {
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
return criteriaQuery.select(root);
}).some(query -> query.setMaxResults(100).getResultList());

// Streaming entities by criteria function
Stream<Person> personStream = jpa.find(builder -> {
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
return criteriaQuery.select(root);
}).stream();
// Under the hood, stream executes jpa queries for each chunk.
// In case if table is populated or modified during stream consuming, the new data will also be included into result set.

// Finding optional entity
// Streaming entities by criteria function specifying chunk size
Stream<Person> personStream = jpa.find(builder -> {
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
return criteriaQuery.select(root);
}).stream(100);

// Finding first optional entity by criteria function
Optional<Person> person = jpa.find(builder -> {
CriteriaQuery<Person> criteriaQuery = builder.createQuery(Person.class);
Root<Person> root = criteriaQuery.from(Person.class);
Expand Down Expand Up @@ -161,6 +192,9 @@ List<Person> youngFemalePersons = jpa.find(AndSpecification.of(isYoungSpecificat
List<Person> youngOrFemalePersons = jpa.find(OrSpecification.of(isYoungSpecification, isFemaleSpecification)).list();
```

> Streaming entities executes jpa queries for each chunk under the hood. In case if table is populated or modified
during stream consuming, the new data will also be included into result set.

### Query DSL

**Query** DSL provides convenient way of stored procedure queries execution
Expand Down
14 changes: 8 additions & 6 deletions build.gradle → build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ import java.nio.charset.StandardCharsets

allprojects {

group = 'com.scalified'
version = '1.1.1'
group = "com.scalified"
version = "1.2.0"

val javaeeVersion by extra("7.0")

repositories {
mavenCentral()
}

tasks.withType(JavaCompile) {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
options.encoding = StandardCharsets.UTF_8.name()
tasks.withType<JavaCompile> {
sourceCompatibility = "${JavaVersion.VERSION_1_8}"
targetCompatibility = "${JavaVersion.VERSION_1_8}"
options.encoding = Charsets.UTF_8.name()
}

}
3 changes: 1 addition & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Wed Apr 03 13:00:25 EEST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip
10 changes: 8 additions & 2 deletions settings.gradle → jpa/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,11 @@
*
*/

rootProject.name = 'jpa-root'
include 'jpa'
plugins {
`java-library`
id("maven-publish")
}

dependencies {
compileOnly("javax:javaee-api:${extra["javaeeVersion"]}")
}
5 changes: 2 additions & 3 deletions jpa/src/main/java/com/scalified/jpa/Jpa.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,13 @@
import java.util.Collection;

/**
* <p>
* The entry point to start working with <b>JPA DSL</b>
*
* <p>
* Declares root <b>DSL</b> methods within <b>DSL</b> call chain
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public interface Jpa {

Expand Down
3 changes: 1 addition & 2 deletions jpa/src/main/java/com/scalified/jpa/JpaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
* A {@link Jpa} implementation
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public class JpaImpl implements Jpa {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
* A {@link Spliterator} used to create {@link Stream} for entity chunk loading
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-08-14
*/
public class EntitySpliterator<T> implements Spliterator<T> {

Expand Down Expand Up @@ -97,7 +96,7 @@ public EntitySpliterator(TypedQuery<T> query, int chunkSize) {
* The action is performed on the next element in encounter order.
* Exceptions thrown by the action are relayed to the caller.
*
* @param action The action
* @param action the action
* @return {@code false} if no remaining elements existed
* upon entry to this method, else {@code true}.
* @throws NullPointerException if the specified action is {@code null}
Expand Down
9 changes: 5 additions & 4 deletions jpa/src/main/java/com/scalified/jpa/commons/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
* Provides utility methods for {@link java.lang.String}
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-08-18
*/
public class StringUtils {

Expand All @@ -46,7 +45,8 @@ public class StringUtils {
* Joins the elements of the provided {@code Iterable} into
* a single String containing the provided elements
*
* <p>No delimiter is added before or after the list. Null objects or empty
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the iteration are represented by empty strings
*
* @param iterable the {@code Iterable} providing the values to join together, may be null
Expand All @@ -64,7 +64,8 @@ public static String join(final Iterable<?> iterable, final char separator) {
* Joins the elements of the provided {@code Iterator} into
* a single String containing the provided elements
*
* <p>No delimiter is added before or after the list. Null objects or empty
* <p>
* No delimiter is added before or after the list. Null objects or empty
* strings within the iteration are represented by empty strings
*
* @param iterator the {@code Iterator} of values to join together, may be null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
* <b>DSL</b> for working with collection of entities
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public interface JpaEntitiesDsl<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
* A {@link JpaEntitiesDsl} implementation
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public class JpaEntitiesDslImpl<T, K extends Collection<T>> implements JpaEntitiesDsl<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
* <b>DSL</b> for working with single entity
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public interface JpaEntityDsl<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
* A {@link JpaEntityDsl} implementation
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public class JpaEntityDslImpl<T> implements JpaEntityDsl<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@
* <b>DSL</b> for finding entities using previously defined {@link CriteriaFunction}
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public interface JpaFindByCriteriaFunctionDsl<T> {

Expand All @@ -56,6 +55,16 @@ public interface JpaFindByCriteriaFunctionDsl<T> {
*/
Set<T> set();

/**
* Returns some generic result using previously defined {@link CriteriaFunction}
* and after applying the specified {@code resultFunction}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @return some generic result
*/
<R> R some(ResultFunction<T, R> resultFunction);

/**
* Returns a stream of all found entities using previously defined {@link CriteriaFunction}
*
Expand All @@ -65,23 +74,13 @@ public interface JpaFindByCriteriaFunctionDsl<T> {

/**
* Returns a stream of all found entities using previously defined {@link CriteriaFunction}
* and the specified chunk size
* and the specified {@code chunkSize}
*
* @param chunkSize size of a chunk
* @return a stream of all found entities
*/
Stream<T> stream(int chunkSize);

/**
* Returns some generic result using previously defined {@link CriteriaFunction}
* and after applying the specified <b>resultFunction</b>
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @return some generic result
*/
<R> R some(ResultFunction<T, R> resultFunction);

/**
* Returns the first found entity using previously defined {@link CriteriaFunction}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
* A {@link JpaFindByCriteriaFunctionDsl} implementation
*
* @author shell
* @version 1.0.0
* @since 1.0.0
* @since 2018-02-06
*/
public class JpaFindByCriteriaFunctionDslImpl<T> implements JpaFindByCriteriaFunctionDsl<T> {

Expand Down Expand Up @@ -87,38 +86,38 @@ public Set<T> set() {
}

/**
* Returns a stream of all found entities using previously defined {@link CriteriaFunction}
* Returns some generic result using previously defined {@link CriteriaFunction}
* and after applying the specified {@code resultFunction}
*
* @return a stream of all found entities
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @return some generic result
*/
@Override
public Stream<T> stream() {
return manager.find(function);
public <R> R some(ResultFunction<T, R> resultFunction) {
return manager.find(function, resultFunction);
}

/**
* Returns a stream of all found entities using previously defined {@link CriteriaFunction}
* and the specified chunk size
*
* @param chunkSize size of a chunk
* @return a stream of all found entities
*/
@Override
public Stream<T> stream(int chunkSize) {
return manager.find(function, chunkSize);
public Stream<T> stream() {
return manager.stream(function);
}

/**
* Returns some generic result using previously defined {@link CriteriaFunction}
* and after applying the specified <b>resultFunction</b>
* Returns a stream of all found entities using previously defined {@link CriteriaFunction}
* and the specified {@code chunkSize}
*
* @param resultFunction a function to apply on result
* @param <R> type of generic result
* @return some generic result
* @param chunkSize size of a chunk
* @return a stream of all found entities
*/
@Override
public <R> R some(ResultFunction<T, R> resultFunction) {
return manager.find(function, resultFunction);
public Stream<T> stream(int chunkSize) {
return manager.stream(function, chunkSize);
}

/**
Expand Down
Loading

0 comments on commit e59e832

Please sign in to comment.