cancel
Showing results for 
Search instead for 
Did you mean: 

SDN6: ClassCastException when projecting relations (with classes)

gonzalad
Node Link

Hello,

I have the following issue (SDN 6.1.5 and 6.2.0-SNAPSHOT) when executing a query returning a projection

java.lang.ClassCastException:

Cannot cast org.springframework.data.neo4j.integration.movies.shared.Movie
to
org.springframework.data.neo4j.integration.movies.imperative.AdvancedMappingIT$MovieRefProjectionClass

The error happens when using classes (and not interfaces) for the projection and when the projection class has a field pointing to a projection class.

Sample projection class (using the testcase from SDN):

static class MovieWithSequelProjectionClass {
    private String title;
    private MovieWithSequelProjectionClass sequel;

    public MovieWithSequelProjectionClass getSequel() {
        return sequel;
    }

    public void setSequel(MovieWithSequelProjectionClass sequel) {
        this.sequel = sequel;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

Please note that we don't have this issue when using projection interfaces.

Steps to reproduce:

Should I open an issue on github ?

Thanks

1 ACCEPTED SOLUTION

gerrit_meier
Neo4j
Neo4j

For a class-based / DTO projection this is not possible.
The problem is that Spring Data (Neo4j) is only aware of the already mapped information, in this case Movie.
Now it wants to transfer this into the projection class that expects the projection class instead of just Movie and it fails.

This is also mentioned in the documentation.

These DTO types can be used in exactly the same way projection interfaces are used, except that no proxying happens and no nested projections can be applied.

View solution in original post

1 REPLY 1

gerrit_meier
Neo4j
Neo4j

For a class-based / DTO projection this is not possible.
The problem is that Spring Data (Neo4j) is only aware of the already mapped information, in this case Movie.
Now it wants to transfer this into the projection class that expects the projection class instead of just Movie and it fails.

This is also mentioned in the documentation.

These DTO types can be used in exactly the same way projection interfaces are used, except that no proxying happens and no nested projections can be applied.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.