diff --git a/src/main/java/com/avaje/ebean/Model.java b/src/main/java/com/avaje/ebean/Model.java index ec66a0c7e..c10183402 100644 --- a/src/main/java/com/avaje/ebean/Model.java +++ b/src/main/java/com/avaje/ebean/Model.java @@ -1,73 +1,26 @@ package com.avaje.ebean; -import java.util.*; -import java.beans.*; -import java.lang.reflect.*; +import java.util.List; +import java.util.Map; +import java.util.Set; -import com.avaje.ebean.Ebean; -import com.avaje.ebean.OrderBy; -import com.avaje.ebean.ExpressionList; -import com.avaje.ebean.Query; -import com.avaje.ebean.RawSql; -import com.avaje.ebean.ExpressionFactory; -import com.avaje.ebean.PagingList; -import com.avaje.ebean.FutureRowCount; -import com.avaje.ebean.FutureList; -import com.avaje.ebean.FutureIds; -import com.avaje.ebean.FetchConfig; -//import com.avaje.ebean.QueryListener; -//import com.avaje.ebean.QueryIterator; -import com.avaje.ebean.EbeanServer; -import com.avaje.ebean.Filter; -//import com.avaje.ebean.QueryResultVisitor; - -//import play.Play; -//import play.libs.F.*; -//import static play.libs.F.*; - -//import org.springframework.beans.*; +import javax.persistence.MappedSuperclass; /** - * Base-class for Ebean-mapped models that provides convenience methods. + * A MappedSuperclass base class that provides convenience methods for inserting, updating and deleting beans. + * + *

By having your entity beans extend this it provides a 'Active Record' style programming model for Ebean users. + * + *

