Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@Relationship (type = "...") ArrayList<T> does not maintain list sequence. #386

Closed
JAFriedrich opened this issue Jul 14, 2017 · 8 comments
Closed

Comments

@JAFriedrich
Copy link

JAFriedrich commented Jul 14, 2017

Expected Behavior

A Node A has a collection of type ArrayList as a relation e.g.:

	@Relationship(type="sortedListItem", direction = Relationship.OUTGOING)
	private ArrayList<ListItem> sortedListItems; 

1.) Add items to that ArrayList in a specific sequence.
2.) Persist Node A including it's direct neighbours (depth >= 1).
All ListItems are thus persisted and related to Node A.
3.) Restart application.
4.) Load Node A including neighbours (depth >=1).

Node A's ArrayList maintains it's original sequence.

Current Behavior

The ArrayList does not maintain it's original sequence.

The sequence of the ListItems in Node A's ArrayList is now random. Working with a list across multiple sessions is therefore not possible, if the list sequence is of interest - which is presumably typically the case.

Context

Native population of neighbours using the OGM Relationship mechanics would be far more convenient to use (if it was working as expected/described) than for example adding a relationship specific property and manually sorting after loading the collection of neighbours.

Edit:
This is different from #196, where there is not necessarily a relationship between the nodes and no annotated collection used. The collection mentioned is just a container on the client side.

Your Environment

  • OGM Version used: 2.1.1
  • Java Version used: 1.8
  • Neo4J Version used: 1.0.6
@vince-bickers
Copy link

vince-bickers commented Jul 18, 2017

Hi,

Thanks for filing this issue. The OGM doesn't make any guarantees about ordering of results, unless you explicitly order them using a custom query.

There are a couple of reasons for this:

First, for performance reasons the OGM splits up insertion of nodes and edges when persisting your objects. The order in which edges are created is currently undefined.

However, even if it did preserve the insertion order of edges this ordering would not be maintained when the data was read back again. This is because the OGM currently sends a generic query for any object to depth n, which returns a list of paths of length 0..n, e.g.

MATCH (n) WHERE ID(n) = { id } WITH n MATCH p=(n)-[*0..1]-(m) RETURN p

Neo4j does not guarantee that these paths will be returned in any particular order, so the OGM is unable to preserve any initial ordering.

@frant-hartm
Copy link
Contributor

@JAFriedrich for options how to achieve ordering on relationship please see https://neo4j.com/docs/ogm-manual/preview/reference/#_ordering

This is being asked quite a lot. Maybe we should consider providing a way to do this automatically - storing internal order property and sorting by that.

@JAFriedrich
Copy link
Author

JAFriedrich commented Sep 19, 2017

Hi, thank you for your quick and detailed responses.

I'm really looking forward to have this available as native OGM feature.

@f-ruta
Copy link

f-ruta commented Nov 8, 2017

Would be very helpful for us too

@denverserraoCisco
Copy link

We could definitely make use of this feature

@meistermeier
Copy link
Collaborator

If we would add an additional (transparent) sorting property to each relationship saved in an ordered collection, this should work. The sorting will then be programmatically done after loading the results from Neo4j.
There are some more investigation needed on this: Like finding out which collections beside an ArrayList can be supported etc. I will do this in the next days.

@meistermeier meistermeier added this to the 3.1.1 milestone Feb 19, 2018
@meistermeier meistermeier removed this from the 3.1.1 milestone Jul 26, 2018
@meistermeier
Copy link
Collaborator

Status update: This brought more problems with it than expected especially in handling non-rich Relationships (not annotated with @RelationshipEntity). It is problematic to spot changes in the ordering. Therefor this issue did not get into 3.1.1 and needs some more investigation.

@michael-simons
Copy link
Collaborator

We don't have any means to make sure that relationships are retrieved in the same order as the database has written them without the user adding an additional ordering. At the moment, we're not adding a new order attribute to the relationship definition.

As a workaround, make your relationship entity java.lang.Comparable and use a SortedSet as collection.

Thanks for your understanding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants