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());
}
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;
}
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());
}
}
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;
}
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);
}
}
Aggregations