spring-projects / spring-data-neo4j

Provide support to increase developer productivity in Java when using Neo4j. Uses familiar Spring concepts such as a template classes for core API usage and lightweight repository style data access.
http://spring.io/projects/spring-data-neo4j
Apache License 2.0
828 stars 620 forks source link

BeanInstantiationException with @DynamicLabels and multiple levels of inheritance #2529

Closed cberes closed 2 years ago

cberes commented 2 years ago

SDN 6.2.4 is unable to instantiate nodes with multiple levels of inheritance and a non-empty @DynamicLabels field.

I have an example repo but here is a summary of the problem.

For example, say you have a super class

@Getter
@Node
public abstract class Animal {
  @DynamicLabels
  private Set<String> labels = new TreeSet<>();
}

A sub class

@Node
public abstract class Feline extends Animal {
}

And a sub class of the sub class

@Node
public class Cat extends Feline {
}

You can query for instances of the Cat sub class only if they do not have a dynamic label

Cat cat1 = new Cat();
animalRepository.save(cat1);

Cat cat2 = new Cat();
cat2.getLabels().add("Orange");
animalRepository.save(cat2);

Cat found1 = (Cat) animalRepository.findById(cat1.getId()).get(); // this is fine
Cat found2 = (Cat) animalRepository.findById(cat2.getId()).get(); // BeanInstantiationException

The second query fails if the Feline sub class is abstract or concrete, though the exception is different.

This example worked correctly in SDN 6.2.1 but it fails with SDN 6.2.4.

meistermeier commented 2 years ago

Thanks for reporting this and providing the nice to follow reproducer. Already the first run seems to uncover that there might be an error in the processing of the labels from the database if a dynamic label is involved.

meistermeier commented 2 years ago

It will be fixed in the upcoming versions. You could give the 6.2.5-SNAPSHOT a try.