To connect a Dropwizard application to a relational database with Hibernate, configure a DataSourceFactory, register a HibernateBundle during application bootstrap, and pass the bundle’s SessionFactory to a DAO. Put the JDBC URL and credentials in the application YAML. Use Dropwizard Migrations separately to track and apply schema changes.
How the Dropwizard–Hibernate connection fits together
DataSourceFactory holds the database connection settings. During bootstrap, HibernateBundle reads that factory and registers the entity classes Hibernate should map. The bundle manages the connection pool and provides a SessionFactory, which your DAO uses to access the database. Dropwizard also provides a database connectivity health check through the bundle. See the Dropwizard Hibernate manual.
This setup connects the application to a database; it does not by itself define how schema changes are reviewed or applied. Dropwizard Migrations, which wraps Liquibase, handles that separate job.
1. Add the Hibernate module
Add the Dropwizard Hibernate module to your build using a release compatible with the Dropwizard version already used by your application. The official manual documents the API but does not establish a dependency version for every project, so do not copy a version without checking your application’s existing release.
#1 Best Overall
2. Expose the database configuration
In your application configuration class, add a DataSourceFactory field and make it available as a configuration property, for example database. The official example marks the field with @Valid and @NotNull so configuration validation checks it.
The Dropwizard configuration reference documents database fields including the JDBC URL, driver class, username, and password. The JDBC URL is required. Add appropriate driver properties and pool settings for your driver and environment rather than treating the manual’s sample values as universal defaults.
3. Register HibernateBundle at bootstrap
Create a HibernateBundle with the entity classes your application will persist. Override its getDataSourceFactory method to return the DataSourceFactory from your configuration, then add the bundle in the application’s initialize method. This gives the bundle both the mapping classes and the connection settings it needs.
The official integration guide includes a PostgreSQL configuration example with org.postgresql.Driver and a jdbc:postgresql: URL. PostgreSQL is an example, not a requirement; select a database and JDBC driver that your application and operations support.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteRank #3
4. Give the SessionFactory to a DAO
In the application’s run method, retrieve the bundle’s SessionFactory, construct your DAO with it, and register the Jersey resource that uses the DAO. Dropwizard’s AbstractDAO is a minimal base class for this pattern. The Hibernate manual notes that an exception in its DAO transaction causes rollback. See the Hibernate manual’s DAO guidance.
For Jersey-managed resources, @UnitOfWork works out of the box to manage the Hibernate unit of work. For methods outside Jersey resources, the manual describes UnitOfWorkAwareProxyFactory for wrapping methods annotated with @UnitOfWork.
Rank #4
5. Configure the database in YAML
Put the connection settings under the configuration property corresponding to your DataSourceFactory field. The official example includes a driver class, username, password, JDBC URL, driver properties, connection wait timeout, validation query, minimum and maximum pool sizes, and idle-connection validation. These are available settings to adapt, not recommended figures or a benchmark. Check the Hibernate setup example and the configuration reference for the field names and details.
Keep environment-specific connection values in the deployment configuration rather than baking them into application code. Confirm that the configured driver is available to the application and that the JDBC URL uses the syntax required by that driver.
Free tools Windows power users keep installed
One-click scans. No signup required.
Handle Hibernate sessions and lazy-loaded data
A common failure occurs when a resource returns an entity whose lazy-loaded associations have not been fetched. The Hibernate session closes before Dropwizard processes the resource method’s return value, so accessing those associations afterward can fail. The official manual warns: “The Hibernate session is closed before your resource method’s return value (e.g., the Person from the database), which means your resource method (or DAO) is responsible for initializing all lazily-loaded collections, etc., before returning.”
Before returning a response, make sure the DAO or resource has loaded the data that response needs while the unit of work is active. Avoid relying on serialization to trigger additional database reads after the session has closed.
Manage schema changes with Dropwizard Migrations
Hibernate maps application objects to relational data; it is not a substitute for a deliberate schema-change workflow. Dropwizard Migrations wraps Liquibase and uses a changelog to record database changes. Register a MigrationsBundle with the application’s DataSourceFactory, keep the changelog in the application resources, and use the migration CLI commands appropriate to the app. The Migrations manual documents commands including status and migrate.
Applying a migration can make irreversible database changes. Treat migration execution as a deployment operation: review the changelog and plan when and how it is run. The documentation establishes the integration and commands, but does not prescribe a complete deployment process for every environment.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

