https://github.com/xvik/guice-persist-orient-play-example
Play framework integration sample for guice-persist-orient
https://github.com/xvik/guice-persist-orient-play-example
guice orientdb play
Last synced: 7 months ago
JSON representation
Play framework integration sample for guice-persist-orient
- Host: GitHub
- URL: https://github.com/xvik/guice-persist-orient-play-example
- Owner: xvik
- License: other
- Created: 2016-09-26T21:07:20.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-09-26T22:16:33.000Z (about 9 years ago)
- Last Synced: 2025-02-04T21:45:47.649Z (9 months ago)
- Topics: guice, orientdb, play
- Language: Java
- Size: 1020 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# [Play](https://www.playframework.com/) integration for [guice-persist-orient](https://github.com/xvik/guice-persist-orient) example
### About
Sample prepared with activator 1.3.10 for play 2.5.8 using guice-persist-orient 3.2.0 (orient 2.1.23).
[Official Java seed](https://github.com/playframework/playframework/tree/master/templates/play-java) was used.[play java docs](https://www.playframework.com/documentation/2.5.x/JavaHome)
### Run
Run app either from IDE or using activator:
```
$ bin/activator run
```Open [http://localhost:9000](http://localhost:9000) to see default home screen. Useful to check that guice context correctly started.
Next open [http://localhost:9000/samples](http://localhost:9000/samples) to see sample json output - demo of using repository for accessing db.
### Integration
guice-persist-orient dependency ([build.sbt](build.sbt)):
```scala
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
"ru.vyarus" % "guice-persist-orient" % "3.2.0"
)
```Guice module (recognized by play convention) ([Module](app/Module.java)):
```java
@Override
public void configure() {
// in normal application fb config should be obtained from configuration
install(new OrientModule("memory:test", "admin", "admin"));
// initialize scheme from models in package
install(new PackageSchemeModule(Sample.class.getPackage().getName()));
// enable repositories support
install(new RepositoryModule());// custom data initializer
bind(DataInitializer.class).to(Bootstrap.class);
// bind to play lifecycle (to start / stop persistence support)
bind(PersistenceLifecycle.class).asEagerSingleton();
}
```[Sample](app/model/Sample.java) class will be registered as scheme orient.
[Bootstrap](app/Bootstrap.java) will insert 10 `Sample` records on startup (if table is empty)
[PersistentLifecycle](app/PersistentLifecycle) will start/stop orient persistence support (lifecycle hook).
Due to new play guidelines, global object usage is deprecated and now it's recommended to use
[constructor as startup hook](https://www.playframework.com/documentation/2.5.x/GlobalSettings#Java).```java
@Inject
public PersistenceLifecycle(final PersistService persist,
final ApplicationLifecycle shutdown) {
preloadOrientEngine();
persist.start();shutdown.addStopHook(() -> {
persist.stop();
return null;
});
}
```**IMPORTANT** pay attention to [preloadOrientEngine()](app/PersistenceLifecycle.java#L30) method, which is a hack to pre-initialize orient
and avoid NoClassDefFound errors on start.### Sample
Actual sample is very simple: [SampleController](app/controllers/SampleController.java). It simply loads all records in `Sample` table and renders it as json.
[Repository](app/repositories/SampleRepository.java) used for accessing database:
```
@Query("select from Sample")
@DetachResult
List allSamples();
```Note that `@DetachResult` used to convert returned orient proxies to simple pojos (which play could serialize to json).
In normal application, such logic will be in service level or even in controller (for example, using repository.detach(..) method
(which is inherited from object mixin)). But make sure detaching occur inside transaction (@Transactional)).
.