This guide shows how to migrate legacy programmatic database creation code from:
new DatabaseConfig()DatabaseFactory.create(...)- old
setXxx(...)builder-style configuration methods
…to the preferred builder-based style using:
Database.builder()- fluent
DatabaseBuildermethods such asname(...),register(...), anddefaultDatabase(...) DatabaseBuilder.build()
Use this guide when upgrading older Ebean setup code or when building an automated/semi-automated migration.
Prefer code shaped like this:
Database database = Database.builder()
.name("db")
.loadFromProperties()
.dataSourceBuilder(dataSource)
.register(true)
.defaultDatabase(true)
.build();The important points are:
- Start with
Database.builder() - Configure via
DatabaseBuilder - Finish with
.build()
DatabaseConfig config = new DatabaseConfig();
config.setName("db");
config.loadFromProperties();DatabaseBuilder config = Database.builder()
.name("db")
.loadFromProperties();- Prefer the
DatabaseBuildertype for local variables and parameters when possible. - If existing code only uses standard builder methods, this change is usually mechanical.
- If existing code later reads configuration back, use
config.settings().
DatabaseConfig config = new DatabaseConfig();
config.setName("db");
config.loadFromProperties();
Database database = DatabaseFactory.create(config);DatabaseBuilder config = Database.builder()
.name("db")
.loadFromProperties();
Database database = config.build();Database database = Database.builder()
.name("db")
.loadFromProperties()
.build();Database database = DatabaseFactory.create("other");Database database = Database.builder()
.name("other")
.loadFromProperties()
.build();For named databases, set .name("...") before .loadFromProperties() so the named configuration is loaded.
DatabaseBuilder already exposes preferred fluent names for most configuration methods.
Use those names when migrating older setup code.
| Legacy call | Preferred call |
|---|---|
setName("db") |
name("db") |
setRegister(false) |
register(false) |
setDefaultServer(false) |
defaultDatabase(false) |
setContainerConfig(cfg) |
containerConfig(cfg) |
setDbSchema("app") |
dbSchema("app") |
setDataSourceConfig(ds) |
dataSourceBuilder(ds) |
setReadOnlyDataSourceConfig(ro) |
readOnlyDataSourceBuilder(ro) |
setRunMigration(true) |
runMigration(true) |
setDisableClasspathSearch(true) |
disableClasspathSearch(true) |
setPersistBatch(batch) |
persistBatch(batch) |
DatabaseConfig config = new DatabaseConfig();
config.setName("db");
config.setRegister(false);
config.setDefaultServer(false);
config.setDataSourceConfig(dataSource);
Database database = DatabaseFactory.create(config);Database database = Database.builder()
.name("db")
.register(false)
.defaultDatabase(false)
.dataSourceBuilder(dataSource)
.build();The migration should preserve behavior, but verify these points:
register(true)is still the defaultdefaultDatabase(true)is still the default- call
loadFromProperties()if the old code loaded configuration from properties - for named databases, set the name before loading properties
- explicit entity registration via
addClass(...)/addAll(...)is unchanged - custom datasource wiring via
dataSourceBuilder(...)andreadOnlyDataSourceBuilder(...)is unchanged
These cases are not simple search-and-replace migrations and should be reviewed manually:
There is no direct builder shorthand for this today. Keep this as-is for now and migrate the surrounding builder configuration first.
This is a container lifecycle concern, not a normal database-builder call. Keep it as-is unless you are intentionally moving the ContainerConfig onto the first builder via containerConfig(...).
This is also a lifecycle concern rather than normal builder setup. Leave it alone unless you are making a deliberate lifecycle change.
If the code only uses standard builder operations, switch the type to DatabaseBuilder.
If the code depends on implementation-specific DatabaseConfig methods, review it manually.
Use:
DatabaseBuilder builder = Database.builder();
DatabaseBuilder.Settings settings = builder.settings();rather than relying on the concrete DatabaseConfig type only to read getters.
This migration is a good candidate for semi-automated upgrading.
These are usually safe to rewrite automatically:
new DatabaseConfig()→Database.builder()DatabaseFactory.create(builder)→builder.build()DatabaseFactory.create("name")→Database.builder().name("name").loadFromProperties().build()- legacy
setXxx(...)calls → preferred fluent builder methods
Automatically flag, but do not blindly rewrite:
DatabaseFactory.createWithContextClassLoader(...)DatabaseFactory.initialiseContainer(...)DatabaseFactory.shutdown()- parameters, fields, or return types declared as
DatabaseConfig - any use that clearly depends on
DatabaseConfigimplementation details rather thanDatabaseBuilder
- Database configuration — preferred modern setup style using
Database.builder() - Guide index — full list of Ebean setup and migration guides