From b1de821817b1247199bddbe8f285e76edb92ea2e Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Fri, 30 May 2014 01:30:57 +1200 Subject: [PATCH] Fix for #135 Make Model abstract, change db() and db(server) to be static methods. --- src/main/java/com/avaje/ebean/Model.java | 35 ++++++++++++++++--- .../server/deploy/BeanDescriptorManager.java | 5 +++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/avaje/ebean/Model.java b/src/main/java/com/avaje/ebean/Model.java index 853ce94b2..a6b43ce8d 100644 --- a/src/main/java/com/avaje/ebean/Model.java +++ b/src/main/java/com/avaje/ebean/Model.java @@ -29,7 +29,7 @@ import javax.persistence.MappedSuperclass; * relatively nice clean way to write queries. */ @MappedSuperclass -public class Model { +public abstract class Model { /** * Return the underlying 'default' EbeanServer. @@ -38,9 +38,36 @@ public class Model { * This provides full access to the API such as explicit transaction demarcation etc. * *

- * TODO: Transaction example + * Example: + *

+   * Transaction transaction = Customer.db().beginTransaction();
+   * try {
+   * 
+   *   // turn off cascade persist for this transaction
+   *   transaction.setPersistCascade(false);
+   * 
+   *   // extra control over jdbc batching for this transaction
+   *   transaction.setBatchGetGeneratedKeys(false);
+   *   transaction.setBatchMode(true);
+   *   transaction.setBatchSize(20);
+   * 
+   *   Customer customer = new Customer();
+   *   customer.setName("Roberto");
+   *   customer.save();
+   * 
+   *   Customer otherCustomer = new Customer();
+   *   otherCustomer.setName("Franko");
+   *   otherCustomer.save();
+   * 
+   *   transaction.commit();
+   * 
+   * } finally {
+   *   transaction.end();
+   * }
+   * 
+   * 
*/ - public EbeanServer db() { + public static EbeanServer db() { return Ebean.getServer(null); } @@ -54,7 +81,7 @@ public class Model { * @param server * The name of the EbeanServer. If this is null then the default EbeanServer is returned. */ - public EbeanServer db(String server) { + public static EbeanServer db(String server) { return Ebean.getServer(server); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorManager.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorManager.java index 7bdbea289..4c0b243a9 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorManager.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/BeanDescriptorManager.java @@ -18,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.avaje.ebean.BackgroundExecutor; +import com.avaje.ebean.Model; import com.avaje.ebean.RawSql; import com.avaje.ebean.RawSqlBuilder; import com.avaje.ebean.annotation.ConcurrencyMode; @@ -1411,6 +1412,10 @@ public class BeanDescriptorManager implements BeanDescriptorMap { // we got to the top of the inheritance return; } + if (Model.class.equals(superclass)) { + // top of the inheritance. Not enhancing Model at this stage + return; + } if (!EntityBean.class.isAssignableFrom(superclass)) { throw new IllegalStateException("Super type "+superclass+" is not enhanced?"); }