Note that there is a avaje-ebeanorm-mocker project that enables you to use Mockito or similar tools to still + * mock out the underlying 'default EbeanServer' for testing purposes. */ -@javax.persistence.MappedSuperclass +@MappedSuperclass public class Model { - - // -- Magic to dynamically access the @Id property - - // @javax.persistence.Transient - // private Tuple _idGetSet; - // - // private Tuple _idAccessors() { - // if(_idGetSet == null) { - // try { - // Class clazz = this.getClass(); - // while(clazz != null) { - // for(Field f:clazz.getDeclaredFields()) { - // if(f.isAnnotationPresent(javax.persistence.Id.class) || - // f.isAnnotationPresent(javax.persistence.EmbeddedId.class)) { - // PropertyDescriptor idProperty = new BeanWrapperImpl(this).getPropertyDescriptor(f.getName()); - // _idGetSet = Tuple(idProperty.getReadMethod() , idProperty.getWriteMethod()); - // } - // } - // clazz = clazz.getSuperclass(); - // } - // if(_idGetSet == null) { - // throw new RuntimeException("No @javax.persistence.Id field found in class [" + this.getClass() - // + "]"); - // } - // } catch(RuntimeException e) { - // throw e; - // } catch(Exception e) { - // throw new RuntimeException(e); - // } - // } - // return _idGetSet; - // } + private Object _getId() { try { - return null;// _idAccessors()._1.invoke(this); + return Ebean.getServer(null).getBeanId(this); } catch (RuntimeException e) { throw e; } catch (Exception e) { @@ -75,27 +28,10 @@ public class Model { } } - private void _setId(Object id) { - try { - // _idAccessors()._2.invoke(this,id); - } catch (RuntimeException e) { - throw e; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - // -- - /** - * Save inserts or updates this entity depending on its state. - */ - public void save() { - Ebean.save(this); - } - - /** - * Return the default EbeanServer. + * Return the underlying 'default' EbeanServer. + * + *

This provides full access to the API such as explicit transaction demarcation etc. */ public EbeanServer db() { return Ebean.getServer(null); @@ -103,53 +39,19 @@ public class Model { /** * Return typically a different EbeanServer to the default. + * * @param server The name of the EbeanServer. If this is null then the default EbeanServer is returned. */ public EbeanServer db(String server) { return Ebean.getServer(server); } - // RB: Actually they should probably not use save() but insert() in this case -// /** -// * Saves (inserts) this entity. -// * -// * @param server -// * the Ebean server to use -// */ -// public void save(String server) { -// Ebean.getServer(server).save(this); -// } - - // RB: Uncommonly used so get this method off the db() so ... db().saveManyToManyAssociations(this, path) -// -// /** -// * Persist a many-to-many association. -// */ -// public void saveManyToManyAssociations(String path) { -// Ebean.saveManyToManyAssociations(this, path); -// } - - // RB: Uncommonly used so get this method off the db() -// /** -// * Persist a many-to-many association. -// * -// * @param server -// * the Ebean server to use -// */ -// public void saveManyToManyAssociations(String server, String path) { -// Ebean.getServer(server).saveManyToManyAssociations(this, path); -// } - - // RB: Uncommonly used so get this method off the db() -// /** -// * Deletes a many-to-many association -// * -// * @param path -// * name of the many-to-many association we want to delete -// */ -// public void deleteManyToManyAssociations(String path) { -// Ebean.deleteManyToManyAssociations(this, path); -// } + /** + * Inserts or update this entity depending on its state. + */ + public void save() { + Ebean.save(this); + } /** * Updates this entity. @@ -158,38 +60,6 @@ public class Model { Ebean.update(this); } - // RB: Uncommon - just use db(server).update(this); -// /** -// * Updates this entity, using a specific Ebean server. -// * -// * @param server -// * the Ebean server to use -// */ -// public void update(String server) { -// Ebean.getServer(server).update(this); -// } - - // RB: ?? -// /** -// * Updates this entity, by specifying the entity ID. -// */ -// public void update(Object id) { -// _setId(id); -// Ebean.update(this); -// } - - // RB: Again ?? -// /** -// * Updates this entity, by specifying the entity ID, using a specific Ebean server. -// * -// * @param server -// * the Ebean server to use -// */ -// public void update(Object id, String server) { -// _setId(id); -// Ebean.getServer(server).update(this); -// } - /** * Deletes this entity. */ @@ -197,17 +67,6 @@ public class Model { Ebean.delete(this); } - // RB: Uncommon - always use db(server) to get other EbeanServer instances -// /** -// * Deletes this entity, using a specific Ebean server. -// * -// * @param server -// * the Ebean server to use -// */ -// public void delete(String server) { -// Ebean.getServer(server).delete(this); -// } - /** * Refreshes this entity from the database. */ @@ -215,15 +74,6 @@ public class Model { Ebean.refresh(this); } -// /** -// * Refreshes this entity from the database, using a specific Ebean server. -// * -// * @param server -// * the Ebean server to use -// */ -// public void refresh(String server) { -// Ebean.getServer(server).refresh(this); -// } @Override public boolean equals(Object other) { @@ -247,9 +97,9 @@ public class Model { } /** - * Helper for Ebean queries. + * Helper for queries. */ - public static class Finder {// implements Query { + public static class Finder { private final Class idType; private final Class type; @@ -257,6 +107,8 @@ public class Model { /** * Creates a finder for entity of type T with ID of type I. + * + *

Typically you use this constructor to have a static "find" field on each entity bean. */ public Finder(Class idType, Class type) { this(null, idType, type); @@ -265,6 +117,8 @@ public class Model { /** * Creates a finder for entity of type T with ID of type I, using a * specific Ebean server. + * + *

Typically you don't need to use this method. */ public Finder(String serverName, Class idType, Class type) { this.type = type; @@ -276,6 +130,24 @@ public class Model { return Ebean.getServer(serverName); } + /** + * Return the underlying 'default' EbeanServer. + * + *

This provides full access to the API such as explicit transaction demarcation etc. + */ + public EbeanServer db() { + return Ebean.getServer(null); + } + + /** + * Return typically a different EbeanServer to the default. + * + * @param server The name of the EbeanServer. If this is null then the default EbeanServer is returned. + */ + public EbeanServer db(String server) { + return Ebean.getServer(server); + } + /** * Changes the Ebean server. */ @@ -298,9 +170,7 @@ public class Model { } /** - * Retrieves an entity reference for this ID. - * - * @deprecated RB: Move to Model + * Creates an entity reference for this ID. */ public T ref(I id) { return server().getReference(type, id); @@ -323,27 +193,12 @@ public class Model { /** * Returns the next identity value. - * - * @deprecated RB: move to model */ + @SuppressWarnings("unchecked") public I nextId() { return (I) server().nextId(type); } - // /** - // * Cancels query execution, if supported by the underlying database and driver. - // */ - // public void cancel() { - // query().cancel(); - // } - - // /** - // * Copies this query. - // */ - // public Query copy() { - // return query().copy(); - // } - /** * Specifies a path to load including all its properties. */ @@ -375,33 +230,15 @@ public class Model { return query().fetch(assocProperty, fetchProperties, fetchConfig); } -// /** -// * Applies a filter on the 'many' property list rather than the root level objects. -// * -// * @deprecated RB: Get this off the query(). Advanced feature. -// */ -// public ExpressionList filterMany(String propertyName) { -// return query().filterMany(propertyName); -// } - /** * Executes a find IDs query in a background thread. * - * @deprecated RB: Hmm, this is a "find all" - less is more, hide from newbies. + * @deprecated RB: Hmm, this is a "find all" - less is more, prefer to hide this - just get from query(). */ public FutureIds findFutureIds() { return query().findFutureIds(); } - /** - * Executes a find list query in a background thread. - * - * @deprecated RB:underlying method deprecated - */ - public FutureList findFutureList() { - return query().findFutureList(); - } - /** * Executes a find row count query in a background thread. */ @@ -417,24 +254,33 @@ public class Model { } /** - * Executes the query and returns the results as a list of objects. + * Retrieves all entities of the given type. + * + *

The same as {@link #all()} */ public List findList() { return query().findList(); } /** - * Executes the query and returns the results as a map of objects. + * Retrieves all entities of the given type as a map of objects. */ public Map findMap() { return query().findMap(); } /** - * Executes the query and returns the results as a map of the objects. + * Executes the query and returns the results as a map of the objects specifying the map key property. */ - public Map findMap(String a, Class b) { - return query().findMap(a, b); + public Map findMap(String keyProperty, Class keyType) { + return query().findMap(keyProperty, keyType); + } + + /** + * Return a PagedList of all entities of the given type (use where() to specify predicates as needed). + */ + public PagedList findPagedList(int pageIndex, int pageSize) { + return query().findPagedList(pageIndex, pageSize); } /** @@ -447,37 +293,19 @@ public class Model { } /** - * Returns the number of entities this query should return. - * - * + * Returns the total number of entities for this type. */ public int findRowCount() { return query().findRowCount(); } /** - * Executes the query and returns the results as a set of objects. + * Returns all the entities of the given type as a set. */ public Set findSet() { return query().findSet(); } - // /** - // * Executes the query and returns the results as either a single bean or null, if - // no matching bean is found. - // */ - // public T findUnique() { - // return query().findUnique(); - // } - - // public void findVisit(QueryResultVisitor visitor) { - // query().findVisit(visitor); - // } - // - // public QueryIterator findIterate() { - // return query().findIterate(); - // } - /** * Returns the ExpressionFactory used by this query. */ @@ -485,62 +313,6 @@ public class Model { return query().getExpressionFactory(); } - // /** - // * Returns the first row value. - // */ - // public int getFirstRow() { - // return query().getFirstRow(); - // } - - // /** - // * Returns the SQL that was generated for executing this query. - // */ - // public String getGeneratedSql() { - // return query().getGeneratedSql(); - // } - - // /** - // * Returns the maximum of rows for this query. - // */ - // public int getMaxRows() { - // return query().getMaxRows(); - // } - - // /** - // * Returns the RawSql that was set to use for this query. - // */ - // public RawSql getRawSql() { - // return query().getRawSql(); - // } - - // /** - // * Returns the query's having clause. - // */ - // public ExpressionList having() { - // return query().having(); - // } - // - // /** - // * Adds an expression to the having clause and returns the query. - // */ - // public Query having(com.avaje.ebean.Expression addExpressionToHaving) { - // return query().having(addExpressionToHaving); - // } - // - // /** - // * Adds clauses to the having clause and returns the query. - // */ - // public Query having(String addToHavingClause) { - // return query().having(addToHavingClause); - // } - // - // /** - // * Returns true if this query was tuned by autoFetch. - // */ - // public boolean isAutofetchTuned() { - // return query().isAutofetchTuned(); - // } - /** * Returns the order by clause so that you can append an ascending or descending * property to the order by clause. @@ -596,27 +368,6 @@ public class Model { return query().setAutofetch(autofetch); } - // /** - // * Sets the rows after which fetching should continue in a background thread. - // */ - // public Query setBackgroundFetchAfter(int backgroundFetchAfter) { - // return query().setBackgroundFetchAfter(backgroundFetchAfter); - // } - // - // /** - // * Sets a hint, which for JDBC translates to Statement.fetchSize(). - // */ - // public Query setBufferFetchSizeHint(int fetchSize) { - // return query().setBufferFetchSizeHint(fetchSize); - // } - // - // /** - // * Sets whether this query uses DISTINCT. - // */ - // public Query setDistinct(boolean isDistinct) { - // return query().setDistinct(isDistinct); - // } - /** * Sets the first row to return for this query. */ @@ -631,13 +382,6 @@ public class Model { return query().setId(id); } - // /** - // * Sets a listener to process the query on a row-by-row basis. - // */ - // public Query setListener(QueryListener queryListener) { - // return query().setListener(queryListener); - // } - /** * When set to true, all the beans from this query are loaded into the bean cache. */ @@ -659,38 +403,6 @@ public class Model { return query().setMaxRows(maxRows); } - // /** - // * Replaces any existing order by clause using an OrderBy object. - // *

- // * This is exactly the same as {@link #setOrderBy(com.avaje.ebean.OrderBy)}. - // */ - // public Query setOrder(OrderBy orderBy) { - // return query().setOrder(orderBy); - // } - // - // /** - // * Set an OrderBy object to replace any existing order by clause. - // *

- // * This is exactly the same as {@link #setOrder(com.avaje.ebean.OrderBy)}. - // */ - // public Query setOrderBy(OrderBy orderBy) { - // return query().setOrderBy(orderBy); - // } - // - // /** - // * Sets an ordered bind parameter according to its position. - // */ - // public Query setParameter(int position, Object value) { - // return query().setParameter(position, value); - // } - // - // /** - // * Sets a named bind parameter. - // */ - // public Query setParameter(String name, Object value) { - // return query().setParameter(name, value); - // } - /** * Sets the OQL query to run */ @@ -712,13 +424,6 @@ public class Model { return query().setReadOnly(readOnly); } - // /** - // * Sets a timeout on this query. - // */ - // public Query setTimeout(int secs) { - // return query().setTimeout(secs); - // } - /** * Sets whether to use the bean cache. */ @@ -758,17 +463,9 @@ public class Model { /** * Execute the select with "for update" which should lock the record "on read" */ - // @Override public Query setForUpdate(boolean forUpdate) { return query().setForUpdate(forUpdate); } - // /** - // * Whether this query is for update - // */ - // @Override - // public boolean isForUpdate() { - // return query().isForUpdate(); - // } } } \ No newline at end of file