Search in sources :

Example 1 with CompositeKey

use of io.requery.proxy.CompositeKey in project requery by requery.

the class FunctionalTest method testFindByCompositeKey.

@Test
public void testFindByCompositeKey() {
    Group group = new Group();
    group.setName("group");
    group.setType(GroupType.PRIVATE);
    Person person = randomPerson();
    person.getGroups().add(group);
    data.insert(person);
    assertTrue(person.getId() > 0);
    // create the composite key
    Map<Attribute<Group_Person, Integer>, Integer> map = new LinkedHashMap<>();
    map.put(Group_Person.GROUPS_ID, group.getId());
    map.put(Group_Person.PERSON_ID, person.getId());
    CompositeKey<Group_Person> compositeKey = new CompositeKey<>(map);
    Group_Person joined = data.findByKey(Group_Person.class, compositeKey);
    assertNotNull(joined);
}
Also used : Group(io.requery.test.model.Group) Group_Person(io.requery.test.model.Group_Person) CompositeKey(io.requery.proxy.CompositeKey) Attribute(io.requery.meta.Attribute) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with CompositeKey

use of io.requery.proxy.CompositeKey in project requery by requery.

the class EntityDataStore method findByKey.

@Override
public <E extends T, K> E findByKey(Class<E> type, K key) {
    Type<E> entityType = entityModel.typeOf(type);
    if (entityType.isCacheable() && entityCache != null) {
        E entity = entityCache.get(type, key);
        if (entity != null) {
            return entity;
        }
    }
    Set<Attribute<E, ?>> keys = entityType.getKeyAttributes();
    if (keys.isEmpty()) {
        throw new MissingKeyException();
    }
    Selection<? extends Result<E>> selection = select(type);
    if (keys.size() == 1) {
        QueryAttribute<E, Object> attribute = Attributes.query(keys.iterator().next());
        selection.where(attribute.equal(key));
    } else {
        if (key instanceof CompositeKey) {
            CompositeKey compositeKey = (CompositeKey) key;
            for (Attribute<E, ?> attribute : keys) {
                QueryAttribute<E, Object> keyAttribute = Attributes.query(attribute);
                Object value = compositeKey.get(keyAttribute);
                selection.where(keyAttribute.equal(value));
            }
        } else {
            throw new IllegalArgumentException("CompositeKey required");
        }
    }
    return selection.get().firstOrNull();
}
Also used : CompositeKey(io.requery.proxy.CompositeKey) UPDATE(io.requery.query.element.QueryType.UPDATE) DELETE(io.requery.query.element.QueryType.DELETE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute)

Aggregations

Attribute (io.requery.meta.Attribute)2 CompositeKey (io.requery.proxy.CompositeKey)2 QueryAttribute (io.requery.meta.QueryAttribute)1 DELETE (io.requery.query.element.QueryType.DELETE)1 UPDATE (io.requery.query.element.QueryType.UPDATE)1 Group (io.requery.test.model.Group)1 Group_Person (io.requery.test.model.Group_Person)1 Person (io.requery.test.model.Person)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1