cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Phase 2: migrate recent content

Pageable Projections Neo4jRepository

mklopp
Node

Hello,

I am trying to get Projections to work, as I want to load only some data from neo4j and not all nested relationships and nodes. I got everything to work so far except "Pages".

Consider following example:

Entity:

@Node(primaryLabel = "Entity")
public class Entity {
    @Property(name = some_property)
    private String someProperty;

    @Relationship(type = "some_rel", direction = Relationship.Direction.OUTGOING)
    private List<Entity2> otherEntities;

Projection:

@Value //lombok
public class Entity {
    String someProperty;

Repository:

public interface EntityRepository extends Neo4jRepository<Entity, Long> {
    Optional<MyProjection> findBySomeProperty(String someProperty);
    Page<MyProjection> findAllProjectedBy(Pageable pageable);
}

Now calling findBySomeProperty("value") works like a charm and the related entities do not have to be loaded from the neo4j database (at least I hope so, as this is the goal of projections)

But calling findAllProjectedBy(PageRequest.of(0,20) throws an unexpected error:

java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class org.springframework.data.neo4j.repository.query.EntityInstanceWithSource (java.util.ArrayList is in module java.base of loader 'bootstrap'; org.springframework.data.neo4j.repository.query.EntityInstanceWithSource is in unnamed module of loader 'app')
	at org.springframework.data.neo4j.repository.query.AbstractNeo4jQuery.lambda$execute$2(AbstractNeo4jQuery.java:94) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
	at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:236) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.lambda$and$0(ResultProcessor.java:222) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.query.ResultProcessor$ChainingConverter.convert(ResultProcessor.java:236) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:170) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.neo4j.repository.query.AbstractNeo4jQuery.execute(AbstractNeo4jQuery.java:97) ~[spring-data-neo4j-6.0.3.jar:6.0.3]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:152) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80) ~[spring-data-commons-2.4.3.jar:2.4.3]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-5.3.3.jar:5.3.3]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388) ~[spring-tx-5.3.3.jar:5.3.3]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-5.3.3.jar:5.3.3]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-5.3.3.jar:5.3.3]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.3.jar:5.3.3]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) ~[spring-aop-5.3.3.jar:5.3.3]
	at com.sun.proxy.$Proxy128.findAllProjectedBy(Unknown Source) ~[na:na]

Maybe someone can tell me what I did wrong or how it is supposed to work.

Thank you.

4 REPLIES 4

mklopp
Node

Still trying to solve this issue. Any help?

Sorry for the delay.
I tried to reproduce your problem with Spring Boot 2.4.4 (Spring Data Neo4j 6.0.6 / current) and 2.4.3 (6.0.5) but could not find a problem. Maybe you can have a look at neo4j-issues-examples/c-35084 at master · meistermeier/neo4j-issues-examples · GitHub and tell me if there is something different in your code. I tried to keep it as close as possible to the given example.

mklopp
Node

Thank you a lot!
I was using Spring Boot 2.4.2.
I downloaded your example and it worked. When I changed the Spring Boot version in your example back to 2.4.2, it threw the same error that I posted above.
I never thought changing from 2.4.2 to 2.4.4 would do the trick.

And now that you are saying this, I remember that there was something around pageables and projections that got fixed.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

All the sessions of the conference are now available online