diff --git a/.travis.yml b/.travis.yml index d395957..96663e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: java jdk: - - oraclejdk8 + - openjdk8 before_install: - chmod +x gradlew diff --git a/CHANGELOG.md b/CHANGELOG.md index f4a0978..9060b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.3.0 + +* [**28** AbstractRepository](https://github.com/Scalified/jpa/issues/28) + # 1.2.1 * [**26** Specification getType Default Implementation Fix](https://github.com/Scalified/jpa/issues/26) diff --git a/README.md b/README.md index 03a9972..e51241b 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,58 @@ jpa.entities(persons).refresh(); jpa.entities(persons).detach(); ``` +### AbstractRepository + +**Jpa** provides an **AbstractRepository** abstract class, which provides common repositories methods: + +```java +Jpa jpa; +// ... jpa initialization skipped + +@Entity +class Person { + + @Id + Integer id; + + String name; + + Person(Integer id, String name) { + this.id = id; + this.name = name; + } + + void setName(String name) { + this.name = name; + } + +} + +// Repository +import com.scalified.jpa.repository.AbstractRepository + +class PersonRepository extends AbstractRepository { + + PersonRepository(Jpa jpa) { + super(jpa); + } + +} + +PersonRepository repository = new PersonRepository(jpa); +Person person = new Person(1, "John"); + +// Adding an entity +repository.add(person); + +// Replacing an entity +person.setName("Alice"); +repository.replace(person); + +// Removing an entity +repository.remove(person); +``` + ## License ``` diff --git a/build.gradle.kts b/build.gradle.kts index 637273b..a599536 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,7 +28,7 @@ import java.nio.charset.StandardCharsets allprojects { group = "com.scalified" - version = "1.2.1" + version = "1.3.0" val javaeeVersion by extra("7.0") diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index ea13fdf..ef9a9e0 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-5.3.1-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-5.6-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/jpa/build.gradle.kts b/jpa/build.gradle.kts index d86a3ea..789eb97 100644 --- a/jpa/build.gradle.kts +++ b/jpa/build.gradle.kts @@ -29,5 +29,5 @@ plugins { } dependencies { - compileOnly("javax:javaee-api:${extra["javaeeVersion"]}") + compileOnly("javax:javaee-api:${project.extra["javaeeVersion"]}") } diff --git a/jpa/src/main/java/com/scalified/jpa/repository/AbstractRepository.java b/jpa/src/main/java/com/scalified/jpa/repository/AbstractRepository.java new file mode 100644 index 0000000..435affc --- /dev/null +++ b/jpa/src/main/java/com/scalified/jpa/repository/AbstractRepository.java @@ -0,0 +1,89 @@ +/* + * MIT License + * + * Copyright (c) 2019 Scalified + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package com.scalified.jpa.repository; + +import com.scalified.jpa.Jpa; + +/** + * An abstract repository to extend by concrete repositories + * + *

+ * Declares common repositories methods + * + * @author shell + * @since 2019-08-17 + */ +public abstract class AbstractRepository { + + /** + * An underlying {@link Jpa} instance + */ + protected final Jpa jpa; + + /** + * Creates this instance + * + * @param jpa underlying {@link Jpa} instance + */ + protected AbstractRepository(Jpa jpa) { + this.jpa = jpa; + } + + /** + * Adds an entity object by calling {@code JPA} insert operation + * + *

+ * Returns the added entity object + * + * @param entity an entity object to add + * @return added entity object + */ + public T add(T entity) { + return jpa.entity(entity).insert(); + } + + /** + * Replaces an entity object by calling {@code JPA} update operation + * + *

+ * Returns the replaced entity object + * + * @param entity an entity object to replace + * @return replaced entity object + */ + public T replace(T entity) { + return jpa.entity(entity).update(); + } + + /** + * Removes an entity object by calling {@code JPA} delete operation + * + * @param entity an entity object to remove + */ + public void remove(T entity) { + jpa.entity(entity).delete(); + } + +}