|
1 | 1 | == Hibernate |
2 | 2 |
|
3 | | -https://hibernate.org/orm/[Hibernate ORM] module. |
| 3 | +https://hibernate.org/orm/[Hibernate ORM] module. |
4 | 4 |
|
5 | 5 | === Usage |
6 | 6 |
|
@@ -119,8 +119,8 @@ options for more explicit control: |
119 | 119 |
|
120 | 120 | === Transactional Request |
121 | 121 |
|
122 | | -The javadoc:hibernate.TransactionalRequest[] decorator takes care of a lifecycle of an `EntityManager` per HTTP request. |
123 | | -The decorator creates, bind, begin/commit/rollback transaction and finally close it, so route handler |
| 122 | +The javadoc:hibernate.TransactionalRequest[] filter takes care of a lifecycle of an `EntityManager`/`StatelessSession` per HTTP request. |
| 123 | +The filter creates, bind, begin/commit/rollback transaction and finally close it, so route handler |
124 | 124 | doesn't have to deal with that boring lines of code. |
125 | 125 |
|
126 | 126 | .TransactionalRequest |
@@ -175,17 +175,69 @@ import io.jooby.hibernate.TransactionalRequest |
175 | 175 | } |
176 | 176 | ---- |
177 | 177 |
|
178 | | -The `EntityManager` is tied to the current HTTP request. Multiple `require`/`injection` calls produce |
179 | | -the same `EntityManager`. It is a simple way of managed simple read/write operations. |
| 178 | +.TransactionalRequest with StatelessSession |
| 179 | +[source, java, role = "primary"] |
| 180 | +---- |
| 181 | +import io.jooby.hikari.HikariModule; |
| 182 | +import io.jooby.hibernate.HibernateModule; |
| 183 | +import io.jooby.hibernate.TransactionalRequest; |
| 184 | +
|
| 185 | +{ |
| 186 | + install(new HikariModule()); |
| 187 | + |
| 188 | + install(new HibernateModule()); |
| 189 | + |
| 190 | + use(new TransactionalRequest().useStatelessSession()); |
| 191 | + |
| 192 | + post("/create", ctx -> { |
| 193 | + StatelessSession session = require(StatelessSession.class); |
| 194 | + |
| 195 | + MyEntity e = ...; |
| 196 | + |
| 197 | + session.insert(e); |
| 198 | +
|
| 199 | + return e; |
| 200 | + }); |
| 201 | +} |
| 202 | +---- |
| 203 | + |
| 204 | +.Kotlin |
| 205 | +[source, kt, role="secondary"] |
| 206 | +---- |
| 207 | +import io.jooby.hikari.HikariModule |
| 208 | +import io.jooby.hibernate.HibernateModule |
| 209 | +import io.jooby.hibernate.TransactionalRequest |
| 210 | +
|
| 211 | +{ |
| 212 | + install(HikariModule()) |
| 213 | + |
| 214 | + install(HibernateModule()) |
| 215 | + |
| 216 | + use(TransactionalRequest().useStatelessSession()) |
| 217 | + |
| 218 | + post("/create") { ctx -> |
| 219 | + val session = require(StatelessSession::class) |
| 220 | + |
| 221 | + val e = ... |
| 222 | + |
| 223 | + session.insert(e) |
| 224 | +
|
| 225 | + e |
| 226 | + } |
| 227 | +} |
| 228 | +---- |
| 229 | + |
| 230 | +The `EntityManager`/`StatelessSession` is tied to the current HTTP request. Multiple `require`/`injection` calls produce |
| 231 | +the same `EntityManager`/`StatelessSession`. It is a simple way of managed simple read/write operations. |
180 | 232 |
|
181 | 233 | [NOTE] |
182 | 234 | ==== |
183 | | -The javadoc:hiernate.TransactionalRequest[] doesn't extend session to the rendering phase (json, html, etc.). |
| 235 | +The javadoc:hibernate.TransactionalRequest[] doesn't extend session to the rendering phase (json, html, etc.). |
184 | 236 | The route handler needs to make sure all the information required by the rendering phase is available. |
185 | 237 | Otherwise, you are going to see `LazyInitializationException`. |
186 | 238 | ==== |
187 | 239 |
|
188 | | -There is a javadoc:hibernate.SessionRequest[] decorator that works identically but leaves transaction |
| 240 | +There is a javadoc:hibernate.SessionRequest[] filter that works identically but leaves transaction |
189 | 241 | management to you, so no transaction is started/committed or rollback during a HTTP request. |
190 | 242 |
|
191 | 243 | ==== @Transactional |
|
0 commit comments