Search in sources :

Example 1 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method testAddManyRelation.

private void testAddManyRelation(boolean onSave) throws InstantiationException, IllegalAccessException {
    ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
    RelatedEntity related1 = new RelatedEntity();
    related1.setId(1L);
    related1.setStringValue("related1");
    relatedRepo.create(related1);
    RelatedEntity related2 = new RelatedEntity();
    related2.setId(2L);
    related2.setStringValue("related2");
    relatedRepo.create(related2);
    TestEntity test = new TestEntity();
    test.setId(3L);
    test.setStringValue("test");
    if (onSave) {
        test.setManyRelatedValues(Arrays.asList(related1, related2));
    }
    testRepo.create(test);
    // query relation
    RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
    if (!onSave) {
        relRepo.addRelations(test, Arrays.asList(1L, 2L), TestEntity.ATTR_manyRelatedValues);
    }
    List<RelatedEntity> related = relRepo.findManyTargets(test.getId(), TestEntity.ATTR_manyRelatedValues, new QuerySpec(RelatedEntity.class));
    Assert.assertEquals(2, related.size());
    // query relation in opposite direction
    RelationshipRepositoryV2<RelatedEntity, Serializable, TestEntity, Serializable> backRelRepo = client.getQuerySpecRepository(RelatedEntity.class, TestEntity.class);
    test = backRelRepo.findOneTarget(2L, RelatedEntity.ATTR_testEntity, new QuerySpec(TestEntity.class));
    Assert.assertNotNull(test);
    Assert.assertEquals(3L, test.getId().longValue());
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) Serializable(java.io.Serializable) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 2 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method addTestWithManyRelations.

private TestEntity addTestWithManyRelations() {
    ResourceRepositoryV2<OtherRelatedEntity, Long> otherRepo = client.getQuerySpecRepository(OtherRelatedEntity.class);
    ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
    RelationshipRepositoryV2<TestEntity, Long, RelatedEntity, Long> relRepo = client.getQuerySpecRepository(TestEntity.class, RelatedEntity.class);
    RelationshipRepositoryV2<RelatedEntity, Long, OtherRelatedEntity, Long> otherRelRepo = client.getQuerySpecRepository(RelatedEntity.class, OtherRelatedEntity.class);
    TestEntity test = new TestEntity();
    test.setId(2L);
    test.setStringValue("test");
    testRepo.create(test);
    RelatedEntity related1 = new RelatedEntity();
    related1.setId(101L);
    related1.setStringValue("related1");
    relatedRepo.create(related1);
    RelatedEntity related2 = new RelatedEntity();
    related2.setId(102L);
    related2.setStringValue("related2");
    relatedRepo.create(related2);
    RelatedEntity related3 = new RelatedEntity();
    related3.setId(103L);
    related3.setStringValue("related3");
    relatedRepo.create(related3);
    OtherRelatedEntity other1 = new OtherRelatedEntity();
    other1.setId(101L);
    other1.setStringValue("related1");
    otherRepo.create(other1);
    OtherRelatedEntity other2 = new OtherRelatedEntity();
    other2.setId(102L);
    other2.setStringValue("related2");
    otherRepo.create(other2);
    OtherRelatedEntity other3 = new OtherRelatedEntity();
    other3.setId(103L);
    other3.setStringValue("related3");
    otherRepo.create(other3);
    List<Long> relatedIds = Arrays.asList(related1.getId(), related2.getId(), related3.getId());
    relRepo.addRelations(test, relatedIds, TestEntity.ATTR_manyRelatedValues);
    otherRelRepo.setRelation(related1, other1.getId(), RelatedEntity.ATTR_otherEntity);
    otherRelRepo.setRelation(related2, other2.getId(), RelatedEntity.ATTR_otherEntity);
    otherRelRepo.setRelation(related3, other3.getId(), RelatedEntity.ATTR_otherEntity);
    return test;
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity)

Example 3 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method testIncludeNested.

