kctang
Apr 18th, 2012, 07:58 PM
Exploring Spring Data JPA, where I am trying out the pagable support. Things looks good for methods without Pageable parameters.
When I introduced Pageable parameter in my repository method, I can't seem to call them method more than once in unit tests. Details below.
My repository looks like:
@Transactional
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
public Page<Item> findByTitleLike(String title, Pageable page);
and my unit test looks like:
@Test
public void testTitleLike() {
// create 5 products and 5 promotions
for(int c=0;c<5;c++) {
// makeItem() just creates a new item object and return
repository.save(makeItem("Product #" + c, TYPE_PRODUCT));
repository.save(makeItem("Promotion #" + c, TYPE_PROMOTION));
}
// create 5 promotions
for(int c=5; c<10; c++) {
repository.save(makeItem("Promotion #" + c, TYPE_PROMOTION));
}
// assertion A: check that i have 5 items where title like "%Product%"
assertEquals(5, repository.findByTitleLike("%Product%", new PageRequest(0, 100)).getContent().size());
// assertion B: check that i have 10 items where title like "%Promotion%"
assertEquals(10, repository.findByTitleLike("%Promotion%", new PageRequest(0, 100)).getContent().size());
}
The code looks simple enough, and it will pass the test ONLY IF i comment out either assertion A or B above. When I run the test case with both assertions, it will fail with:
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java: 350)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator$ParameterExpressionProvider.next(JpaQu eryCreator.java:330)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.toPredicate(JpaQueryCreator.java:205)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.create(JpaQueryCreator.java:103)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.create(JpaQueryCreator.java:50)
at org.springframework.data.repository.query.parser.A bstractQueryCreator.createCriteria(AbstractQueryCr eator.java:107)
at org.springframework.data.repository.query.parser.A bstractQueryCreator.createQuery(AbstractQueryCreat or.java:86)
at org.springframework.data.jpa.repository.query.Part TreeJpaQuery$QueryPreparer.createQuery(PartTreeJpa Query.java:145)
at org.springframework.data.jpa.repository.query.Part TreeJpaQuery.createCountQuery(PartTreeJpaQuery.jav a:91)
at org.springframework.data.jpa.repository.query.JpaQ ueryExecution$PagedExecution.doExecute(JpaQueryExe cution.java:106)
at org.springframework.data.jpa.repository.query.JpaQ ueryExecution.execute(JpaQueryExecution.java:55)
at org.springframework.data.jpa.repository.query.Abst ractJpaQuery.doExecute(AbstractJpaQuery.java:100)
at org.springframework.data.jpa.repository.query.Abst ractJpaQuery.execute(AbstractJpaQuery.java:89)
at org.springframework.data.repository.core.support.R epositoryFactorySupport$QueryExecutorMethodInterce ptor.invoke(RepositoryFactorySupport.java:343)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy33.findByTitleLike(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:183)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :150)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy35.findByTitleLike(Unknown Source)
at xxx.model.ItemTest.testTitleLike(ItemTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
...
Am I doing something wrong or is this a bug? Help is much appreciated.
When I introduced Pageable parameter in my repository method, I can't seem to call them method more than once in unit tests. Details below.
My repository looks like:
@Transactional
@Repository
public interface ItemRepository extends JpaRepository<Item, Long> {
public Page<Item> findByTitleLike(String title, Pageable page);
and my unit test looks like:
@Test
public void testTitleLike() {
// create 5 products and 5 promotions
for(int c=0;c<5;c++) {
// makeItem() just creates a new item object and return
repository.save(makeItem("Product #" + c, TYPE_PRODUCT));
repository.save(makeItem("Promotion #" + c, TYPE_PROMOTION));
}
// create 5 promotions
for(int c=5; c<10; c++) {
repository.save(makeItem("Promotion #" + c, TYPE_PROMOTION));
}
// assertion A: check that i have 5 items where title like "%Product%"
assertEquals(5, repository.findByTitleLike("%Product%", new PageRequest(0, 100)).getContent().size());
// assertion B: check that i have 10 items where title like "%Promotion%"
assertEquals(10, repository.findByTitleLike("%Promotion%", new PageRequest(0, 100)).getContent().size());
}
The code looks simple enough, and it will pass the test ONLY IF i comment out either assertion A or B above. When I run the test case with both assertions, it will fail with:
java.util.NoSuchElementException
at java.util.AbstractList$Itr.next(AbstractList.java: 350)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator$ParameterExpressionProvider.next(JpaQu eryCreator.java:330)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.toPredicate(JpaQueryCreator.java:205)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.create(JpaQueryCreator.java:103)
at org.springframework.data.jpa.repository.query.JpaQ ueryCreator.create(JpaQueryCreator.java:50)
at org.springframework.data.repository.query.parser.A bstractQueryCreator.createCriteria(AbstractQueryCr eator.java:107)
at org.springframework.data.repository.query.parser.A bstractQueryCreator.createQuery(AbstractQueryCreat or.java:86)
at org.springframework.data.jpa.repository.query.Part TreeJpaQuery$QueryPreparer.createQuery(PartTreeJpa Query.java:145)
at org.springframework.data.jpa.repository.query.Part TreeJpaQuery.createCountQuery(PartTreeJpaQuery.jav a:91)
at org.springframework.data.jpa.repository.query.JpaQ ueryExecution$PagedExecution.doExecute(JpaQueryExe cution.java:106)
at org.springframework.data.jpa.repository.query.JpaQ ueryExecution.execute(JpaQueryExecution.java:55)
at org.springframework.data.jpa.repository.query.Abst ractJpaQuery.doExecute(AbstractJpaQuery.java:100)
at org.springframework.data.jpa.repository.query.Abst ractJpaQuery.execute(AbstractJpaQuery.java:89)
at org.springframework.data.repository.core.support.R epositoryFactorySupport$QueryExecutorMethodInterce ptor.invoke(RepositoryFactorySupport.java:343)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy33.findByTitleLike(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:183)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :150)
at org.springframework.dao.support.PersistenceExcepti onTranslationInterceptor.invoke(PersistenceExcepti onTranslationInterceptor.java:155)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:110)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :172)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy35.findByTitleLike(Unknown Source)
at xxx.model.ItemTest.testTitleLike(ItemTest.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:45)
...
Am I doing something wrong or is this a bug? Help is much appreciated.