public class Hbm extends Object implements org.jooby.Jooby.Module
Hibernate ORM enables developers to more easily write applications whose data outlives the application process. As an Object/Relational Mapping (ORM) framework, Hibernate is concerned with data persistence as it applies to relational databases.
This module setup and configure Hibernate ORM and
JPA Provider.
This module depends on Jdbc module, make sure you read the doc of the Jdbc
module.
{
use(new Hbm("jdbc:mysql://localhost/mydb")
.classes(Beer.class)
);
get("/api/beer/", req -> {
return require(UnitOfWork.class).apply(em -> {
return em.createQuery("from Beer").getResultList();
});
});
}
We provide an UnitOfWork to simplify the amount of code required to interact within the
database.
For example the next line:
{
require(UnitOfWork.class).apply(em -> {
return em.createQuery("from Beer").getResultList();
});
}
Is the same as:
{
Session session = require(SessionFactory.class).openSession();
Transaction trx = session.getTransaction();
try {
trx.begin();
List<Beer> beers = em.createQuery("from Beer").getResultList();
trx.commit();
} catch (Exception ex) {
trx.rollback();
} finally {
session.close();
}
}
An UnitOfWork takes care of transactions and session life-cycle. It's worth to mention
too that a first requested UnitOfWork bind the Session to the current request. If later
in the execution flow an UnitOfWork, Session and/or EntityManager is
required then the one that belong to the current request (first requested) will be provided it.
We provide an advanced and recommended Open Session in View pattern, which basically keep the Session opened until the view
is rendered, but it uses two database transactions:
Here is an example on how to setup the open session in view filter:
{
use(new Hbm());
use("*", Hbm.openSessionInView());
}
JPA event listeners are provided by Guice, which means you can inject dependencies into your event listeners:
@Entity
@EntityListeners({BeerListener.class})
public class Beer {
}
public class BeerListener {
@Inject
public BeerListener(DependencyA depA) {
this.depA = depA;
}
@PostLoad
public void postLoad(Beer beer) {
this.depA.complementBeer(beer);
}
}
Hibernate event listeners are supported too via onEvent(EventType, Class):
{
use(new Hbm()
.onEvent(EventType.POST_LOAD, MyPostLoadListener.class));
}
Again, MyPostLoadListener will be provided by Guice.
Persistent classes must be provided at application startup time via
classes(Class...):
{
use(new Hbm()
.classes(Entity1.class, Entity2.class, ..., )
);
}
Or via scan():
{
use(new Hbm()
.scan()
);
}
Which scan the application package defined by hibernate.packagesToScan,
or you can provide where to look:
{
use(new Hbm()
.scan("foo.bar", "x.y.z")
);
}
Advanced configuration is provided via doWithXXX callbacks:
{
use(new Hbm()
.doWithBootstrap(bsrb -> {
// do with bsrb
})
.doWithRegistry(ssrb -> {
// do with ssrb
})
);
}
Or via hibernate.* properties from your .conf file:
hibernate.hbm2ddl.auto = update
You are free to inject a SessionFactory or EntityManagerFactory create a new
EntityManagerFactory.createEntityManager(), start transactions and do everything you
need.
For the time being, this doesn't work for a Session or EntityManager. A
Session EntityManager is bound to the current request, which means you can't
freely access from every single thread (like manually started thread, started by an executor
service, quartz, etc...).
Another restriction, is the access from Singleton services. If you need access from a
singleton services, you need to inject a Provider.
@Singleton
public class MySingleton {
@Inject
public MySingleton(Provider<EntityManager> em) {
this.em = em;
}
}
Still, we strongly recommend to leave your services in the default scope and avoid to use
Singleton objects, except of course for really expensive resources. This is also
recommend it by Guice.
Services in the default scope won't have this problem and are free to inject the
Session or EntityManager directly.
| Constructor and Description |
|---|
Hbm()
Creates a new
Hbm module. |
Hbm(String db)
Creates a new
Hbm module. |
| Modifier and Type | Method and Description |
|---|---|
Hbm |
classes(Class... classes)
Append persistent classes (classess annotated with Entity).
|
com.typesafe.config.Config |
config() |
void |
configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder) |
<T> Hbm |
doWithBootstrap(BiConsumer<org.hibernate.boot.registry.BootstrapServiceRegistryBuilder,com.typesafe.config.Config> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithBootstrap(Consumer<org.hibernate.boot.registry.BootstrapServiceRegistryBuilder> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithRegistry(BiConsumer<org.hibernate.boot.registry.StandardServiceRegistryBuilder,com.typesafe.config.Config> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithRegistry(Consumer<org.hibernate.boot.registry.StandardServiceRegistryBuilder> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSessionFactory(BiConsumer<org.hibernate.SessionFactory,com.typesafe.config.Config> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSessionFactory(Consumer<org.hibernate.SessionFactory> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSessionFactoryBuilder(BiConsumer<org.hibernate.boot.SessionFactoryBuilder,com.typesafe.config.Config> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSessionFactoryBuilder(Consumer<org.hibernate.boot.SessionFactoryBuilder> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSources(BiConsumer<org.hibernate.boot.MetadataSources,com.typesafe.config.Config> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
doWithSources(Consumer<org.hibernate.boot.MetadataSources> configurer)
Configurer callback to apply advanced configuration while bootstrapping hibernate:
|
<T> Hbm |
onEvent(org.hibernate.event.spi.EventType<T> type,
Class<? extends T> listenerType)
Register an hibernate event listener.
|
static org.jooby.Route.Filter |
openSessionInView()
Creates an open session in view filter as described here.
|
Hbm |
scan()
Scan the packages defined by
hibernate.packagesToScan property or the application
package (that's the package where you application was defined) and discover persistent classes
(annotated with Entity). |
Hbm |
scan(String... packages)
Scan the provided packages and discover persistent classes (annotated with Entity).
|
public Hbm(String db)
Hbm module.db - A jdbc connection string or a property with a jdbc connection string.public Hbm()
public Hbm classes(Class... classes)
classes - Persistent classes.public Hbm scan(String... packages)
packages - Package to scan.public Hbm scan()
hibernate.packagesToScan property or the application
package (that's the package where you application was defined) and discover persistent classes
(annotated with Entity).public static org.jooby.Route.Filter openSessionInView()
{
use(new Hbm());
use("*", Hbm.openSessionInView());
}
public <T> Hbm onEvent(org.hibernate.event.spi.EventType<T> type, Class<? extends T> listenerType)
type - Event type.listenerType - Listener type.public <T> Hbm doWithBootstrap(BiConsumer<org.hibernate.boot.registry.BootstrapServiceRegistryBuilder,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public <T> Hbm doWithBootstrap(Consumer<org.hibernate.boot.registry.BootstrapServiceRegistryBuilder> configurer)
configurer - Configurer callback.public <T> Hbm doWithRegistry(Consumer<org.hibernate.boot.registry.StandardServiceRegistryBuilder> configurer)
configurer - Configurer callback.public <T> Hbm doWithRegistry(BiConsumer<org.hibernate.boot.registry.StandardServiceRegistryBuilder,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public <T> Hbm doWithSources(Consumer<org.hibernate.boot.MetadataSources> configurer)
configurer - Configurer callback.public <T> Hbm doWithSources(BiConsumer<org.hibernate.boot.MetadataSources,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public <T> Hbm doWithSessionFactoryBuilder(Consumer<org.hibernate.boot.SessionFactoryBuilder> configurer)
configurer - Configurer callback.public <T> Hbm doWithSessionFactory(BiConsumer<org.hibernate.SessionFactory,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public <T> Hbm doWithSessionFactory(Consumer<org.hibernate.SessionFactory> configurer)
configurer - Configurer callback.public <T> Hbm doWithSessionFactoryBuilder(BiConsumer<org.hibernate.boot.SessionFactoryBuilder,com.typesafe.config.Config> configurer)
configurer - Configurer callback.public void configure(org.jooby.Env env,
com.typesafe.config.Config conf,
com.google.inject.Binder binder)
configure in interface org.jooby.Jooby.Modulepublic com.typesafe.config.Config config()
config in interface org.jooby.Jooby.ModuleCopyright © 2019. All rights reserved.