|
| 1 | +== Hibernate |
| 2 | + |
| 3 | +https://hibernate.org/orm/[Hibernate ORM] module. |
| 4 | + |
| 5 | +=== Usage |
| 6 | + |
| 7 | +1) Add the dependencies (hikari + hibernate): |
| 8 | + |
| 9 | +[dependency, artifactId="jooby-hikari:DataSource via HikariCP, jooby-hibernate:Hibernate Module"] |
| 10 | +. |
| 11 | + |
| 12 | +2) Add database driver (mySQL here): |
| 13 | + |
| 14 | +[dependency, artifactId="mysql-connector-java"] |
| 15 | +. |
| 16 | + |
| 17 | +3) Set database properties |
| 18 | + |
| 19 | +.application.conf |
| 20 | +[source, properties] |
| 21 | +---- |
| 22 | +db.url = "jdbc:mysql://localhost/mydb" |
| 23 | +db.user = myuser |
| 24 | +db.password = mypass |
| 25 | +---- |
| 26 | + |
| 27 | + |
| 28 | +4) Install and use Hibernate |
| 29 | + |
| 30 | +.Java |
| 31 | +[source, java, role="primary"] |
| 32 | +---- |
| 33 | +import io.jooby.hikari.Hikari; |
| 34 | +
|
| 35 | +{ |
| 36 | + install(new Hikari()); <1> |
| 37 | +
|
| 38 | + install(new Hibernate()); <2> |
| 39 | + |
| 40 | + get("/", ctx -> { |
| 41 | + EntityManager em = require(EntityManager.class); <3> |
| 42 | + Transaction trx = em.getTransaction(); <4> |
| 43 | + try { |
| 44 | + trx.begin(); <5> |
| 45 | + |
| 46 | + // work with EntityManager compute a result <6> |
| 47 | + |
| 48 | + trx.commit(); <7> |
| 49 | + |
| 50 | + return result; |
| 51 | + } catch(Exception x) { |
| 52 | + trx.rollback(); <8> |
| 53 | + throw x; |
| 54 | + } finally { |
| 55 | + em.close(); <9> |
| 56 | + } |
| 57 | + }); |
| 58 | +} |
| 59 | +---- |
| 60 | + |
| 61 | +.Kotlin |
| 62 | +[source, kt, role="secondary"] |
| 63 | +---- |
| 64 | +import io.jooby.hikari.Hikari |
| 65 | +
|
| 66 | +{ |
| 67 | + install(Hikari()) <1> |
| 68 | + |
| 69 | + install(new Hibernate()) <2> |
| 70 | + |
| 71 | + get("/") { |
| 72 | + val em = require(EntityManager::class) <3> |
| 73 | + val trx = em.getTransaction() <4> |
| 74 | + try { |
| 75 | + trx.begin() <5> |
| 76 | + |
| 77 | + // work with EntityManager compute a result <6> |
| 78 | + |
| 79 | + trx.commit() <7> |
| 80 | + |
| 81 | + result |
| 82 | + } catch(Exception x) { |
| 83 | + trx.rollback() <8> |
| 84 | + throw x |
| 85 | + } finally { |
| 86 | + em.close() <9> |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +---- |
| 91 | + |
| 92 | +<1> Install and creates a `DataSource` |
| 93 | +<2> Install and initializes Hibernate. Entities are automatically detected |
| 94 | +<3> Get a new `EntityManager` |
| 95 | +<4> Creates a new transaction |
| 96 | +<5> Being the transaction |
| 97 | +<6> Work with EntityManager (read, write to dababase) |
| 98 | +<7> Commit the transaction |
| 99 | +<8> Rollback transaction in case of error |
| 100 | +<9> Close the `EntityManager` |
| 101 | + |
| 102 | +=== Entity Discovering |
| 103 | + |
| 104 | +By default the javadoc:hibernate.Hbm[] module detects all the persistent entities under javadoc:Jooby[getBasePackage, text = "base/root package"]. The module provides two |
| 105 | +options for more explicit control: |
| 106 | + |
| 107 | +- List persistent classes at creation time: |
| 108 | + |
| 109 | +---- |
| 110 | + install(new Hbm(MyEntity1.class, MyEntity2.class)); |
| 111 | +---- |
| 112 | + |
| 113 | +- Explicit package scanning |
| 114 | + |
| 115 | +---- |
| 116 | + install(new Hbm().scan("mypackage")); |
| 117 | +---- |
| 118 | + |
| 119 | +=== Transactional Request |
| 120 | + |
| 121 | +The javadoc:TransactionalRequest[] decorator takes care of a lifecycle of an `EntityManager` per HTTP request. |
| 122 | +The decorator creates, bind, begin/commit/rollback transaction and finally close it, so route handler |
| 123 | +doesn't have to deal with that boring lines of code. |
| 124 | + |
| 125 | +.TransactionalRequest |
| 126 | +[source, java, role = "primary"] |
| 127 | +---- |
| 128 | +import io.jooby.hikari.Hikari; |
| 129 | +import io.jooby.hibernate.Hbm; |
| 130 | +import io.jooby.hibernate.TransactionalRequest; |
| 131 | +
|
| 132 | +{ |
| 133 | + install(new Hikari()); |
| 134 | + |
| 135 | + install(new Hbm()); |
| 136 | + |
| 137 | + decorator(new TransactionalRequest()); |
| 138 | + |
| 139 | + post("/create", ctx -> { |
| 140 | + EntityManager em = require(EntityManager.class); |
| 141 | + |
| 142 | + MyEntity e = ...; |
| 143 | + |
| 144 | + em.persist(e); |
| 145 | +
|
| 146 | + return e; |
| 147 | + }); |
| 148 | +} |
| 149 | +---- |
| 150 | + |
| 151 | +.Kotlin |
| 152 | +[source, kt, role="secondary"] |
| 153 | +---- |
| 154 | +import io.jooby.hikari.Hikari |
| 155 | +import io.jooby.hibernate.Hbm |
| 156 | +import io.jooby.hibernate.TransactionalRequest |
| 157 | +
|
| 158 | +{ |
| 159 | + install(Hikari()) |
| 160 | + |
| 161 | + install(Hbm()) |
| 162 | + |
| 163 | + decorator(TransactionalRequest()) |
| 164 | + |
| 165 | + post("/create") { ctx -> |
| 166 | + val em = require(EntityManager::class) |
| 167 | + |
| 168 | + val e = ... |
| 169 | + |
| 170 | + em.persist(e) |
| 171 | +
|
| 172 | + e |
| 173 | + } |
| 174 | +} |
| 175 | +---- |
| 176 | + |
| 177 | +The `EntityManager` is tied to the current HTTP request. Multiple `require`/`injection` calls produce |
| 178 | +the same `EntityManager`. It is a simple way of managed simple read/write operations. |
| 179 | + |
| 180 | +[NOTE] |
| 181 | +==== |
| 182 | +The javadoc:TransactionalRequest[] doesn't extend session to the rendering phase (json, html, etc.). |
| 183 | +The route handler needs to make sure all the information required by the rendering phase is available. |
| 184 | +Otherwise, you are going to see `LazyInitializationException`. |
| 185 | +==== |
| 186 | + |
| 187 | +=== Schema Creation |
| 188 | + |
| 189 | +Schema creation is controlled by the `hibernate.hbm2ddl.auto` property. The Hbm module configure this property using the following rules: |
| 190 | + |
| 191 | +- When the javadoc:flyway.Flywayby[] module is present the value of `hibernate.hbm2ddl.auto` is set to `none` |
| 192 | +- When running on `dev` or `test` mode the value of `hibernate.hbm2ddl.auto` is set to `update` |
| 193 | +- Otherwise is set to `none` |
| 194 | + |
| 195 | +=== Advanced Options |
| 196 | + |
| 197 | +Advanced Hibernate configuration is supported from application configuration properties. |
| 198 | + |
| 199 | +.application.conf |
| 200 | +[source, properties] |
| 201 | +---- |
| 202 | +hibernate.hbm2ddl.auto = create |
| 203 | +---- |
0 commit comments