public class HibernateModule extends Object implements Extension
{
install(new HikariModule());
install(new HibernateModule());
}
- Use it
{
get("/", ctx -> {
EntityManagerFactory emf = require(EntityManagerFactory.class);
// do with emf
});
}
Optionally, you can require/inject a SessionFactory too:
{
get("/", ctx -> {
SessionFactory sf = require(SessionFactory.class);
// do with sf
});
}
By default the hibernate module scan the Jooby.getBasePackage() to register all the
persistent classes. To scan a different package use the scan(String...)
method.
To turn it off you need to specify all the persistent classes at creation time, using the
HibernateModule(Class[]) constructor.
It is important to close either an EntityManager or Session created manually
from EntityManagerFactory and SessionFactory.
So code around session/entityManager looks like:
get("/", ctx -> {
EntityManager em = require(EntityManager.class);
Transaction trx = em.getTransaction();
try {
trx.begin();
// work with EntityManager compute a result
trx.commit();
return result;
} catch(Exception x) {
trx.rollback();
throw x;
} finally {
em.close();
}
});
To avoid all these lines of code we do provide a TransactionalRequest decorator so code
looks more simple:
decorator(new TransactionalRequest());
get("/", ctx -> {
EntityManager em = require(EntityManager.class);
// work with EntityManager compute a result
return result;
});
Transaction and lifecycle of session/entityManager is managed by TransactionalRequest.
Complete documentation is available at: https://jooby.io/modules/hibernate.| Constructor and Description |
|---|
HibernateModule(Class... classes)
Creates a new Hibernate module.
|
HibernateModule(String name,
Class... classes)
Creates a Hibernate module.
|
HibernateModule(String name,
List<Class> classes)
Creates a Hibernate module.
|
| Modifier and Type | Method and Description |
|---|---|
void |
install(Jooby application) |
HibernateModule |
scan(List<String> packages)
Scan packages and look for persistent classes.
|
HibernateModule |
scan(String... packages)
Scan packages and look for persistent classes.
|
HibernateModule |
with(HibernateConfigurer configurer)
Hook into Hibernate bootstrap components and allow to customize them.
|
HibernateModule |
with(SessionProvider sessionProvider)
Allow to customize a
Session before opening it. |
public HibernateModule(@Nonnull String name, Class... classes)
name - The name/key of the data source to attach.classes - Persistent classes.public HibernateModule(Class... classes)
db key.classes - Persistent classes.@Nonnull public HibernateModule scan(@Nonnull String... packages)
packages - Package names.@Nonnull public HibernateModule scan(@Nonnull List<String> packages)
packages - Package names.@Nonnull public HibernateModule with(@Nonnull SessionProvider sessionProvider)
Session before opening it.sessionProvider - Session customizer.@Nonnull public HibernateModule with(@Nonnull HibernateConfigurer configurer)
configurer - Configurer.Copyright © 2020. All rights reserved.