Archives for Spring Data

Current gotchas with Spring Data Neo4j. Update 1.0

For the past 6 months I have been working on a personal project of mine to learn some of the amazing things that the SpringSource guys are doing with the project Spring Data. If you haven’t checked it our, or using it for your data access code, well, I have some choice words and they aren’t pretty. It is a no brainer, especially if you already are using the Spring Framework. And if you aren’t using the Spring Framework for any reasons, then Spring Data would be the biggest reason to use Spring. But you should be using Spring anyway.

OK, to the point of this blog post.

First to post some cool tricks you can do, or things that aren’t exactly tricks but hard to figure out how to do just from the standard doumentation.

And in using Spring Data Neo4J, I found some gotchas, not really bad, but things that you might not infer from the great documentation. These are very subtle and unless you have experienced them, it is easy to fall prey to these simple mistakes. So I create this blog post to list those gotchas, and to try to keep this up to date.

As of version 1.0 of this blog post, I am using Spring Data Neo4J 2.1.0.RC1. But check here for the list of versions used with which version of this post

Versions:

Post Version SDN Version
1.0 SDN 2.1.0.RC1

 

Gotchas

Indexes

    • You can only have one unique index per node/relationship
    • You cannot combine unique=true with IndexType.FULLTEXT

Queries with @Query

    • If you are using derived queries or creating queries with @Query on your repository interface method, then use the nodeId as the parameter to the method and the placement in the Cypher Query, rather than passing in your domain object as the parameter to the method.For example, use
      @Query("start user=node({0}) " +
      "match (user)<-[:FRIEND]->(friends) " +
      "return friends " +
       "order by friends.lastName asc")
       public Page<User> findFriends(Long userId, Pageable page);

      instead of
      @Query("start user=node({0}) " +
      "match (user)<-[:FRIEND]->(friends) " +
      "return friends " +
      "order by friends.lastName asc")
      public Page<User> findFriends(User user, Pageable page);

Coding Domain objects

  • Make your domain objects implement Serializable
  • In the docs it has you write your equals/hashcode methods use the nodeId. I am following htat, but it causes an issue of relationships where you have a parent child relationship between two domain objects and the parent has a Set of child domain objects, and since the child domain objects uses the nodeId in equals/hascode, it causes problems with HashSet. If the domain object is new, there is no nodeId set. Also I have found in a couple cases that set.contains(a) can return true and then set.remove(a) returns false, even though object “a” is the same object.

Well that is a start.

Mark