When using multiple databases one database is known as the default database. We need to do 4 things for entity beans that should only be used on the non-default database(s).
Model constructor
Use the io.ebean.Model
constructor that take the string name of
the database that should be used.
...
@DbName("other")
@MappedSuperclass
public abstract class OtherBaseDomain extends Model {
public OtherBaseDomain() {
super("other"); // "other" -> database name
}
...
}
...
@DbName("other")
@MappedSuperclass
open class OtherBaseDomain : Model("other") { // "other" -> database name
...
}
All entity beans that extend this model will use the named database when
they save()
and delete()
.
@DbName
Put @DbName
on a mapped superclass or individual entity beans.
This is read by the querybean generator when generating the query beans
so that queries for this entity bean type will execute using the named non-default
database.
...
@DbName("other") // "other" -> database name
@MappedSuperclass
public abstract class OtherBaseDomain extends Model {
public OtherBaseDomain() {
super("other"); // "other" -> database name
}
...
}
...
@DbName("other") // "other" -> database name
@MappedSuperclass
open class OtherBaseDomain : Model("other") { // "other" -> database name
...
}
For all entity beans extending OtherBaseDomain
their query beans
will now default to using the named database.
Finder constructor
If using io.ebean.Finder
we similarly use the constructor that takes the
string name of the database that should be used.
...
public class CustomerFinder extends Finder {
public CustomerFinder() {
super("other"); // "other" -> database name
}
...
}
...
open class CustomerFinder : Finder("other") { // "other" -> database name
...
}
Now using methods on CustomerFinder will default to using the named database.
Register entity types
We also need to register some entity types to the default database and some entity types to the other non default database(s).
Note that from Ebean version 12.2.1 the querybean generator will generate code to register the entity bean types to the appropriate database.
Create an implementation of ServerConfigProvider that registers the entity beans with the appropriate database based on the name.
public class RegisterDomainBeans implements ServerConfigProvider {
private static final String OTHER = "other";
private static final String DB = "db";
@Override
public void apply(ServerConfig serverConfig) {
log.debug("register beans for {}", serverConfig.getName());
if (DB.equals(serverConfig.getName())) {
// the default database
serverConfig.addClass(DCustomer.class);
serverConfig.addClass(DOrder.class);
serverConfig.addClass(DProduct.class);
...
} else if (OTHER.equals(serverConfig.getName())) {
// second OTHER database
serverConfig.setDefaultServer(false);
serverConfig.addClass(OSales.class);
serverConfig.addClass(OMarket.class);
...
}
}
}
Add a service loader src/main/resources/META-INF/services/io.ebean.config.ServerConfigProvider
file to specify our ServerConfigProvider implementation (e.g. RegisterDomainBeans).