|
1 | 1 | # jooby-hbm |
2 | 2 |
|
| 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 | + |
3 | 14 | ## dependency |
4 | 15 |
|
5 | 16 | ```xml |
|
9 | 20 | <version>0.4.2.1</version> |
10 | 21 | </dependency> |
11 | 22 | ``` |
| 23 | + |
12 | 24 | ## usage |
13 | 25 |
|
| 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<EntityManager> 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 | + |
14 | 104 |
|
15 | 105 | # appendix: hbm.conf |
16 | 106 | ```properties |
17 | 107 | hibernate { |
18 | 108 | id.new_generator_mappings = true |
19 | 109 | archive.autodetection = class |
20 | | - hbm2ddl.auto = update |
| 110 | + hbm2ddl.auto = validate |
| 111 | + current_session_context_class = managed |
21 | 112 | } |
22 | 113 |
|
23 | 114 | javax.persistence { |
|
0 commit comments