Extract annotations in SDG 2.0, strategy selection

Hi to all patient developers using spring data graph. Since there is less documentation and fairly poor testing coverage, it is sometimes very difficult to understand what the expected behavior of the underlying structure is, and it is assumed that the structure should work. Currently, I have some questions related to the new selection method introduced in SDG 1.1. Unlike SDG 1.1 write \ read through in 2.0, only relationships and a related object annotated with @Fetch annotation eagerly expect others to be received lazily .. and now my first question:

  • Is it possible to configure SDG so that when loading an object and calling a getter by a lazy ratio, it takes place in the same transaction, the requested collection is automatically selected? View retention Context in the transaction area, or perhaps it is planned for the function releases.
  • How can I get a lazy collection right away for @RelatedTo annotation? The fetch () method of Neo4jOperation allows you to retrieve only one object. Is it necessary to iterate over the entire list and select an object for each object? What would be the best way to check if a given object is already selected or not?
  • As a suggestion, I think it would be more intuitive if there would be some kind of lazy loading error instead of getting NPE when working with non-initialized objects. In addition, the behavior is misleading, because when the object is not initialized and all properties of the element are equal to zero, except for the id method, the equals method can provide true for different objects that have not been initialized, which is quite serious problems, for example, to use sets
  • Another problem that I noticed when working with SDG 2.0.0.RC1 is this: when I add a new object to an unprepared collection, it is sometimes added and saved, but sometimes it is not. I wrote a test for this case, and it does not work in a deterministic way. Sometimes this does not always end in success. Here is a usage example:

    Group groupFromDb = neoTemplate.findOne(group.getId(), Group.class); assertNotNull(groupFromDb); assertEquals("Number of members must be equals to 1", 1, groupFromDb.getMembers().size()); User secondMember = UserMappingTest.createUser("secondMember"); groupFromDb.addMember(secondMember); neoTemplate.save(groupFromDb); Group groupAfterChange = neoTemplate.findOne(groupFromDb.getId(), Group.class); assertNotNull(groupAfterChange); assertEquals("Number of members must be equals to saved entity", groupFromDb.getMembers().size(), groupAfterChange.getMembers().size()); assertEquals("Number of members must be equals to 2", 2, groupAfterChange.getMembers().size()); 

This test sometimes fails with the last statement, which means that sometimes a member is added to the set, and sometimes not. I assume that the problem lies somewhere in the ManagedFieldAccessorSet, but this is hard to say since it is not deterministic. I run the test with mvn2 and mvn3 with java 1.6_22 and 1.6_27, and I always get the same result: sometimes it is normal when the test fails. The implementation of User equals is as follows:

 @Override public boolean equals(final Object other) { if ( !(other instanceof User) ) { return false; } User castOther = (User) other; if(castOther.getId() == this.getId()) { return true; } return new EqualsBuilder().append(username, castOther.username).isEquals(); } 

- I also find it problematic that for objects annotated using @Fetch, a Java HashSet is used, which is serialized, when used for lazy loaded fields, a ManagedFieldAccessorSet is used, which is not serializable and does not cause a serializable exception.

Any help or advice is appreciated. Thanks in advance!

+6
source share
2 answers

A simple mapping approach was added only in Spring Data Neo4j 2.0, so it is not as mature as the AspectJ advanced mapping. We are currently working on documenting it more widely.

Recently, the option of lazy loading has also been added. Therefore, your feedback is very welcome.

Currently, the SDN does not use a proxy approach for lazily loaded objects. Thus, automatic “access sampling” is not yet supported. Therefore, an exception also does not occur when accessing unloaded fields, and there is no means to "detect" if the object has not been fully loaded.

There is a template.fetch() operation in the current snapshot to fully load lazy downloadable objects and collections.

We will look at the problem of HashSet and ManagedSet, it’s right that this is not a good solution.

For a test case. is it getId () returning a Long object or a Long primitive? It would be wise to use getId().equals(castOther.getId()) here, since referential equality is not guaranteed for Number objects.

+3
source

I put together a quick code example showing how to use the fetch () method. Michael describes:

http://springinpractice.com/2011/12/28/initializing-lazy-loaded-collections-with-spring-data-neo4j/

+4
source

Source: https://habr.com/ru/post/902059/


All Articles