neo4j / neo4j-ogm

Java Object-Graph Mapping Library for Neo4j
https://neo4j.com/docs/ogm-manual/
Apache License 2.0
329 stars 164 forks source link

Inheritance in SCHEMA_LOAD_STRATEGY does not work, if it is not the same session #670

Closed oleksandrzakharov closed 4 years ago

oleksandrzakharov commented 4 years ago

LoadStrategy.SCHEMA_LOAD_STRATEGY

@NodeEntity
public class Person {
  private Long id;
  private String name;
}
@NodeEntity
public class Teacher extends Person {
  private List<Course> cources;
}
@NodeEntity
public class Course {
 private Long id;
 private Long title;
}
Neo4jSession session;
Teacher teacher = new Teacher();
teacher.setName("Name");
Course course = new Course();
course.setTitle("Course");
teacher.getCourses().add(course);
session.save(teacher);

It was saved, teacher(with two labels: Teacher, Person), course(Course), (:COURSE). In database evething is correct

id(teacher) ==> 1

Expected Behavior

After save in another session I can read Neo4jSession session; session.load(Person.class, 1) ==> Teacher with the course. Type of the java class is correct. The cypher query should have the relationship (:COURSE) in the cypher query with course node

Current Behavior

Neo4jSession session; session.load(Person.class, 1) ==> Teacher without the course (only values from Person). Type of the java class is correct. In the cypher query, the relationship does not exist

Possible Solution

With LoadStrategy.PATH_LOAD_STRATEGY (MATCH p=(n)-[*0..1]-(m) RETURN p) it works, but it is not the default behaviour, not compliant with load based on a schema. When loading for example LoadOneDelegate.class instead of public <T, ID extends Serializable> T load(Class<T> type, ID id, int depth) {

    ClassInfo classInfo = session.metaData().classInfo(type.getName());`

load labels from the entity MATCH (n) where id(n) = {id} RETURN labels(n) and build tree of inherentence and after that build a query => exact call => mapping

Context

Using spring repositories I can do like: public interface PersionRepository extends Neo4JRepository<Person, Long> { } but if I have child-parent it is not possible to extract values from a child using parent type. For example in JPA it works

Your Environment

meistermeier commented 4 years ago

We have to clarify this internally. I know that this came up about a year ago but need to find the discussion again.

michael-simons commented 4 years ago

Thanks, @oleksandrzakharov for your patience here.

This is fixed in master and will be available from at least OGM 3.2.4 onwards, I'll have a look now whether we back port it to 3.1.x, too.

michael-simons commented 4 years ago

Will be in 3.1.16 as well.

oleksandrzakharov commented 4 years ago

Thank you very much!!!