forked from playframework/play-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'play-java-jpa-example/2.6.x' into 2.6.x
* play-java-jpa-example/2.6.x: (48 commits) Nest play-java-jpa-example Updated with template-control on 2019-02-13T20:25:39.298Z (playframework#87) Upgrade branch 2.6.x using TemplateControl (playframework#85) Upgrade branch 2.6.x using TemplateControl (playframework#84) Upgrade branch 2.6.x using TemplateControl (playframework#83) Updated with template-control on 2019-01-08T14:44:42.531Z (playframework#81) Updated with template-control on 2019-01-04T17:13:23.827Z (playframework#76) Upgrade branch 2.6.x using TemplateControl (playframework#71) Upgrade branch 2.6.x using TemplateControl (playframework#69) Updated with template-control on 2018-09-11T20:14:50.333Z (playframework#65) Updated with template-control on 2018-08-20T20:37:13.878Z (playframework#63) Updated with template-control on 2018-07-19T01:58:55.873Z (playframework#61) Updated with template-control on 2018-07-16T18:38:45.441Z (playframework#60) Upgrade branch 2.6.x using TemplateControl (playframework#59) Updated with template-control on 2018-05-27T23:54:09.603Z (playframework#58) Updated with template-control on 2018-04-06T19:34:58.092Z (playframework#55) Updated with template-control on 2018-03-02T18:56:28.087Z (playframework#54) Upgrade branch 2.6.x using TemplateControl (playframework#53) Fix cross build to sbt 1.1.0 (playframework#52) Upgrade branch 2.6.x using TemplateControl (playframework#51) ...
- Loading branch information
Showing
36 changed files
with
1,117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
logs | ||
target | ||
build | ||
/.idea | ||
/.idea_modules | ||
/.classpath | ||
/.project | ||
/.gradle | ||
/.settings | ||
/RUNNING_PID |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
pull_request_rules: | ||
- name: automatic merge on CI success require review | ||
conditions: | ||
- status-success=Travis CI - Pull Request | ||
- "#approved-reviews-by>=1" | ||
- "#changes-requested-reviews-by=0" | ||
- label!=block-merge | ||
actions: | ||
merge: | ||
method: squash | ||
strict: smart | ||
|
||
- name: automatic merge on CI success for TemplateControl | ||
conditions: | ||
- status-success=Travis CI - Pull Request | ||
- label=merge-when-green | ||
- label!=block-merge | ||
actions: | ||
merge: | ||
method: squash | ||
strict: smart | ||
|
||
- name: delete branch after merge | ||
conditions: | ||
- merged | ||
actions: | ||
delete_head_branch: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
language: scala | ||
scala: | ||
- 2.12.8 | ||
|
||
before_install: | ||
- curl -sL https://github.com/shyiko/jabba/raw/master/install.sh | bash && . ~/.jabba/jabba.sh | ||
|
||
env: | ||
global: | ||
- JABBA_HOME=$HOME/.jabba | ||
matrix: | ||
# There is no concise way to specify multi-dimensional build matrix: | ||
# https://github.com/travis-ci/travis-ci/issues/1519 | ||
- SCRIPT=scripts/test-sbt [email protected] | ||
- SCRIPT=scripts/test-sbt [email protected] | ||
- SCRIPT=scripts/test-gradle [email protected] | ||
- SCRIPT=scripts/test-gradle [email protected] | ||
|
||
# Exclude some combinations from build matrix. See: | ||
# https://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix | ||
matrix: | ||
fast_finish: true | ||
allow_failures: | ||
# Current release of Gradle still does not supports Play 2.7.x releases | ||
# As soon as there is a release of Gradle that fixes that, we can then | ||
# remove this allowed failure. | ||
- env: SCRIPT=scripts/test-gradle [email protected] | ||
- env: SCRIPT=scripts/test-gradle [email protected] | ||
# Java 11 is still not fully supported. It is good that we are already | ||
# testing our sample applications to better discover possible problems | ||
# but we can allow failures here too. | ||
- env: SCRIPT=scripts/test-sbt [email protected] | ||
|
||
install: | ||
- $JABBA_HOME/bin/jabba install $TRAVIS_JDK | ||
- unset _JAVA_OPTIONS | ||
- export JAVA_HOME="$JABBA_HOME/jdk/$TRAVIS_JDK" && export PATH="$JAVA_HOME/bin:$PATH" && java -Xmx32m -version | ||
|
||
script: | ||
- $SCRIPT | ||
|
||
before_cache: | ||
- find $HOME/.ivy2 -name "ivydata-*.properties" -delete | ||
- find $HOME/.sbt -name "*.lock" -delete | ||
|
||
cache: | ||
directories: | ||
- "$HOME/.ivy2/cache" | ||
- "$HOME/.gradle/caches" | ||
- "$HOME/.jabba/jdk" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
CC0 1.0 Universal | ||
|
||
Statement of Purpose | ||
|
||
The laws of most jurisdictions throughout the world automatically confer | ||
exclusive Copyright and Related Rights (defined below) upon the creator and | ||
subsequent owner(s) (each and all, an "owner") of an original work of | ||
authorship and/or a database (each, a "Work"). | ||
|
||
Certain owners wish to permanently relinquish those rights to a Work for the | ||
purpose of contributing to a commons of creative, cultural and scientific | ||
works ("Commons") that the public can reliably and without fear of later | ||
claims of infringement build upon, modify, incorporate in other works, reuse | ||
and redistribute as freely as possible in any form whatsoever and for any | ||
purposes, including without limitation commercial purposes. These owners may | ||
contribute to the Commons to promote the ideal of a free culture and the | ||
further production of creative, cultural and scientific works, or to gain | ||
reputation or greater distribution for their Work in part through the use and | ||
efforts of others. | ||
|
||
For these and/or other purposes and motivations, and without any expectation | ||
of additional consideration or compensation, the person associating CC0 with a | ||
Work (the "Affirmer"), to the extent that he or she is an owner of Copyright | ||
and Related Rights in the Work, voluntarily elects to apply CC0 to the Work | ||
and publicly distribute the Work under its terms, with knowledge of his or her | ||
Copyright and Related Rights in the Work and the meaning and intended legal | ||
effect of CC0 on those rights. | ||
|
||
1. Copyright and Related Rights. A Work made available under CC0 may be | ||
protected by copyright and related or neighboring rights ("Copyright and | ||
Related Rights"). Copyright and Related Rights include, but are not limited | ||
to, the following: | ||
|
||
i. the right to reproduce, adapt, distribute, perform, display, communicate, | ||
and translate a Work; | ||
|
||
ii. moral rights retained by the original author(s) and/or performer(s); | ||
|
||
iii. publicity and privacy rights pertaining to a person's image or likeness | ||
depicted in a Work; | ||
|
||
iv. rights protecting against unfair competition in regards to a Work, | ||
subject to the limitations in paragraph 4(a), below; | ||
|
||
v. rights protecting the extraction, dissemination, use and reuse of data in | ||
a Work; | ||
|
||
vi. database rights (such as those arising under Directive 96/9/EC of the | ||
European Parliament and of the Council of 11 March 1996 on the legal | ||
protection of databases, and under any national implementation thereof, | ||
including any amended or successor version of such directive); and | ||
|
||
vii. other similar, equivalent or corresponding rights throughout the world | ||
based on applicable law or treaty, and any national implementations thereof. | ||
|
||
2. Waiver. To the greatest extent permitted by, but not in contravention of, | ||
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and | ||
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright | ||
and Related Rights and associated claims and causes of action, whether now | ||
known or unknown (including existing as well as future claims and causes of | ||
action), in the Work (i) in all territories worldwide, (ii) for the maximum | ||
duration provided by applicable law or treaty (including future time | ||
extensions), (iii) in any current or future medium and for any number of | ||
copies, and (iv) for any purpose whatsoever, including without limitation | ||
commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes | ||
the Waiver for the benefit of each member of the public at large and to the | ||
detriment of Affirmer's heirs and successors, fully intending that such Waiver | ||
shall not be subject to revocation, rescission, cancellation, termination, or | ||
any other legal or equitable action to disrupt the quiet enjoyment of the Work | ||
by the public as contemplated by Affirmer's express Statement of Purpose. | ||
|
||
3. Public License Fallback. Should any part of the Waiver for any reason be | ||
judged legally invalid or ineffective under applicable law, then the Waiver | ||
shall be preserved to the maximum extent permitted taking into account | ||
Affirmer's express Statement of Purpose. In addition, to the extent the Waiver | ||
is so judged Affirmer hereby grants to each affected person a royalty-free, | ||
non transferable, non sublicensable, non exclusive, irrevocable and | ||
unconditional license to exercise Affirmer's Copyright and Related Rights in | ||
the Work (i) in all territories worldwide, (ii) for the maximum duration | ||
provided by applicable law or treaty (including future time extensions), (iii) | ||
in any current or future medium and for any number of copies, and (iv) for any | ||
purpose whatsoever, including without limitation commercial, advertising or | ||
promotional purposes (the "License"). The License shall be deemed effective as | ||
of the date CC0 was applied by Affirmer to the Work. Should any part of the | ||
License for any reason be judged legally invalid or ineffective under | ||
applicable law, such partial invalidity or ineffectiveness shall not | ||
invalidate the remainder of the License, and in such case Affirmer hereby | ||
affirms that he or she will not (i) exercise any of his or her remaining | ||
Copyright and Related Rights in the Work or (ii) assert any associated claims | ||
and causes of action with respect to the Work, in either case contrary to | ||
Affirmer's express Statement of Purpose. | ||
|
||
4. Limitations and Disclaimers. | ||
|
||
a. No trademark or patent rights held by Affirmer are waived, abandoned, | ||
surrendered, licensed or otherwise affected by this document. | ||
|
||
b. Affirmer offers the Work as-is and makes no representations or warranties | ||
of any kind concerning the Work, express, implied, statutory or otherwise, | ||
including without limitation warranties of title, merchantability, fitness | ||
for a particular purpose, non infringement, or the absence of latent or | ||
other defects, accuracy, or the present or absence of errors, whether or not | ||
discoverable, all to the greatest extent permissible under applicable law. | ||
|
||
c. Affirmer disclaims responsibility for clearing rights of other persons | ||
that may apply to the Work or any use thereof, including without limitation | ||
any person's Copyright and Related Rights in the Work. Further, Affirmer | ||
disclaims responsibility for obtaining any necessary consents, permissions | ||
or other rights required for any use of the Work. | ||
|
||
d. Affirmer understands and acknowledges that Creative Commons is not a | ||
party to this document and has no duty or obligation with respect to this | ||
CC0 or use of the Work. | ||
|
||
For more information, please see | ||
<http://creativecommons.org/publicdomain/zero/1.0/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Written by Lightbend <[email protected]> | ||
|
||
To the extent possible under law, the author(s) have dedicated all copyright and | ||
related and neighboring rights to this software to the public domain worldwide. | ||
This software is distributed without any warranty. | ||
|
||
You should have received a copy of the CC0 Public Domain Dedication along with | ||
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[<img src="https://img.shields.io/travis/playframework/play-java-jpa-example.svg"/>](https://travis-ci.org/playframework/play-java-jpa-example) | ||
|
||
# play-java-jpa-example | ||
|
||
This project demonstrates how to create a simple database application with Play, using JPA. | ||
|
||
Please see the Play documentation for more details: | ||
|
||
* https://www.playframework.com/documentation/latest/JavaJPA | ||
* https://www.playframework.com/documentation/latest/ThreadPools | ||
* https://www.playframework.com/documentation/latest/JavaAsync |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$ -> | ||
$.get "/persons", (persons) -> | ||
$.each persons, (index, person) -> | ||
$("#persons").append $("<li>").text person.name |
51 changes: 51 additions & 0 deletions
51
play-java-jpa-example/app/controllers/PersonController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package controllers; | ||
|
||
import models.Person; | ||
import models.PersonRepository; | ||
import play.data.FormFactory; | ||
import play.libs.concurrent.HttpExecutionContext; | ||
import play.mvc.Controller; | ||
import play.mvc.Result; | ||
|
||
import javax.inject.Inject; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.stream.Collectors; | ||
|
||
import static play.libs.Json.toJson; | ||
|
||
/** | ||
* The controller keeps all database operations behind the repository, and uses | ||
* {@link play.libs.concurrent.HttpExecutionContext} to provide access to the | ||
* {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}. | ||
*/ | ||
public class PersonController extends Controller { | ||
|
||
private final FormFactory formFactory; | ||
private final PersonRepository personRepository; | ||
private final HttpExecutionContext ec; | ||
|
||
@Inject | ||
public PersonController(FormFactory formFactory, PersonRepository personRepository, HttpExecutionContext ec) { | ||
this.formFactory = formFactory; | ||
this.personRepository = personRepository; | ||
this.ec = ec; | ||
} | ||
|
||
public Result index() { | ||
return ok(views.html.index.render()); | ||
} | ||
|
||
public CompletionStage<Result> addPerson() { | ||
Person person = formFactory.form(Person.class).bindFromRequest().get(); | ||
return personRepository.add(person).thenApplyAsync(p -> { | ||
return redirect(routes.PersonController.index()); | ||
}, ec.current()); | ||
} | ||
|
||
public CompletionStage<Result> getPersons() { | ||
return personRepository.list().thenApplyAsync(personStream -> { | ||
return ok(toJson(personStream.collect(Collectors.toList()))); | ||
}, ec.current()); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
play-java-jpa-example/app/models/DatabaseExecutionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package models; | ||
|
||
import akka.actor.ActorSystem; | ||
import scala.concurrent.ExecutionContext; | ||
import scala.concurrent.ExecutionContextExecutor; | ||
|
||
import javax.inject.Inject; | ||
|
||
/** | ||
* Custom execution context wired to "database.dispatcher" thread pool | ||
*/ | ||
public class DatabaseExecutionContext implements ExecutionContextExecutor { | ||
private final ExecutionContext executionContext; | ||
|
||
private static final String name = "database.dispatcher"; | ||
|
||
@Inject | ||
public DatabaseExecutionContext(ActorSystem actorSystem) { | ||
this.executionContext = actorSystem.dispatchers().lookup(name); | ||
} | ||
|
||
@Override | ||
public ExecutionContext prepare() { | ||
return executionContext.prepare(); | ||
} | ||
|
||
@Override | ||
public void execute(Runnable command) { | ||
executionContext.execute(command); | ||
} | ||
|
||
@Override | ||
public void reportFailure(Throwable cause) { | ||
executionContext.reportFailure(cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package models; | ||
|
||
import play.db.jpa.JPAApi; | ||
|
||
import javax.inject.Inject; | ||
import javax.persistence.EntityManager; | ||
import java.util.List; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.concurrent.CompletableFuture.supplyAsync; | ||
|
||
/** | ||
* Provide JPA operations running inside of a thread pool sized to the connection pool | ||
*/ | ||
public class JPAPersonRepository implements PersonRepository { | ||
|
||
private final JPAApi jpaApi; | ||
private final DatabaseExecutionContext executionContext; | ||
|
||
@Inject | ||
public JPAPersonRepository(JPAApi jpaApi, DatabaseExecutionContext executionContext) { | ||
this.jpaApi = jpaApi; | ||
this.executionContext = executionContext; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Person> add(Person person) { | ||
return supplyAsync(() -> wrap(em -> insert(em, person)), executionContext); | ||
} | ||
|
||
@Override | ||
public CompletionStage<Stream<Person>> list() { | ||
return supplyAsync(() -> wrap(em -> list(em)), executionContext); | ||
} | ||
|
||
private <T> T wrap(Function<EntityManager, T> function) { | ||
return jpaApi.withTransaction(function); | ||
} | ||
|
||
private Person insert(EntityManager em, Person person) { | ||
em.persist(person); | ||
return person; | ||
} | ||
|
||
private Stream<Person> list(EntityManager em) { | ||
List<Person> persons = em.createQuery("select p from Person p", Person.class).getResultList(); | ||
return persons.stream(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package models; | ||
|
||
import javax.persistence.*; | ||
|
||
@Entity | ||
public class Person { | ||
|
||
@Id | ||
@GeneratedValue(strategy=GenerationType.AUTO) | ||
public Long id; | ||
|
||
public String name; | ||
} |
Oops, something went wrong.