@Test
public void testIncludeNested() throws InstantiationException, IllegalAccessException {
    addTestWithManyRelations();
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues, RelatedEntity.ATTR_otherEntity));
    List<TestEntity> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestEntity testEntity = list.get(0);
    List<RelatedEntity> manyRelatedValues = testEntity.getManyRelatedValues();
    Assert.assertNotNull(manyRelatedValues);
    Assert.assertEquals(3, manyRelatedValues.size());
    for (RelatedEntity relatedEntity : manyRelatedValues) {
        Assert.assertNotNull(relatedEntity.getOtherEntity());
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Example 4 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method addTestWithOneRelation.

private TestEntity addTestWithOneRelation() {
    ResourceRepositoryV2<RelatedEntity, Long> relatedRepo = client.getQuerySpecRepository(RelatedEntity.class);
    RelatedEntity related = new RelatedEntity();
    related.setId(1L);
    related.setStringValue("project");
    relatedRepo.create(related);
    TestEntity test = new TestEntity();
    test.setId(2L);
    test.setStringValue("test");
    test.setOneRelatedValue(related);
    testRepo.create(test);
    return test;
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity)

Example 5 with RelatedEntity

use of io.crnk.jpa.model.RelatedEntity in project crnk-framework by crnk-project.

the class JpaQuerySpecEndToEndTest method testIncludeAndFilterManyRelations.

@Test
public void testIncludeAndFilterManyRelations() throws InstantiationException, IllegalAccessException {
    addTestWithManyRelations();
    QuerySpec querySpec = new QuerySpec(TestEntity.class);
    querySpec.includeRelation(Arrays.asList(TestEntity.ATTR_manyRelatedValues));
    QuerySpec relatedSpec = querySpec.getOrCreateQuerySpec(RelatedEntity.class);
    relatedSpec.addFilter(new FilterSpec(Arrays.asList(RelatedEntity.ATTR_id), FilterOperator.LT, 103L));
    List<TestEntity> list = testRepo.findAll(querySpec);
    Assert.assertEquals(1, list.size());
    TestEntity testEntity = list.get(0);
    List<RelatedEntity> manyRelatedValues = testEntity.getManyRelatedValues();
    Assert.assertNotNull(manyRelatedValues);
    Assert.assertEquals(2, manyRelatedValues.size());
    for (RelatedEntity manyRelatedValue : manyRelatedValues) {
        Assert.assertTrue(manyRelatedValue.getId() == 101L || manyRelatedValue.getId() == 102L);
    }
}
Also used : TestEntity(io.crnk.jpa.model.TestEntity) RelatedEntity(io.crnk.jpa.model.RelatedEntity) OtherRelatedEntity(io.crnk.jpa.model.OtherRelatedEntity) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) AbstractJpaJerseyTest(io.crnk.jpa.AbstractJpaJerseyTest) Test(org.junit.Test)

Aggregations

RelatedEntity (io.crnk.jpa.model.RelatedEntity)35 Test (org.junit.Test)27 TestEntity (io.crnk.jpa.model.TestEntity)24 QuerySpec (io.crnk.core.queryspec.QuerySpec)21 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)13 OtherRelatedEntity (io.crnk.jpa.model.OtherRelatedEntity)13 AbstractJpaTest (io.crnk.jpa.query.AbstractJpaTest)11 Serializable (java.io.Serializable)6 FilterSpec (io.crnk.core.queryspec.FilterSpec)3 JsonLinksInformation (io.crnk.client.response.JsonLinksInformation)2 JsonMetaInformation (io.crnk.client.response.JsonMetaInformation)2 PagedMetaInformation (io.crnk.core.resource.meta.PagedMetaInformation)2 CollectionAttributesTestEntity (io.crnk.jpa.model.CollectionAttributesTestEntity)2 UuidTestEntity (io.crnk.jpa.model.UuidTestEntity)2 ObjectProxy (io.crnk.client.internal.proxy.ObjectProxy)1 ResourceField (io.crnk.core.engine.information.resource.ResourceField)1 OneToOneTestEntity (io.crnk.jpa.model.OneToOneTestEntity)1 TestSubclassWithSuperclassPk (io.crnk.jpa.model.TestSubclassWithSuperclassPk)1 RelatedDTO (io.crnk.jpa.model.dto.RelatedDTO)1 QueryParams (io.crnk.legacy.queryParams.QueryParams)1