Results 1 to 4 of 4

Thread: Spring Data Neo4j Incoming Relationships on Collections

  1. #1
    Join Date
    Sep 2012
    Posts
    2

    Default Spring Data Neo4j Incoming Relationships on Collections

    I've run into a situation that I don't know if it is a bug or not, but things are not working the way I think they should. I've got a test project with test case attached to this post to describe what I'm running into.

    Class A (Line) has a M-1 relation with Class B (Brand). The relationship is managed through Class A with a setter set the one instance it has.

    In Class B, I have a class variable a Set of Class A labeled as a relationship type of Incoming.

    The test works fine for the first item of Class A. I can retrieve the instance of Class B and it has the instance of Class A as a member of its Set.

    When I create another instance of Class A referring back to Class B, and then re retrieve Class B, it still only has one instance of Class A. I'm expecting both instances to be there.

    I've isolated the situation I'm talking about to the attached project with a junit to show what I'm running into. Thanks in advance.
    Attached Files Attached Files

  2. #2

    Default

    Quote Originally Posted by bhiles View Post
    I've run into a situation that I don't know if it is a bug or not, but things are not working the way I think they should. I've got a test project with test case attached to this post to describe what I'm running into.

    Class A (Line) has a M-1 relation with Class B (Brand). The relationship is managed through Class A with a setter set the one instance it has.

    In Class B, I have a class variable a Set of Class A labeled as a relationship type of Incoming.

    The test works fine for the first item of Class A. I can retrieve the instance of Class B and it has the instance of Class A as a member of its Set.

    When I create another instance of Class A referring back to Class B, and then re retrieve Class B, it still only has one instance of Class A. I'm expecting both instances to be there.

    I've isolated the situation I'm talking about to the attached project with a junit to show what I'm running into. Thanks in advance.
    Your problem is in the equals method of the Line class, not covering the GraphId field.
    Because of this, when the brand is retrieved and the line collection is built, since each line has the name field set to null (lazyness: all fields are null except for the graph id), only one line is added to the brand line set. Here comes your problem.
    Change the hashCode/equals method in the Line class as follows (include check over the id field) and your problem will vanish...

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((brand == null) ? 0 : brand.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (getClass() != obj.getClass())
    return false;
    Line other = (Line) obj;
    if (brand == null) {
    if (other.brand != null)
    return false;
    } else if (!brand.equals(other.brand))
    return false;
    if (id == null) {
    if (other.id != null)
    return false;
    } else if (!id.equals(other.id))
    return false;
    if (name == null) {
    if (other.name != null)
    return false;
    } else if (!name.equals(other.name))
    return false;
    return true;
    }
    Last edited by DavidKundalini; Oct 1st, 2012 at 04:52 AM.

  3. #3
    Join Date
    May 2012
    Posts
    107

    Default

    David,

    Thanks for finding this!

    bhiles,

    Further to what David said, just want to point to this manual entry I wrote, for completeness: http://static.springsource.org/sprin...e/html/#d5e807

    There has been enough chat on this list about equality that it is worth understanding it thoroughly.

    Regards,

    Lasse

  4. #4
    Join Date
    Sep 2012
    Posts
    2

    Default

    Thanks. That did it. Appreciate the link to the manual. Data frameworks and equality are always tricky. Now I know what I need to do for this one!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •