Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 629d4a4

Browse files
committed
doc for hbm module
1 parent 1f2afb7 commit 629d4a4

16 files changed

Lines changed: 606 additions & 123 deletions

File tree

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ cache:
44
- $HOME/.m2
55
jdk:
66
- oraclejdk8
7+
install:
8+
- mvn install -Dlogback.configurationFile=logback-travis.xml -DskipTests=true -Dmaven.javadoc.skip=true -B -V
9+
script:
10+
- mvn test -Dlogback.configurationFile=logback-travis.xml -B
711
after_success:
812
- mvn -Dlogback.configurationFile=logback-travis.xml clean package coveralls:report -P coverage

coverage-report/src/test/java/org/jooby/hbm/HbmCustomFeature.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
public class HbmCustomFeature extends ServerFeature {
1919

2020
{
21-
use(ConfigFactory.empty().withValue("db.audit", ConfigValueFactory.fromAnyRef("mem")));
21+
use(ConfigFactory.empty()
22+
.withValue("db.audit", ConfigValueFactory.fromAnyRef("mem"))
23+
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));
2224

2325
use(new Hbm("db.audit", Member.class));
2426

coverage-report/src/test/java/org/jooby/hbm/HbmFeature.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
public class HbmFeature extends ServerFeature {
1717

1818
{
19-
use(ConfigFactory.empty().withValue("db", ConfigValueFactory.fromAnyRef("mem")));
19+
use(ConfigFactory.empty()
20+
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))
21+
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));
2022

2123
use(new Hbm(Member.class));
2224

coverage-report/src/test/java/org/jooby/hbm/HbmParamConverterFeature.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import javax.persistence.EntityManager;
66

7-
import org.jooby.hbm.Hbm;
87
import org.jooby.hbm.data.Member;
98
import org.jooby.test.ServerFeature;
109
import org.junit.Test;
@@ -15,7 +14,9 @@
1514
public class HbmParamConverterFeature extends ServerFeature {
1615

1716
{
18-
use(ConfigFactory.empty().withValue("db", ConfigValueFactory.fromAnyRef("mem")));
17+
use(ConfigFactory.empty()
18+
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))
19+
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));
1920

2021
use(new Hbm(Member.class));
2122

coverage-report/src/test/java/org/jooby/hbm/HbmProdEnvFeature.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public class HbmProdEnvFeature extends ServerFeature {
1818
{
1919
use(ConfigFactory.empty()
2020
.withValue("application.env", ConfigValueFactory.fromAnyRef("prod"))
21-
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));
21+
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))
22+
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));
2223

2324
use(new Hbm(Member.class));
2425

coverage-report/src/test/java/org/jooby/hbm/HbmScanFeature.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
public class HbmScanFeature extends ServerFeature {
1717

1818
{
19-
use(ConfigFactory.empty().withValue("db", ConfigValueFactory.fromAnyRef("mem")));
19+
use(ConfigFactory.empty()
20+
.withValue("db", ConfigValueFactory.fromAnyRef("mem"))
21+
.withValue("hibernate.hbm2ddl.auto", ConfigValueFactory.fromAnyRef("update")));
2022

2123
use(new Hbm().scan());
2224

gh-pages.ant

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<fileset dir="target/site/apidocs" />
3333
</copy>
3434

35-
<!--
35+
<!-- -->
3636
<exec executable="git" dir="${workdir}">
3737
<arg value="add" />
3838
<arg value="-A" />
@@ -48,7 +48,7 @@
4848
<arg value="push" />
4949
<arg value="origin" />
5050
<arg value="site" />
51-
</exec>-->
51+
</exec>
5252

5353
</target>
5454

jooby-hbm/README.md

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# jooby-hbm
22

3+
Object-Relational-Mapping via [Hibernate](http://hibernate.org/). Exposes an ```EntityManagerFactory``` and ```EntityManager``` services.
4+
5+
This module extends [jdbc](/doc/jooby-dbc) module, before going forward, make sure you read the doc of the [jdbc](/doc/jooby-dbc) module first.
6+
7+
This module provides an advanced and recommended [Open Session in View](https://developer.jboss.org/wiki/OpenSessionInView#jive_content_id_Can_I_use_two_transactions_in_one_Session)
8+
pattern, which basically keeps the ```Session``` opened until the view is rendered. But it uses two database transactions:
9+
10+
1) first transaction is committed before rendering the view and then
11+
12+
2) a read only transaction is opened for rendering the view.
13+
314
## dependency
415

516
```xml
@@ -9,15 +20,95 @@
920
<version>0.4.2.1</version>
1021
</dependency>
1122
```
23+
1224
## usage
1325

26+
```java
27+
{
28+
use(new Hbm(EntityA.class, EntityB.class));
29+
30+
get("/", req -> {
31+
EntityManager em = req.require(EntityManager.class);
32+
// work with em...
33+
});
34+
}
35+
```
36+
37+
At bootstrap time you will see something similar to this:
38+
39+
```bash
40+
* /** [*/*] [*/*] (hbm)
41+
```
42+
43+
That is the filter with the <strong>Open Session in View</strong> pattern.
44+
45+
## life-cycle
46+
47+
You are free to inject an ```EntityManagerFactory``` create a new
48+
```EntityManagerFactory#createEntityManager()```, start transactions and do everything you
49+
need.
50+
51+
For the time being, this doesn't work for an ```EntityManager```. An ```EntityManager``` is
52+
bound to the current request, which means you can't freely access from every single thread (like
53+
manually started thread, started by an executor service, quartz, etc...).
54+
55+
Another restriction, is the access from ```Singleton``` services. If you need access from a
56+
singleton services, you need to inject a ```Provider```.
57+
58+
```java
59+
@Singleton
60+
public class MySingleton {
61+
62+
@Inject
63+
public MySingleton(Provider&lt;EntityManager&gt; em) {
64+
this.em = em;
65+
}
66+
}
67+
```
68+
69+
This is because the ```EntityManager``` is bound as [RequestScoped]({{defdocs/RequestScoped.html}}).
70+
71+
72+
Still, we strongly recommend to leave your services in the default scope and avoid to use
73+
```Singleton``` objects, except of course for really expensive resources. This is also
74+
recommend it by [Guice](https://github.com/google/guice).
75+
76+
Services in the default scope won't have this problem and are free to inject the ```EntityManager``` directly.
77+
78+
## persistent classes
79+
80+
Classpath scanning is OFF by default, so you need to explicitly tell [Hibernate](http://hibernate.org/) which classes are
81+
persistent. This intentional and helps to reduce bootstrap time and have explicit control over
82+
persistent classes.
83+
84+
If you don't care about bootstrap time and/or just like the auto-discover feature, just do:
85+
86+
```java
87+
{
88+
use(new Hbm().scan());
89+
}
90+
```
91+
92+
After calling ```scan()```, [Hibernate](http://hibernate.org/) will auto-discover all the entities application's
93+
namespace. The namespace is defined by the package of your application. Given:
94+
```org.myproject.App``` it will scan everything under ```org.myproject```.
95+
96+
## options
97+
98+
[Hibernate](http://hibernate.org/) options can be set from your ```application.conf``` file, just make sure to prefix them with ```hibernate.*```
99+
100+
```properties
101+
hibernate.hbm2ddl.auto = update
102+
```
103+
14104

15105
# appendix: hbm.conf
16106
```properties
17107
hibernate {
18108
id.new_generator_mappings = true
19109
archive.autodetection = class
20-
hbm2ddl.auto = update
110+
hbm2ddl.auto = validate
111+
current_session_context_class = managed
21112
}
22113

23114
javax.persistence {

0 commit comments

Comments
 (0)