Skip to content

Commit

Permalink
Merge pull request #29 from Scalified/#28
Browse files Browse the repository at this point in the history
#28 AbstractRepository
  • Loading branch information
vbaidak committed Aug 17, 2019
2 parents 828d150 + 38242b7 commit de57f45
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java

jdk:
- oraclejdk8
- openjdk8

before_install:
- chmod +x gradlew
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Person> {

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

```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion jpa/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ plugins {
}

dependencies {
compileOnly("javax:javaee-api:${extra["javaeeVersion"]}")
compileOnly("javax:javaee-api:${project.extra["javaeeVersion"]}")
}
Original file line number Diff line number Diff line change
@@ -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
*
* <p>
* Declares common repositories methods
*
* @author shell
* @since 2019-08-17
*/
public abstract class AbstractRepository<T> {

/**
* 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
*
* <p>
* 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
*
* <p>
* 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();
}

}

0 comments on commit de57f45

Please sign in to comment.