Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * Copyright 2007 Daniel Spiewak
    *
    * Licensed under the Apache License, Version 2.0 (the "License");
    * you may not use this file except in compliance with the License.
    * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package net.java.ao;
  
  import org.slf4j.Logger;
  
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.HashSet;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  
  import static com.google.common.base.Preconditions.checkArgument;
  import static com.google.common.base.Preconditions.checkNotNull;
  import static net.java.ao.Common.getValueFieldsNames;
  import static net.java.ao.Common.preloadValue;
  import static net.java.ao.sql.SqlUtils.closeQuietly;
  import static org.apache.commons.lang.ArrayUtils.contains;

The root control class for the entire ActiveObjects API. EntityManager is the source of all RawEntity objects, as well as the dispatch layer between the entities, the pluggable table name converters, and the database abstraction layers. This is the entry point for any use of the API.

EntityManager is designed to be used in an instance fashion with each instance corresponding to a single database. Thus, rather than a singleton instance or a static factory method, EntityManager does have a proper constructor. Any static instance management is left up to the developer using the API.

Author(s):
Daniel Spiewak
  
  public class EntityManager
  {
  
      private static final Logger log = LoggerFactory.getLogger(EntityManager.class);
  
      private final DatabaseProvider provider;
      private final EntityManagerConfiguration configuration;
  
      private final SchemaConfiguration schemaConfiguration;
      private final NameConverters nameConverters;
  
      private final EntityInfoResolver entityInfoResolver;
  
      private PolymorphicTypeMapper typeMapper;
      private final ReadWriteLock typeMapperLock = new ReentrantReadWriteLock(true);
  
      private final com.google.common.cache.Cache<Class<? extends ValueGenerator<?>>, ValueGenerator<?>> valGenCache;

    
Creates a new instance of EntityManager using the specified DatabaseProvider.

Parameters:
provider the DatabaseProvider to use in all database operations.
configuration the configuration for this entity manager
 
     public EntityManager(DatabaseProvider providerEntityManagerConfiguration configuration)
     {
         this. = checkNotNull(provider);
         this. = checkNotNull(configuration);
          = CacheBuilder.newBuilder().build(new CacheLoader<Class<? extends ValueGenerator<?>>, ValueGenerator<?>>() {
             @Override
             public ValueGenerator<?> load(Class<? extends ValueGenerator<?>> generatorClassthrows Exception {
                 return generatorClass.newInstance();
             }
         });
 
         // TODO: move caching out of there!
          = new CachingNameConverters(configuration.getNameConverters());
          = checkNotNull(configuration.getSchemaConfiguration());
 
          = new DefaultPolymorphicTypeMapper(new HashMap<Class<? extends RawEntity<?>>, String>());
 
          = checkNotNull(configuration.getEntityInfoResolverFactory().create(provider.getTypeManager()), "entityInfoResolver");
     }


    
Convenience method to create the schema for the specified entities using the current settings (table/field name converter and database provider).

 
     public void migrate(Class<? extends RawEntity<?>>... entitiesthrows SQLException
     {
         SchemaGenerator.migrate(falseentities);
     }

    
Convenience method to create the schema for the specified entities using the current settings (table/field name converter and database provider). Note that if the given entities do not include the full set of entities, or those entities have removed any fields, then the corresponding tables or columns will be dropped, and any data they contained will be lost. Use this at your own risk.

 
     public void migrateDestructively(Class<? extends RawEntity<?>>... entitiesthrows SQLException
     {
         SchemaGenerator.migrate(trueentities);
     }

    

Deprecated:
since 0.23. EntityManager now no longer caches entities. use flush(net.java.ao.RawEntity[]) to flush values for individual entities
 
     @Deprecated
     public void flushAll()
     {
         // no-op
     }

    

Deprecated:
since 0.23. EntityManager now no longer caches entities. use flush(net.java.ao.RawEntity[]) to flush values for individual entities
 
     @Deprecated
     public void flushEntityCache()
     {
         // no-op
     }

    

Deprecated:
since 0.25. Entities and values now no longer cached.
 
     @Deprecated
 	public void flush(RawEntity<?>... entities) {
         // no-op
 	}

Returns an array of entities of the specified type corresponding to the varargs primary keys. If an in-memory reference already exists to a corresponding entity (of the specified type and key), it is returned rather than creating a new instance.

If the entity is known to exist in the database, then no checks are performed and the method returns extremely quickly. However, for any key which has not already been verified, a query to the database is performed to determine whether or not the entity exists. If the entity does not exist, then null is returned.

Parameters:
type The type of the entities to retrieve.
keys The primary keys corresponding to the entities to retrieve. All keys must be typed according to the generic type parameter of the entity's RawEntity inheritence (if inheriting from Entity, this is Integer or int). Thus, the keys array is type-checked at compile time.
Returns:
An array of entities of the given type corresponding with the specified primary keys. Any entities which are non-existent will correspond to a null value in the resulting array.
 
 	public <T extends RawEntity<K>, K> T[] get(final Class<T> type, K... keysthrows SQLException
     {
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
         final String primaryKeyField = entityInfo.getPrimaryKey().getName();
         return get(typefindByPrimaryKey(typeprimaryKeyField), keys);
     }
 
     private <T extends RawEntity<K>, K> Function<T, K> findByPrimaryKey(final Class<T> typefinal String primaryKeyField)
     {
         return new Function<T, K>()
         {
             @Override
             public T invoke(K kthrows SQLException
             {
                 final T[] ts = find(typeprimaryKeyField + " = ?"k);
                 if (ts.length == 1)
                 {
                     return ts[0];
                 }
                 else if (ts.length == 0)
                 {
                     return null;
                 }
                 else
                 {
                     throw new ActiveObjectsException("Found more that one object of type '" + type.getName() + "' for key '" + k + "'");
                 }
             }
         };
     }
 
     protected <T extends RawEntity<K>, K> T[] peer(final EntityInfo<T, K> entityInfo, K... keysthrows SQLException
     {
         return get(entityInfo.getEntityType(), new Function<T, K>()
         {
             public T invoke(K key)
             {
                 return getAndInstantiate(entityInfokey);
             }
         }, keys);
     }
 
 
     private <T extends RawEntity<K>, K> T[] get(Class<T> typeFunction<T, K> create, K... keysthrows SQLException
     {
         //noinspection unchecked
         T[] back = (T[])Array.newInstance(typekeys.length);
         int index = 0;
 
 		for (K key : keys) {
             back[index++] = create.invoke(key);
         }
 
         return back;
     }

    
Creates a new instance of the entity of the specified type corresponding to the given primary key. This is used by get(java.lang.Class,java.lang.Object[])} to create the entity.

Parameters:
entityInfo The type of the entity to create.
key The primary key corresponding to the entity instance required.
Returns:
An entity instance of the specified type and primary key.
 
     protected <T extends RawEntity<K>, K> T getAndInstantiate(EntityInfo<T, K> entityInfo, K key)
     {
         Class<T> type = entityInfo.getEntityType();
         EntityProxy<T, K> proxy = new EntityProxy<T, K>(thisentityInfokey);
 
 		T entity = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{typeEntityProxyAccessor.class}, proxy));
 		return entity;
 	}

    
Cleverly overloaded method to return a single entity of the specified type rather than an array in the case where only one ID is passed. This method meerly delegates the call to the overloaded get method and functions as syntactical sugar.

Parameters:
type The type of the entity instance to retrieve.
key The primary key corresponding to the entity to be retrieved.
Returns:
An entity instance of the given type corresponding to the specified primary key, or null if the entity does not exist in the database.
See also:
get(java.lang.Class,java.lang.Object[])
 
     public <T extends RawEntity<K>, K> T get(Class<T> type, K keythrows SQLException
     {
         return get(typetoArray(key))[0];
     }
 
     protected <T extends RawEntity<K>, K> T peer(EntityInfo<T, K> entityInfo, K keythrows SQLException
     {
         if (null == key) {
             return null;
         }
         
         return peer(entityInfotoArray(key))[0];
     }
 
     @SuppressWarnings("unchecked")
     private static <K> K[] toArray(K key)
     {
         return (K[])new Object[]{key};
     }

    

Creates a new entity of the specified type with the optionally specified initial parameters. This method actually inserts a row into the table represented by the entity type and returns the entity instance which corresponds to that row.

The DBParam object parameters are designed to allow the creation of entities which have non-null fields which have no defalut or auto-generated value. Insertion of a row without such field values would of course fail, thus the need for db params. The db params can also be used to set the values for any field in the row, leading to more compact code under certain circumstances.

Unless within a transaction, this method will commit to the database immediately and exactly once per call. Thus, care should be taken in the creation of large numbers of entities. There doesn't seem to be a more efficient way to create large numbers of entities, however one should still be aware of the performance implications.

This method delegates the action INSERT action to DatabaseProvider.insertReturningKey(net.java.ao.EntityManager,java.sql.Connection,java.lang.Class,java.lang.Class,java.lang.String,boolean,java.lang.String,net.java.ao.DBParam[]). This is necessary because not all databases support the JDBC RETURN_GENERATED_KEYS constant (e.g. PostgreSQL and HSQLDB). Thus, the database provider itself is responsible for handling INSERTion and retrieval of the correct primary key value.

Parameters:
type The type of the entity to INSERT.
params An optional varargs array of initial values for the fields in the row. These values will be passed to the database within the INSERT statement.
Returns:
The new entity instance corresponding to the INSERTed row.
See also:
DBParam
 
 	public <T extends RawEntity<K>, K> T create(Class<T> typeDBParam... paramsthrows SQLException {
 		T back = null;
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
 		final String table = entityInfo.getName();
 
         Set<DBParamlistParams = new HashSet<DBParam>();
         listParams.addAll(Arrays.asList(params));
 
         for (FieldInfo fieldInfo : Iterables.filter(entityInfo.getFields(), .))
         {
 
             ValueGenerator<?> generator;
             try
             {
                 generator = .get(fieldInfo.getGeneratorType());
             }
             catch (ExecutionException e)
             {
                 throw Throwables.propagate(e.getCause());
             }
             catch (UncheckedExecutionException e)
             {
                 throw Throwables.propagate(e.getCause());
             }
             catch (ExecutionError e)
             {
                 throw Throwables.propagate(e.getCause());
             }
 
             listParams.add(new DBParam(fieldInfo.getName(), generator.generateValue(this)));
         }
 
         Set<FieldInforequiredFields = Sets.newHashSet(Sets.filter(entityInfo.getFields(), .));
 
         for (DBParam param : listParams)
         {
             FieldInfo field = entityInfo.getField(param.getField());
             checkNotNull(field"Entity %s does not have field %s"type.getName(), param.getField());
 
             if (field.isPrimary())
             {
                 //noinspection unchecked
                 Common.validatePrimaryKey(fieldparam.getValue());
             }
             else if (!field.isNullable())
             {
                 checkArgument(param.getValue() != null"Cannot set non-null field %s to null"param.getField());
                 if (param.getValue() instanceof String)
                 {
                     checkArgument(!StringUtils.isBlank((Stringparam.getValue()), "Cannot set non-null String field %s to ''"param.getField());
                 }
             }
 
             requiredFields.remove(field);
 
             final TypeInfo dbType = field.getTypeInfo();
             if (dbType != null && param.getValue() != null)
             {
                 dbType.getLogicalType().validate(param.getValue());
             }
         }
 
         if (!requiredFields.isEmpty())
         {
             throw new IllegalArgumentException("The follow required fields were not set when trying to create entity '" + type.getName() + "', those fields are: " + Joiner.on(", ").join(requiredFields));
         }
 
         Connection connection = null;
         try
         {
             connection = .getConnection();
             back = peer(entityInfo.insertReturningKey(thisconnection,
                                                           type,
                                                           entityInfo.getPrimaryKey().getJavaType(),
                                                           entityInfo.getPrimaryKey().getName(),
                                                           entityInfo.getPrimaryKey().hasAutoIncrement(),
                                                           tablelistParams.toArray(new DBParam[listParams.size()])));
         }
         finally
         {
             closeQuietly(connection);
         }
 		back.init();
 		return back;
 	}

    
Creates and INSERTs a new entity of the specified type with the given map of parameters. This method merely delegates to the create(java.lang.Class,net.java.ao.DBParam[]) method. The idea behind having a separate convenience method taking a map is in circumstances with large numbers of parameters or for people familiar with the anonymous inner class constructor syntax who might be more comfortable with creating a map than with passing a number of objects.

Parameters:
type The type of the entity to INSERT.
params A map of parameters to pass to the INSERT.
Returns:
The new entity instance corresponding to the INSERTed row.
See also:
create(java.lang.Class,net.java.ao.DBParam[])
 
     public <T extends RawEntity<K>, K> T create(Class<T> typeMap<StringObjectparamsthrows SQLException
     {
         DBParam[] arrParams = new DBParam[params.size()];
         int i = 0;
 
         for (String key : params.keySet())
         {
             arrParams[i++] = new DBParam(keyparams.get(key));
         }
 
         return create(typearrParams);
     }

    

Deletes the specified entities from the database. DELETE statements are called on the rows in the corresponding tables. The entity instances themselves are not invalidated, but it doesn't even make sense to continue using the instance without a row with which it is paired.

This method does attempt to group the DELETE statements on a per-type basis. Thus, if you pass 5 instances of EntityA and two instances of EntityB, the following SQL prepared statements will be invoked:

DELETE FROM entityA WHERE id IN (?,?,?,?,?);
 DELETE FROM entityB WHERE id IN (?,?);

Thus, this method scales very well for large numbers of entities grouped into types. However, the execution time increases linearly for each entity of unique type.

Parameters:
entities A varargs array of entities to delete. Method returns immediately if length == 0.
 
     @SuppressWarnings("unchecked")
     public void delete(RawEntity<?>... entitiesthrows SQLException
     {
         if (entities.length == 0)
         {
             return;
         }
 
         Map<Class<? extends RawEntity<?>>, List<RawEntity<?>>> organizedEntities =
             new HashMap<Class<? extends RawEntity<?>>, List<RawEntity<?>>>();
 
         for (RawEntity<?> entity : entities)
         {
             verify(entity);
             Class<? extends RawEntity<?>> type = getProxyForEntity(entity).getType();
 
             if (!organizedEntities.containsKey(type))
             {
                 organizedEntities.put(typenew LinkedList<RawEntity<?>>());
             }
             organizedEntities.get(type).add(entity);
         }
 
         Connection conn = null;
         PreparedStatement stmt = null;
         try
         {
             conn = .getConnection();
             for (Class type : organizedEntities.keySet()) {
                 EntityInfo entityInfo = resolveEntityInfo(type);
                 List<RawEntity<?>> entityList = organizedEntities.get(type);
 
                 StringBuilder sql = new StringBuilder("DELETE FROM ");
                 sql.append(.withSchema(entityInfo.getName()));
                 sql.append(" WHERE ").append(.processID(entityInfo.getPrimaryKey().getName())).append(" IN (?");
 
                 for (int i = 1; i < entityList.size(); i++) {
                     sql.append(",?");
                 }
                 sql.append(')');
 
                 stmt = .preparedStatement(connsql);
 
                 int index = 1;
                 for (RawEntity<?> entity : entityList) {
                     TypeInfo typeInfo = entityInfo.getPrimaryKey().getTypeInfo();
                     typeInfo.getLogicalType().putToDatabase(thisstmtindex++, Common.getPrimaryKeyValue(entity), typeInfo.getJdbcWriteType());
                 }
                 stmt.executeUpdate();
             }
         }
         finally
         {
             closeQuietly(stmt);
             closeQuietly(conn);
         }
     }

    

Deletes rows from the table corresponding to type. In contrast to delete(net.java.ao.RawEntity[]), this method allows you to delete rows without creating entities for them first.

Example:

manager.deleteWithSQL(Person.class, "name = ?", "Charlie")

The SQL in criteria is not parsed or modified in any way by ActiveObjects, and is simply appended to the DELETE statement in a WHERE clause. The above example would cause an SQL statement similar to the following to be executed:

DELETE FROM people WHERE name = 'Charlie';

If criteria is null, this method deletes all rows from the table corresponding to type.

This method does not attempt to determine the set of entities affected by the statement. As such, it is recommended that you call flushAll() after calling this method.

Parameters:
type The entity type corresponding to the table to delete from.
criteria An optional SQL fragment specifying which rows to delete.
parameters A varargs array of parameters to be passed to the executed prepared statement. The length of this array must match the number of parameters (denoted by the '?' char) in criteria.
Returns:
The number of rows deleted from the table.
See also:
delete(net.java.ao.RawEntity[])
find(java.lang.Class,java.lang.String,java.lang.Object[])
findWithSQL(java.lang.Class,java.lang.String,java.lang.String,java.lang.Object[])
 
     public <K> int deleteWithSQL(Class<? extends RawEntity<K>> typeString criteriaObject... parametersthrows SQLException
     {
         int rowCount = 0;
         StringBuilder sql = new StringBuilder("DELETE FROM ");
 
         if (criteria != null)
         {
             sql.append(" WHERE ");
             sql.append(.processWhereClause(criteria));
         }
 
         final Connection connection = .getConnection();
         try
         {
             final PreparedStatement stmt = .preparedStatement(connectionsql);
             try
             {
                 putStatementParameters(stmtparameters);
                 rowCount = stmt.executeUpdate();
             }
             finally
             {
                 stmt.close();
             }
         }
         finally
         {
             connection.close();
         }
 
         return rowCount;
     }

    
Returns all entities of the given type. This actually peers the call to the find(java.lang.Class,net.java.ao.Query) method.

Parameters:
type The type of entity to retrieve.
Returns:
An array of all entities which correspond to the given type.
 
     public <T extends RawEntity<K>, K> T[] find(Class<T> typethrows SQLException
     {
         return find(type, Query.select());
     }

    

Convenience method to select all entities of the given type with the specified, parameterized criteria. The criteria String specified is appended to the SQL prepared statement immediately following the WHERE.

Example:

manager.find(Person.class, "name LIKE ? OR age > ?", "Joe", 9);

This actually delegates the call to the find(java.lang.Class,net.java.ao.Query) method, properly parameterizing the Query object.

Parameters:
type The type of the entities to retrieve.
criteria A parameterized WHERE statement used to determine the results.
parameters A varargs array of parameters to be passed to the executed prepared statement. The length of this array must match the number of parameters (denoted by the '?' char) in the criteria.
Returns:
An array of entities of the given type which match the specified criteria.
 
     public <T extends RawEntity<K>, K> T[] find(Class<T> typeString criteriaObject... parametersthrows SQLException
     {
         return find(type, Query.select().where(criteriaparameters));
     }

    

Convenience method to select a single entity of the given type with the specified, parameterized criteria. The criteria String specified is appended to the SQL prepared statement immediately following the WHERE.

Example:

manager.findSingleEntity(Person.class, "name LIKE ? OR age > ?", "Joe", 9);

This actually delegates the call to the find(java.lang.Class,java.lang.String,java.lang.Object[]) method, properly parameterizing the java.lang.Object object.

Parameters:
type The type of the entities to retrieve.
criteria A parameterized WHERE statement used to determine the results.
parameters A varargs array of parameters to be passed to the executed prepared statement. The length of this array must match the number of parameters (denoted by the '?' char) in the criteria.
Returns:
A single entity of the given type which match the specified criteria or null if none returned
 
     public <T extends RawEntity<K>, K> T findSingleEntity(Class<T> typeString criteriaObject... parametersthrows SQLException
     {
         T[] entities = find(typecriteriaparameters);
 
         if (entities.length < 1)
         {
             return null;
         }
         else if (entities.length > 1)
         {
             throw new IllegalStateException("Found more than one entities of type '"
                                                 + type.getSimpleName() + "' that matched the criteria '" + criteria
                                                 + "' and parameters '" + parameters.toString() + "'.");
         }
 
         return entities[0];
     }

    

Selects all entities matching the given type and Query. By default, the entities will be created based on the values within the primary key field for the specified type (this is usually the desired behavior).

Example:

manager.find(Person.class, Query.select().where("name LIKE ? OR age > ?", "Joe", 9).limit(10));

This method delegates the call to find(java.lang.Class,java.lang.String,net.java.ao.Query), passing the primary key field for the given type as the String parameter.

Note that in the case of calling this function with a Query with select fields, the first field will be passed to find(java.lang.Class,java.lang.String,net.java.ao.Query). If this is not the intention, a direct call to find(java.lang.Class,java.lang.String,net.java.ao.Query) should be made instead, with the primary key field specified and present in the select fields.

Parameters:
type The type of the entities to retrieve.
query The Query instance to be used to determine the results.
Returns:
An array of entities of the given type which match the specified query.
 
     public <T extends RawEntity<K>, K> T[] find(Class<T> typeQuery querythrows SQLException
     {
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
 
         query.resolvePrimaryKey(entityInfo.getPrimaryKey());
         String selectField = entityInfo.getPrimaryKey().getName();
 
         Iterable<Stringfields = query.getFields();
         if (Iterables.size(fields) == 1)
         {
             selectField = Iterables.get(fields, 0);
         }
 
         return find(typeselectFieldquery);
     }

    

Selects all entities of the specified type which match the given Query. This method creates a PreparedStatement using the Query instance specified against the table represented by the given type. This query is then executed (with the parameters specified in the query). The method then iterates through the result set and extracts the specified field, mapping an entity of the given type to each row. This array of entities is returned.

Parameters:
type The type of the entities to retrieve.
field The field value to use in the creation of the entities. This is usually the primary key field of the corresponding table.
query The Query instance to use in determining the results.
Returns:
An array of entities of the given type which match the specified query.
 
     public <T extends RawEntity<K>, K> T[] find(Class<T> typeString fieldQuery querythrows SQLException
     {
         List<T> back = new ArrayList<T>();
 
 
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
 
         query.resolvePrimaryKey(entityInfo.getPrimaryKey());
 
         // legacy support for "*" in the select - to be removed after AO-552 implemented
         boolean starSelected = false;
         for (final String selectedField : query.getFields())
         {
             if ("*".equals(selectedField))
             {
                 // change the field to the PK; not sure how this ever worked, but being safe
                 field = entityInfo.getPrimaryKey().getName();
 
                 starSelected = true;
                 break;
             }
         }
 
         final Preload preloadAnnotation = type.getAnnotation(Preload.class);
         final Set<StringselectedFields;
         if (starSelected || preloadAnnotation == null || contains(preloadAnnotation.value(), .))
         {
             // select all fields from the table - no preload is specified, the user has asked for all or "*" is selected
             selectedFields = getValueFieldsNames(entityInfo.getFieldNameConverter());
         }
         else
         {
             // select user's selection, as well as any specific preloads
             selectedFields = new HashSet<String>(preloadValue(preloadAnnotation.getFieldNameConverter()));
             for (String existingField : query.getFields())
             {
                 selectedFields.add(existingField);
             }
         }
         query.setFields(selectedFields.toArray(new String[selectedFields.size()]));
 
         Connection conn = null;
         PreparedStatement stmt = null;
         ResultSet res = null;
         try
         {
             conn = .getConnection();
             final String sql = query.toSQL(entityInfogetTableNameConverter(), false);
 
             stmt = .preparedStatement(connsql..);
             .setQueryStatementProperties(stmtquery);
 
             query.setParameters(thisstmt);
 
             res = stmt.executeQuery();
             .setQueryResultSetProperties(resquery);
 
             final TypeInfo<K> primaryKeyType = entityInfo.getPrimaryKey().getTypeInfo();
             final Class<K> primaryKeyClassType = entityInfo.getPrimaryKey().getJavaType();
             final String[] canonicalFields = query.getCanonicalFields(entityInfo);
             while (res.next())
             {
                 final T entity = peer(entityInfoprimaryKeyType.getLogicalType().pullFromDatabase(thisresprimaryKeyClassTypefield));
                 final Map<StringObjectvalues = new HashMap<StringObject>();
                 for (String name : canonicalFields)
                 {
                     final FieldInfo fieldInfo = entityInfo.getField(name);
                     final TypeInfo<K> typeInfo = fieldInfo.getTypeInfo();
                     final LogicalType logicalType = typeInfo.getLogicalType();
                     
                     values.put(namelogicalType.pullFromDatabase(this,res,fieldInfo.getJavaType(),name));
                 }
                 if (!values.isEmpty())
                 {
                     final EntityProxy<?, ?> proxy = getProxyForEntity(entity);
                     proxy.updateValues(values);
                 }
                 back.add(entity);
             }
         }
         finally
         {
             closeQuietly(resstmtconn);
         }
         return back.toArray((T[]) Array.newInstance(typeback.size()));
     }

    

Executes the specified SQL and extracts the given key field, wrapping each row into a instance of the specified type. The SQL itself is executed as a java.sql.PreparedStatement with the given parameters.

Example:

manager.findWithSQL(Person.class, "personID", "SELECT personID FROM chairs WHERE position < ? LIMIT ?",
 10,
 5);

The SQL is not parsed or modified in any way by ActiveObjects. As such, it is possible to execute database-specific queries using this method without realizing it. For example, the above query will not run on MS SQL Server or Oracle, due to the lack of a LIMIT clause in their SQL implementation. As such, be extremely careful about what SQL is executed using this method, or else be conscious of the fact that you may be locking yourself to a specific DBMS.

Parameters:
type The type of the entities to retrieve.
keyField The field value to use in the creation of the entities. This is usually the primary key field of the corresponding table.
sql The SQL statement to execute.
parameters A varargs array of parameters to be passed to the executed prepared statement. The length of this array must match the number of parameters (denoted by the '?' char) in the criteria.
Returns:
An array of entities of the given type which match the specified query.
 
     @SuppressWarnings("unchecked")
     public <T extends RawEntity<K>, K> T[] findWithSQL(Class<T> typeString keyFieldString sqlObject... parametersthrows SQLException
     {
         List<T> back = new ArrayList<T>();
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
 
         Connection connection = null;
         PreparedStatement stmt = null;
         ResultSet res = null;
         try
         {
             connection = .getConnection();
             stmt = .preparedStatement(connectionsql);
             putStatementParameters(stmtparameters);
 
             res = stmt.executeQuery();
             while (res.next())
             {
                 back.add(peer(entityInfoentityInfo.getPrimaryKey().getTypeInfo().getLogicalType().pullFromDatabase(thisres, (Class<K>) typekeyField)));
             }
         }
         finally
         {
             closeQuietly(res);
             closeQuietly(stmt);
             closeQuietly(connection);
         }
 
         return back.toArray((T[]) Array.newInstance(typeback.size()));
     }

    

Opitimsed read for large datasets. This method will stream all rows for the given type to the given callback.

Please see stream(java.lang.Class,net.java.ao.Query,net.java.ao.EntityStreamCallback) for details / limitations.

Parameters:
type The type of the entities to retrieve.
streamCallback The receiver of the data, will be passed one entity per returned row
 
     public <T extends RawEntity<K>, K> void stream(Class<T> typeEntityStreamCallback<T, K> streamCallbackthrows SQLException
     {
         stream(type, Query.select("*"), streamCallback);
     }

    

Selects all entities of the given type and feeds them to the callback, one by one. The entities are slim, read-only representations of the data. They only supports getters or designated Accessor methods. Calling setters or

save
will result in an exception. Other method calls will be ignored. The proxies do not support lazy-loading of related entities.

Only the fields specified in the Query are loaded. Since lazy loading is not supported, calls to unspecified getters will return null (or AO's defaults in case of primitives)

This call is optimised for efficient read operations on large datasets. For best memory usage, do not buffer the entities passed to the callback but process and discard them directly.

Unlike regular Entities, the read only implementations do not support flushing/refreshing. The data is a snapshot view at the time of query.

Parameters:
type The type of the entities to retrieve.
query
streamCallback The receiver of the data, will be passed one entity per returned row
 
     public <T extends RawEntity<K>, K> void stream(Class<T> typeQuery queryEntityStreamCallback<T, K> streamCallbackthrows SQLException
     {
         EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
 
         query.resolvePrimaryKey(entityInfo.getPrimaryKey());
 
         final String[] canonicalFields = query.getCanonicalFields(entityInfo);
 
         // Execute the query
         Connection conn = null;
         PreparedStatement stmt = null;
         ResultSet res = null;
         try
         {
             conn = .getConnection();
             final String sql = query.toSQL(entityInfogetTableNameConverter(), false);
 
             // we're only going over the result set once, so use the slimmest possible cursor type
             stmt = .preparedStatement(connsql..);
             .setQueryStatementProperties(stmtquery);
 
             query.setParameters(thisstmt);
 
             res = stmt.executeQuery();
             .setQueryResultSetProperties(resquery);
 
             while (res.next())
             {
                 K primaryKey = entityInfo.getPrimaryKey().getTypeInfo().getLogicalType().pullFromDatabase(thisresentityInfo.getPrimaryKey().getJavaType(), entityInfo.getPrimaryKey().getName());
                 ReadOnlyEntityProxy<T, K> proxy = createReadOnlyProxy(entityInfoprimaryKey);
                 T entity = type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class[]{type}, proxy));
 
                 // transfer the values from the result set into the local proxy value store. We're not caching the proxy itself anywhere, since
                 // it's designated as a read-only snapshot view of the data and thus doesn't need flushing.
                 for (String fieldName : canonicalFields)
                 {
                     proxy.addValue(fieldNameres);
                 }
 
                 // forward the proxy to the callback for the client to consume
                 streamCallback.onRowRead(entity);
             }
         }
         finally
         {
             closeQuietly(resstmtconn);
         }
     }
 
     private <T extends RawEntity<K>, K> ReadOnlyEntityProxy<T, K> createReadOnlyProxy(EntityInfo<T, K> entityInfo, K primaryKey)
     {
         return new ReadOnlyEntityProxy<T, K>(thisentityInfoprimaryKey);
     }

    
Counts all entities of the specified type. This method is actually a delegate for: count(Class<? extends Entity>, Query)

Parameters:
type The type of the entities which should be counted.
Returns:
The number of entities of the specified type.
 
     public <K> int count(Class<? extends RawEntity<K>> typethrows SQLException
     {
         return count(type, Query.select());
     }

    
Counts all entities of the specified type matching the given criteria and parameters. This is a convenience method for: count(type, Query.select().where(criteria, parameters))

Parameters:
type The type of the entities which should be counted.
criteria A parameterized WHERE statement used to determine the result set which will be counted.
parameters A varargs array of parameters to be passed to the executed prepared statement. The length of this array must match the number of parameters (denoted by the '?' char) in the criteria.
Returns:
The number of entities of the given type which match the specified criteria.
 
     public <K> int count(Class<? extends RawEntity<K>> typeString criteriaObject... parametersthrows SQLException
     {
         return count(type, Query.select().where(criteriaparameters));
     }

    
Counts all entities of the specified type matching the given Query instance. The SQL runs as a SELECT COUNT(*) to ensure maximum performance.

Parameters:
type The type of the entities which should be counted.
query The Query instance used to determine the result set which will be counted.
Returns:
The number of entities of the given type which match the specified query.
 
     public <K> int count(Class<? extends RawEntity<K>> typeQuery querythrows SQLException
     {
         EntityInfo entityInfo = resolveEntityInfo(type);
         Connection connection = null;
         PreparedStatement stmt = null;
         ResultSet res = null;
         try
         {
             connection = .getConnection();
             final String sql = query.toSQL(entityInfogetTableNameConverter(), true);
 
             stmt = .preparedStatement(connectionsql);
             .setQueryStatementProperties(stmtquery);
 
             query.setParameters(thisstmt);
 
             res = stmt.executeQuery();
             return res.next() ? res.getInt(1) : -1;
 
         }
         finally
         {
             closeQuietly(res);
             closeQuietly(stmt);
             closeQuietly(connection);
         }
     }
 
     {
         return ;
    }
    protected <T extends RawEntity<K>, K> EntityInfo<T, K> resolveEntityInfo(Class<T> type) {
        return .resolve(type);
    }

    
Retrieves the net.java.ao.schema.TableNameConverter instance used for name conversion of all entity types.
    {
        return .getTableNameConverter();
    }

    
Retrieves the net.java.ao.schema.FieldNameConverter instance used for name conversion of all entity methods.
    {
        return .getFieldNameConverter();
    }

    
Specifies the PolymorphicTypeMapper instance to use for all flag value conversion of polymorphic types. The default type mapper is an empty DefaultPolymorphicTypeMapper instance (thus using the fully qualified classname for all values).

    public void setPolymorphicTypeMapper(PolymorphicTypeMapper typeMapper)
    {
        .writeLock().lock();
        try
        {
            this. = typeMapper;
            if (typeMapper instanceof DefaultPolymorphicTypeMapper)
            {
                ((DefaultPolymorphicTypeMapper)typeMapper).resolveMappings(getTableNameConverter());
            }
        }
        finally
        {
            .writeLock().unlock();
        }
    }

    
Retrieves the PolymorphicTypeMapper instance used for flag value conversion of polymorphic types.

    {
        .readLock().lock();
        try
        {
            if ( == null)
            {
                throw new RuntimeException("No polymorphic type mapper was specified");
            }
            return ;
        }
        finally
        {
            .readLock().unlock();
        }
    }

    

Retrieves the database provider used by this EntityManager for all database operations. This method can be used reliably to obtain a database provider and hence a java.sql.Connection instance which can be used for JDBC operations outside of ActiveObjects. Thus:

Connection conn = manager.getProvider().getConnection();
 try {
     // ...
 } finally {
     conn.close();
 }
    {
        return ;
    }
    <T extends RawEntity<K>, K> EntityProxy<T, K> getProxyForEntity(T entity) {
        return ((EntityProxyAccessorentity).getEntityProxy();
    }
    private void verify(RawEntity<?> entity)
    {
        if (entity.getEntityManager() != this)
        {
            throw new RuntimeException("Entities can only be used with a single EntityManager instance");
        }
    }
    private void putStatementParameters(PreparedStatement stmtObject... parametersthrows SQLException
    {
        for (int i = 0; i < parameters.length; ++i)
        {
            Object parameter = parameters[i];
            Class entityTypeOrClass = (parameter instanceof RawEntity) ? ((RawEntity)parameter).getEntityType() : parameter.getClass();
            @SuppressWarnings("unchecked"TypeInfo<ObjecttypeInfo = .getTypeManager().getType(entityTypeOrClass);
            typeInfo.getLogicalType().putToDatabase(thisstmti + 1, parametertypeInfo.getJdbcWriteType());
        }
    }
    private static interface Function<R, F>
    {
        public R invoke(F formalsthrows SQLException;
    }
New to GrepCode? Check out our FAQ X