#1691 - Refactor io.ebean.Model to use a named database - support for use with non-default named database

This commit is contained in:
rob bygrave
2019-04-30 23:42:59 +12:00
parent 7e6d059ef1
commit 9fdb375229
3 changed files with 36 additions and 49 deletions
+21 -32
View File
@@ -75,6 +75,25 @@ import io.ebean.bean.EntityBean;
*/
public abstract class Model {
/**
* The name of the database this entity will use, null for the default database.
*/
private final String _$dbName;
/**
* Create using the default database.
*/
public Model() {
this._$dbName = null;
}
/**
* Create with a named database (typically not the default database).
*/
public Model(String dbName) {
this._$dbName = dbName;
}
/**
* Return the underlying 'default' Database.
* <p>
@@ -107,17 +126,8 @@ public abstract class Model {
*
* }</pre>
*/
public static Database db() {
return DB.getDefault();
}
/**
* Return a named Database that is typically different to the default database.
*
* @param server The name of the Database. If this is null then the default Database is returned.
*/
public static Database db(String server) {
return DB.byName(server);
public Database db() {
return DB.byName(_$dbName);
}
/**
@@ -245,27 +255,6 @@ public abstract class Model {
return db().deletePermanent(this);
}
/**
* Perform an update using this entity against the specified server.
*/
public void update(String server) {
db(server).update(this);
}
/**
* Perform an insert using this entity against the specified server.
*/
public void insert(String server) {
db(server).insert(this);
}
/**
* Perform a delete using this entity against the specified server.
*/
public boolean delete(String server) {
return db(server).delete(this);
}
/**
* Refreshes this entity from the database.
*
@@ -1,5 +1,6 @@
package io.ebeaninternal.server.deploy.parse;
import io.ebean.Model;
import io.ebean.annotation.DbArray;
import io.ebean.annotation.DbHstore;
import io.ebean.annotation.DbJson;
@@ -64,17 +65,18 @@ public class DeployCreateProperties {
* </p>
*/
private boolean ignoreFieldByName(String fieldName) {
if (fieldName.startsWith("_ebean_") || fieldName.equals("_$targetDatabase")) {
if (fieldName.startsWith("_ebean_")) {
// ignore Ebean internal fields
return true;
}
if (fieldName.startsWith("ajc$instance$")) {
// ignore AspectJ internal fields
return true;
}
// ignore AspectJ internal fields
return fieldName.startsWith("ajc$instance$");
}
// we are interested in this field
return false;
private boolean ignoreField(Field field) {
return Modifier.isStatic(field.getModifiers())
|| Modifier.isTransient(field.getModifiers())
|| ignoreFieldByName(field.getName());
}
/**
@@ -83,6 +85,10 @@ public class DeployCreateProperties {
*/
private void createProperties(DeployBeanDescriptor<?> desc, Class<?> beanType, int level) {
if (beanType.equals(Model.class)) {
// ignore all fields on model (_$dbName)
return;
}
boolean scalaObject = desc.isScalaObject();
try {
@@ -92,16 +98,7 @@ public class DeployCreateProperties {
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
if (Modifier.isStatic(field.getModifiers())) {
// not interested in static fields
logger.trace("Skipping static field {} in {}", field.getName(), beanType.getName());
} else if (Modifier.isTransient(field.getModifiers())) {
// not interested in transient fields
logger.trace("Skipping transient field {} in {}", field.getName(), beanType.getName());
} else if (!ignoreFieldByName(field.getName())) {
if (!ignoreField(field)) {
String fieldName = getFieldName(field, beanType);
String initFieldName = initCap(fieldName);