public static <C> List<C> fetch(Class<C> cls, DatabaseObject filteredBy) { Database database = filteredBy.getDatabase(); @SuppressWarnings("unchecked") AnnotatedRecordMapper<DatabaseObject> parentMapper = (AnnotatedRecordMapper<DatabaseObject>) create(filteredBy.getClass()); AnnotatedRecordMapper<?> mapper = create(cls); List<String> relationColumnNames = mapper.getRelationColumnNames(filteredBy.getClass()); List<String> idColumnNames = parentMapper.getIdColumnNames(); Map<String, Object> values = parentMapper.extractIds(filteredBy); String relationExpression = ""; Object[] parameters = new Object[relationColumnNames.size()]; for (int i = 0; i < relationColumnNames.size(); i++) { relationExpression += relationColumnNames.get(i) + "=?"; parameters[i] = values.get(idColumnNames.get(i)); if (parameters[i] == null) { return new ArrayList<>(); } } try (Transaction transaction = database.beginReadOnlyTransaction()) { transaction.associate(filteredBy); return transaction.select(cls, relationExpression, parameters); } }
@SuppressWarnings("unchecked") public static <T extends DatabaseObject> T fetch(T instance) { Database database = instance.getDatabase(); Object[] ids; synchronized (STUBS) { ids = STUBS.get(instance); } if (database == null || ids == null) { return instance; } AnnotatedRecordMapper<? extends Object> mapper = create(instance.getClass()); String whereClause = ""; for (String fieldName : mapper.getIdColumnNames()) { if (!whereClause.isEmpty()) { whereClause += " AND "; } whereClause += fieldName + "=?"; } try (Transaction transaction = database.beginReadOnlyTransaction()) { return (T) transaction.selectUnique(instance.getClass(), whereClause, ids); } }