0

When I try to get different relationship type of collection object, the collection property will retrieve all the same entity type even if the relationship type if not the collection relationship type. Is it a bug ?

The entity Demo contains two fields which reference to User entity : user and users

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.RelationshipEntity;

@NodeEntity
public class Demo extends FormLog implements java.io.Serializable {


    private String name;


    @Relationship(type="MY_USER")
    private User user;

    @Relationship(type="DEMO_USERS")
    private Set<User> users = new HashSet<User>();

    public Demo(){}
    public Demo(String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }



    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
    public Set<User> getUsers() {
        return users;
    }

    public void setUsers(Set<User> users) {
        this.users = users;
    }
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }




}

When I save the demo with 1 "MY_USER" and 2 "DEMO_USERS", it is fine. But when I get find the Demo by group id, the "DEMO_USERS" return 3 users.

@Test
public void test_saveAndFindOne_save2KindsOfUser_NoConfliction(){

    Demo demo = new Demo();

    List<User> user = userService.findUserByNameLike("Hank");

    demo.setUser(user.get(0));

    demo.getUsers().add(user.get(1));
    demo.getUsers().add(user.get(2));

    demo.setName("Set Multiple");


    demo = demoRepository.save(demo);

    System.out.println("Users size =  "+ demo.getUsers().size());
    System.out.println("==========Get Demo from DB ==============");
    Demo db = demoRepository.findOne(demo.getId());
    System.out.println("Users size =  "+ db.getUsers().size());


}

The output

Users size =  2
==========Get Demo from DB ==============
Users size =  3    
Luanne
  • 19,145
  • 1
  • 39
  • 51
minglight
  • 87
  • 6

1 Answers1

0

This is probably what you are running into: https://github.com/neo4j/neo4j-ogm/issues/38

You should be able to work around this by annotating your setUser and setUsers methods with the appropriate @Relationship

If that does not help, please report an issue (or add onto the one above).

Update: Confirmed that this is indeed the same issue as described in https://github.com/neo4j/neo4j-ogm/issues/38

Until this issue is fixed, you'll need to make your objects navigable in both directions. In your example, User will need to have two references to Demo each annotated with the appropriate @Relationship

Luanne
  • 19,145
  • 1
  • 39
  • 51