java.lang.Object
io.jooby.hibernate.HibernateModule
- All Implemented Interfaces:
io.jooby.Extension
Hibernate ORM module: https://jooby.io/modules/hibernate.
Usage:
- Add hikari and hibernate dependency
- Install them
{
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:
use(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.
- Since:
- 2.0.0
- Author:
- edgar
-
Constructor Summary
ConstructorsConstructorDescriptionHibernateModule(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. -
Method Summary
Modifier and TypeMethodDescriptionvoidinstall(io.jooby.Jooby application) Scan packages and look for persistent classes.Scan packages and look for persistent classes.with(HibernateConfigurer configurer) Hook into Hibernate bootstrap components and allow to customize them.with(SessionProvider sessionProvider) Allow to customize aSessionbefore opening it.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.jooby.Extension
lateinit
-
Constructor Details
-
HibernateModule
Creates a Hibernate module.- Parameters:
name- The name/key of the data source to attach.classes- Persistent classes.
-
HibernateModule
Creates a new Hibernate module. Use the default/first datasource and register objects using thedbkey.- Parameters:
classes- Persistent classes.
-
HibernateModule
Creates a Hibernate module.- Parameters:
name- The name/key of the data source to attach.classes- Persistent classes.
-
-
Method Details
-
scan
Scan packages and look for persistent classes.- Parameters:
packages- Package names.- Returns:
- This module.
-
scan
Scan packages and look for persistent classes.- Parameters:
packages- Package names.- Returns:
- This module.
-
with
Allow to customize aSessionbefore opening it.- Parameters:
sessionProvider- Session customizer.- Returns:
- This module.
-
with
Hook into Hibernate bootstrap components and allow to customize them.- Parameters:
configurer- Configurer.- Returns:
- This module.
-
install
public void install(@NonNull io.jooby.Jooby application) - Specified by:
installin interfaceio.jooby.Extension
-