package com.avaje.ebean; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import javax.persistence.OptimisticLockException; import com.avaje.ebean.annotation.CacheStrategy; import com.avaje.ebean.cache.ServerCacheManager; import com.avaje.ebean.config.ServerConfig; import com.avaje.ebean.meta.MetaInfoManager; import com.avaje.ebean.text.csv.CsvReader; import com.avaje.ebean.text.json.JsonContext; /** * Provides the API for fetching and saving beans to a particular DataSource. *
* Registration with the Ebean Singleton:
* When a EbeanServer is constructed it can be registered with the Ebean
* singleton (see {@link ServerConfig#setRegister(boolean)}). The Ebean
* singleton is essentially a map of EbeanServer's that have been registered
* with it. The EbeanServer can then be retrieved later via
* {@link Ebean#getServer(String)}.
*
* The 'default' EbeanServer
* One EbeanServer can be designated as the 'default' or 'primary' EbeanServer
* (see {@link ServerConfig#setDefaultServer(boolean)}. Many methods on Ebean
* such as {@link Ebean#find(Class)} etc are actually just a convenient way to
* call methods on the 'default/primary' EbeanServer. This is handy for
* applications that use a single DataSource.
*
* There is one EbeanServer per Database (javax.sql.DataSource). One EbeanServer * is referred to as the 'default' server and that is the one that * Ebean methods such as {@link Ebean#find(Class)} use. *
*
* Constructing a EbeanServer
* EbeanServer's are constructed by the EbeanServerFactory. They can be created
* programmatically via {@link EbeanServerFactory#create(ServerConfig)} or they
* can be automatically constructed on demand using configuration information in
* the ebean.properties file.
*
* Example: Get a EbeanServer *
* *
* // Get access to the Human Resources EbeanServer/Database
* EbeanServer hrServer = Ebean.getServer("HR");
*
*
* // fetch contact 3 from the HR database Contact contact =
* hrServer.find(Contact.class, new Integer(3));
*
* contact.setStatus("INACTIVE"); ...
*
* // save the contact back to the HR database hrServer.save(contact);
*
*
*
* EbeanServer has more API than Ebean
* EbeanServer provides additional API compared with Ebean. For example it
* provides more control over the use of Transactions that is not available in
* the Ebean API.
*
* External Transactions: If you wanted to use transactions created * externally to eBean then EbeanServer provides additional methods where you * can explicitly pass a transaction (that can be created externally). *
** Bypass ThreadLocal Mechanism: If you want to bypass the built in * ThreadLocal transaction management you can use the createTransaction() * method. Example: a single thread requires more than one transaction. *
* * @see Ebean * @see EbeanServerFactory * @see ServerConfig */ public interface EbeanServer { /** * Shutdown the EbeanServer. ** If the under underlying DataSource is the EbeanORM implementation then you * also have the option of shutting down the DataSource and deregistering the * JDBC driver. *
* * @param shutdownDataSource * if true then shutdown the underlying DataSource if it is the EbeanORM * DataSource implementation. * @param deregisterDriver * if true then deregister the JDBC driver if it is the EbeanORM * DataSource implementation. */ public void shutdown(boolean shutdownDataSource, boolean deregisterDriver); /** * Return the AdminAutofetch which is used to control and configure the * Autofetch service at runtime. */ public AdminAutofetch getAdminAutofetch(); /** * Return the name. This is used with {@link Ebean#getServer(String)} to get a * EbeanServer that was registered with the Ebean singleton. */ public String getName(); /** * Return the ExpressionFactory for this server. */ public ExpressionFactory getExpressionFactory(); /** * Return the MetaInfoManager which is used to get meta data from the EbeanServer * such as query execution statistics. */ public MetaInfoManager getMetaInfoManager(); /** * Return the BeanState for a given entity bean. ** This will return null if the bean is not an enhanced (or subclassed) entity * bean. *
*/ public BeanState getBeanState(Object bean); /** * Return the value of the Id property for a given bean. */ public Object getBeanId(Object bean); /** * Return a map of the differences between two objects of the same type. ** When null is passed in for b, then the 'OldValues' of a is used for the * difference comparison. *
*/ public Map* Note that if you are using enhancement (rather than subclassing) then you * do not need to use this method and just new up a bean. *
** Potentially useful when using subclassing and you wish to programmatically * load a entity bean . Otherwise this method is generally not required. *
*/ public* The query statement will be defined in a deployment orm xml file. *
* * @see Ebean#createQuery(Class, String) */ public* Note that you are allowed to add additional clauses using where() as well * as use fetch() and setOrderBy() after the query has been created. *
** Note that this method signature used to map to named queries and that has * moved to {@link #createNamedQuery(Class, String)}. *
* *
* EbeanServer ebeanServer = ... ;
* String q = "find order fetch details where status = :st";
*
* List<Order> newOrders
* = ebeanServer.createQuery(Order.class, q)
* .setParameter("st", Order.Status.NEW)
* .findList();
*
*
* @param query
* the object query
*/
public * This will only work when a IdGenerator is on the bean such as for beans * that use a DB sequence or UUID. *
** For DB's supporting getGeneratedKeys and sequences such as Oracle10 you do * not need to use this method generally. It is made available for more * complex cases where it is useful to get an ID prior to some processing. *
*/ public Object nextId(Class> beanType); /** * Create a filter for filtering lists of entity beans. */ public* The query statement will be defined in a deployment orm xml file. *
* * @see Ebean#createNamedSqlQuery(String) */ public SqlQuery createNamedSqlQuery(String namedQuery); /** * Create a sql update for executing native dml statements (refer * {@link Ebean#createSqlUpdate(String)}). * * @see Ebean#createSqlUpdate(String) */ public SqlUpdate createSqlUpdate(String sql); /** * Create a CallableSql to execute a given stored procedure. */ public CallableSql createCallableSql(String callableSql); /** * Create a named sql update (refer {@link Ebean#createNamedSqlUpdate(String)} * ). ** The statement (an Insert Update or Delete statement) will be defined in a * deployment orm xml file. *
* * @see Ebean#createNamedSqlUpdate(String) */ public SqlUpdate createNamedSqlUpdate(String namedQuery); /** * Create a new transaction that is not held in TransactionThreadLocal. ** You will want to do this if you want multiple Transactions in a single * thread or generally use transactions outside of the TransactionThreadLocal * management. *
*/ public Transaction createTransaction(); /** * Create a new transaction additionally specifying the isolation level. ** Note that this transaction is NOT stored in a thread local. *
*/ public Transaction createTransaction(TxIsolation isolation); /** * Start a new transaction putting it into a ThreadLocal. * * @see Ebean#beginTransaction() */ public Transaction beginTransaction(); /** * Start a transaction additionally specifying the isolation level. */ public Transaction beginTransaction(TxIsolation isolation); /** * Returns the current transaction or null if there is no current transaction * in scope. */ public Transaction currentTransaction(); /** * Commit the current transaction. * * @see Ebean#commitTransaction() */ public void commitTransaction(); /** * Rollback the current transaction. * * @see Ebean#rollbackTransaction() */ public void rollbackTransaction(); /** * If the current transaction has already been committed do nothing otherwise * rollback the transaction. ** Useful to put in a finally block to ensure the transaction is ended, rather * than a rollbackTransaction() in each catch block. *
** Code example: * *
* Ebean.startTransaction(); try { // do some fetching
* and or persisting
*
* // commit at the end Ebean.commitTransaction();
*
* } finally { // if commit didn't occur then rollback the transaction
* Ebean.endTransaction(); }
*
*
*
*
* @see Ebean#endTransaction()
*/
public void endTransaction();
/**
* Refresh the values of a bean.
* * Note that this does not refresh any OneToMany or ManyToMany properties. *
* * @see Ebean#refresh(Object) */ public void refresh(Object bean); /** * Refresh a many property of an entity bean. * * @param bean * the entity bean containing the 'many' property * @param propertyName * the 'many' property to be refreshed * * @see Ebean#refreshMany(Object, String) */ public void refreshMany(Object bean, String propertyName); /** * Find a bean using its unique id. * * @see Ebean#find(Class, Object) */ public* This will not perform a query against the database. *
* * @see Ebean#getReference(Class, Object) */ public