Skip to content

Commit

Permalink
Fix to-many relationships in JPAModule
Browse files Browse the repository at this point in the history
When you tried to query a to-many relation it didn't produce a proper result.

Reproduction of the issue is found in JpaRelationshipRepositoryTestBase class.

I had to disable the QueryDSL tests because the QueryFactory for QueryDsl still
cannot handle to-many type of queries properly.
  • Loading branch information
Zoltan Reegn committed Sep 22, 2016
1 parent 05274a2 commit 10ea37f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
9 changes: 7 additions & 2 deletions katharsis-jpa/src/main/java/io/katharsis/jpa/JpaModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,13 @@ private Set<Class<?>> getRelatedResource(MetaEntity metaEntity) {
Set<Class<?>> relatedResourceClasses = new HashSet<>();
for (MetaAttribute attr : metaEntity.getAttributes()) {
if (attr.isAssociation()) {
Class<?> relType = attr.getType().getImplementationClass();

Class<?> relType = null;
if(attr.getType().isCollection()) {
relType = attr.getType().getElementType().getImplementationClass();
}
else {
relType = attr.getType().getImplementationClass();
}
// only include relations that are exposed as repositories
if (entityClasses.contains(relType)) {
relatedResourceClasses.add(relType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public AbstractJpaQueryImpl(MetaLookup metaLookup, EntityManager em, Class<?> en

MetaDataObject parentMeta = metaLookup.getMeta(entityClass).asDataObject();
MetaAttribute attrMeta = parentMeta.getAttribute(attrName);
this.meta = attrMeta.getType().asEntity();
if(attrMeta.getType().isCollection()) {
this.meta = attrMeta.getType().asCollection().getElementType().asEntity();
}
else {
this.meta = attrMeta.getType().asEntity();
}
this.clazz = (Class<T>) meta.getImplementationClass();

this.parentEntityClass = entityClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package io.katharsis.jpa.repository;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.hamcrest.core.Is;
import org.hamcrest.core.IsInstanceOf;
import org.hamcrest.core.IsNot;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -12,6 +18,7 @@
import io.katharsis.jpa.model.RelatedEntity;
import io.katharsis.jpa.model.TestEntity;
import io.katharsis.jpa.query.AbstractJpaTest;
import io.katharsis.jpa.query.querydsl.QuerydslQueryFactory;
import io.katharsis.queryspec.FilterOperator;
import io.katharsis.queryspec.FilterSpec;
import io.katharsis.queryspec.QuerySpec;
Expand Down Expand Up @@ -146,4 +153,29 @@ public void testSetRelation() throws InstantiationException, IllegalAccessExcept
related = em.find(RelatedEntity.class, 101L);
Assert.assertNull(related.getTestEntity());
}

@Test
public void testGetManyRelation() {
//TODO fix for QueryDSL
Assume.assumeThat("QueryDSL still breaks on this, so ignore it for now",
queryFactory, IsNot.not(IsInstanceOf.instanceOf(QuerydslQueryFactory.class)));
TestEntity test = em.find(TestEntity.class, 1L);
Assert.assertThat(test.getManyRelatedValues().size(), Is.is(0));

repo.addRelations(test, Arrays.asList(101L,102L), TestEntity.ATTR_manyRelatedValues);
em.flush();
em.clear();
test = em.find(TestEntity.class, 1L);
Assert.assertThat(test.getManyRelatedValues().size(), Is.is(2));

QuerySpec querySpec = new QuerySpec(RelatedEntity.class);
Iterable<RelatedEntity> targets = repo.findManyTargets(1L, TestEntity.ATTR_manyRelatedValues, querySpec);
List<RelatedEntity> res = new ArrayList<>();
for (RelatedEntity relatedEntity : targets) {
res.add(relatedEntity);
}
Assert.assertThat(res.size(), Is.is(2));
Assert.assertThat(res.get(0).getId(), Is.is(101L));
Assert.assertThat(res.get(1).getId(), Is.is(102L));
}
}

0 comments on commit 10ea37f

Please sign in to comment.