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); } }
public void toStorageType(Object value, Map<String, Object> valuesRef) { Object[] values = null; if (isRelation(getType())) { if (value != null) { @SuppressWarnings("unchecked") AnnotatedRecordMapper<Object> mapper = (AnnotatedRecordMapper<Object>) create(getType()); values = mapper.getIds(value); } } else if (isEmbedded()) { if (value != null) { values = getEmbeddedFields(value); } } else { values = new Object[] {dataTypeConverter.toStorageType(value)}; } if (values != null) { for (int i = 0; i < names.length; i++) { valuesRef.put(names[i], values[i]); } } else { for (String columnName : names) { valuesRef.put(columnName, null); } } }
@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); } }