public static <T extends RawEntity<K>, K> T createPeer(EntityManager manager, Class<T> type, K key) throws SQLException { return manager.peer(manager.resolveEntityInfo(type), key); }
public static <T extends RawEntity<K>, K> T createPeer(EntityManager manager, Class<T> type, K key) throws SQLException { return manager.peer(manager.resolveEntityInfo(type), key); }
/** * <p>Optimised read for large datasets. This method will stream all rows for the given type to the given * callback.</p> * * <p>Please see {@link #stream(Class, Query, EntityStreamCallback)} for details / limitations. * * @param type The type of the entities to retrieve. * @param streamCallback The receiver of the data, will be passed one entity per returned row */ public <T extends RawEntity<K>, K> void stream(Class<T> type, EntityStreamCallback<T, K> streamCallback) throws SQLException { final EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final Set<String> valueFields = getValueFieldsNames(entityInfo, nameConverters.getFieldNameConverter()); final Query query = Query.select(); query.setFields(valueFields.toArray(new String[valueFields.size()])); stream(type, query, streamCallback); }
/** * <p>Optimised read for large datasets. This method will stream all rows for the given type to the given * callback.</p> * * <p>Please see {@link #stream(Class, Query, EntityStreamCallback)} for details / limitations. * * @param type The type of the entities to retrieve. * @param streamCallback The receiver of the data, will be passed one entity per returned row */ public <T extends RawEntity<K>, K> void stream(Class<T> type, EntityStreamCallback<T, K> streamCallback) throws SQLException { final EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final Set<String> valueFields = getValueFieldsNames(entityInfo, nameConverters.getFieldNameConverter()); final Query query = Query.select(); query.setFields(valueFields.toArray(new String[valueFields.size()])); stream(type, query, streamCallback); }
EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
conn = provider.getConnection(); for (Class type : organizedEntities.keySet()) { EntityInfo entityInfo = resolveEntityInfo(type); List<RawEntity<?>> entityList = organizedEntities.get(type);
EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
conn = provider.getConnection(); for (Class type : organizedEntities.keySet()) { EntityInfo entityInfo = resolveEntityInfo(type); List<RawEntity<?>> entityList = organizedEntities.get(type);
private RawEntity[] fetchOneToMany(final Method method, final OneToMany annotation) throws SQLException, NoSuchMethodException { final Class remoteType = method.getReturnType().getComponentType(); final EntityInfo entityInfo = manager.resolveEntityInfo(remoteType); final String remotePrimaryKeyFieldName = Common.getPrimaryKeyField(remoteType, getFieldNameConverter()); final String whereClause = where(annotation, getFieldNameConverter());
/** * Counts all entities of the specified type matching the given {@link Query} instance. The SQL runs as a * <code>SELECT COUNT(*)</code> to ensure maximum performance. * * @param type The type of the entities which should be counted. * @param query The {@link Query} instance used to determine the result set which will be counted. * @return The number of entities of the given type which match the specified query. */ public <K> int count(Class<? extends RawEntity<K>> type, Query query) throws SQLException { EntityInfo entityInfo = resolveEntityInfo(type); Connection connection = null; PreparedStatement stmt = null; ResultSet res = null; try { connection = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), true); stmt = provider.preparedStatement(connection, sql); provider.setQueryStatementProperties(stmt, query); query.setParameters(this, stmt); res = stmt.executeQuery(); return res.next() ? res.getInt(1) : -1; } finally { closeQuietly(res); closeQuietly(stmt); closeQuietly(connection); } }
public <T extends RawEntity<K>, K> T[] findWithSQL(Class<T> type, String keyField, String sql, Object... parameters) throws SQLException { List<T> back = new ArrayList<T>(); EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
public <T extends RawEntity<K>, K> T[] findWithSQL(Class<T> type, String keyField, String sql, Object... parameters) throws SQLException { List<T> back = new ArrayList<T>(); EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
/** * <p>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.</p> * * <p>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 <code>null</code> * is returned.</p> * * @param type The type of the entities to retrieve. * @param 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 * {@link RawEntity} inheritence (if inheriting from {@link Entity}, this is <code>Integer</code> * or <code>int</code>). Thus, the <code>keys</code> array is type-checked at compile * time. * @return An array of entities of the given type corresponding with the specified * primary keys. Any entities which are non-existent will correspond to a <code>null</code> * value in the resulting array. */ public <T extends RawEntity<K>, K> T[] get(final Class<T> type, K... keys) throws SQLException { EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final String primaryKeyField = entityInfo.getPrimaryKey().getName(); return get(type, findByPrimaryKey(type, primaryKeyField), keys); }
/** * Counts all entities of the specified type matching the given {@link Query} instance. The SQL runs as a * <code>SELECT COUNT(*)</code> to ensure maximum performance. * * @param type The type of the entities which should be counted. * @param query The {@link Query} instance used to determine the result set which will be counted. * @return The number of entities of the given type which match the specified query. */ public <K> int count(Class<? extends RawEntity<K>> type, Query query) throws SQLException { EntityInfo entityInfo = resolveEntityInfo(type); Connection connection = null; PreparedStatement stmt = null; ResultSet res = null; try { connection = provider.getConnection(); final String sql = query.toSQL(entityInfo, provider, getTableNameConverter(), true); stmt = provider.preparedStatement(connection, sql); provider.setQueryStatementProperties(stmt, query); query.setParameters(this, stmt); res = stmt.executeQuery(); return res.next() ? res.getInt(1) : -1; } finally { closeQuietly(res); closeQuietly(stmt); closeQuietly(connection); } }
/** * <p>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.</p> * * <p>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 <code>null</code> * is returned.</p> * * @param type The type of the entities to retrieve. * @param 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 * {@link RawEntity} inheritence (if inheriting from {@link Entity}, this is <code>Integer</code> * or <code>int</code>). Thus, the <code>keys</code> array is type-checked at compile * time. * @return An array of entities of the given type corresponding with the specified * primary keys. Any entities which are non-existent will correspond to a <code>null</code> * value in the resulting array. */ public <T extends RawEntity<K>, K> T[] get(final Class<T> type, K... keys) throws SQLException { EntityInfo<T, K> entityInfo = resolveEntityInfo(type); final String primaryKeyField = entityInfo.getPrimaryKey().getName(); return get(type, findByPrimaryKey(type, primaryKeyField), keys); }
} else if (RawEntity.class.isAssignableFrom(type)) { EntityInfo<? extends RawEntity, Object> remoteEntityInfo = manager.resolveEntityInfo((Class<? extends RawEntity>) type); if (instanceOf(value, remoteEntityInfo.getPrimaryKey().getJavaType())) { value = manager.peer(remoteEntityInfo, value);
private RawEntity fetchOneToOne(final Method method, final OneToOne annotation) throws SQLException, NoSuchMethodException { final Class remoteType = method.getReturnType(); final EntityInfo entityInfo = manager.resolveEntityInfo(remoteType); final String remotePrimaryKeyFieldName = Common.getPrimaryKeyField(remoteType, getFieldNameConverter()); final String whereClause = Common.where(annotation, getFieldNameConverter());
} else if (RawEntity.class.isAssignableFrom(type)) { EntityInfo<? extends RawEntity, Object> remoteEntityInfo = manager.resolveEntityInfo((Class<? extends RawEntity>) type); if (instanceOf(value, remoteEntityInfo.getPrimaryKey().getJavaType())) { value = manager.peer(remoteEntityInfo, value);
EntityInfo<T, K> entityInfo = resolveEntityInfo(type);
EntityInfo<T, K> entityInfo = resolveEntityInfo(type);