Skip to content

Commit

Permalink
DELTASPIKE-1160 use return type of query method in native queries to …
Browse files Browse the repository at this point in the history
…build correct entity type
  • Loading branch information
mmain\heda151 authored and mmain\heda151 committed Jun 2, 2016
1 parent 787b385 commit 2b06c14
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
Expand Up @@ -80,10 +80,4 @@ QueryHint[] hints() default {
*/
SingleResultType singleResult() default SingleResultType.JPA;

/**
* For native queries only, whether or not this query returns the defined entity class or not.
* Due to type erasure from generics, we don't have runtime information about the return collection
*/
boolean returnsEntity() default true;

}
Expand Up @@ -27,6 +27,8 @@

import javax.persistence.EntityManager;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import static org.apache.deltaspike.core.util.StringUtils.isNotEmpty;

Expand Down Expand Up @@ -70,14 +72,7 @@ private javax.persistence.Query createJpaQuery(Query query, CdiQueryInvocationCo
else if (query.isNative())
{
String jpqlQuery = context.applyQueryStringPostProcessors(query.value());
if (query.returnsEntity())
{
result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, context.getEntityClass()));
}
else
{
result = params.applyTo(entityManager.createNativeQuery(jpqlQuery));
}
result = params.applyTo(entityManager.createNativeQuery(jpqlQuery, getMethodResultEntityClass(context.getMethod())));
}
else
{
Expand All @@ -88,4 +83,12 @@ else if (query.isNative())
return context.applyRestrictions(result);
}

private Class<?> getMethodResultEntityClass(Method m){
Class<?> rt = m.getReturnType();
if(rt == List.class){
ParameterizedType pt = (ParameterizedType) m.getGenericReturnType();
return (Class<?>) pt.getActualTypeArguments()[0];
}
return rt;
}
}
Expand Up @@ -488,4 +488,26 @@ public void should_query_names()
assertEquals(name, names.get(0));
}

@Test
public void should_query_by_name()
{
String name = "should_return_entity_primary_key";
Simple simple = testData.createSimple(name);

Simple byName = stringIdRepo.findByName(name);

assertEquals(simple, byName);
}

@Test
public void should_query_list_by_name()
{
String name = "should_return_entity_primary_key";
Simple simple = testData.createSimple(name);

List<Simple> byName = stringIdRepo.findByName2(name);

assertEquals(byName.size(), 1);
assertEquals(simple, byName.get(0));
}
}
Expand Up @@ -36,6 +36,6 @@ public interface SimpleIntermediateRepository extends EntityRepository<Simple, L
})
Simple findBy(Long id);

@Query(value = "select name from simple_table", isNative = true, returnsEntity = false)
@Query(value = "select name from simple_table", isNative = true)
List<String> findAllNames();
}
Expand Up @@ -19,10 +19,20 @@
package org.apache.deltaspike.data.test.service;

import org.apache.deltaspike.data.api.EntityRepository;
import org.apache.deltaspike.data.api.Query;
import org.apache.deltaspike.data.api.Repository;
import org.apache.deltaspike.data.test.domain.Simple;
import org.apache.deltaspike.data.test.domain.SimpleStringId;

import javax.persistence.QueryHint;
import java.util.List;

@Repository
public interface SimpleStringIdRepository extends EntityRepository<SimpleStringId, String>
{
@Query("SELECT s FROM Simple s WHERE s.name = ?1")
Simple findByName(String name);

@Query("SELECT s FROM Simple s WHERE s.name = ?1")
List<Simple> findByName2(String name);
}

0 comments on commit 2b06c14

Please sign in to comment.