2

I am trying to use Mongodb with spring-data and QueryDsl. I have following entitys:

@QueryEntity
@Document(collection="groups")
public class GroupImpl implements Group {
    private String name;
    @DBref
    private List<User> groupMembers;

and

@QueryEntity
@Document(collection="users")
public class UserImpl implements User{
    public static final String FIRST_NAME = "firstName";
    public static final String LAST_NAME = "lastName";

My Repositories are implemented like this:

public interface GroupRepository extends Repository<Group, String>,QueryDslPredicateExecutor<Group>{}

Every normal CRUD operations were running successfully. Also operations like "getUserByEmail" etc working successfully. Now I am trying to get all Groups of an User in a UnitTest.

    @Before
    public void setup(){


        roles = Arrays.asList(new String[]{"admin","user","guest"});
        user = new UserImpl();
        user.setFirstName("Foo");
        user.setLastName("Bar");
        user.setShortname("fbar");
        user.setEMail("foo@bar.com");
        user.setRoles(roles);

        user2 = new UserImpl();
        user2.setFirstName("Foo");
        user2.setLastName("Bar");
        user2.setShortname("fbar");
        user2.setEMail("foo@bar.com");
        user2.setRoles(roles);
        user = userRepository.save(user);
        user2 = userRepository.save(user2);
        group = new GroupImpl();        
        group.setGroupMembers(Arrays.asList(new User[]{user,user2}));
        group.setName("testGroup2");

        group2 = new GroupImpl();       
        group2.setGroupMembers(Arrays.asList(new User[]{user,user2}));
        group2.setName("testGroup2");
    }        
        @Test
        public void findGroupsByUser(){
            Group savedGroup = repository.save(group);
            Group savedGroup2 = repository.save(group2);
            Assert.assertTrue(savedGroup2.getGroupMembers().contains(user));
            List<Group> foundGroup = (List<Group>)repository.findAll(QGroupImpl.groupImpl.groupMembers.contains(user));
            Assert.assertNotNull(foundGroup);
            Assert.assertEquals(2, foundGroup.size());
        }

This test fails "expected:<2> but was:<0>" I am confused because savedGroup2 contains the user but if I try to get all Groups with this groupmember using QueryDsl I get no result.

I already checked the database and the users are stored successfully. I also debugged and checked if the "_id"s of the given user and the user in the database are equal. I have no Idea whats wrong.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
DCO
  • 1,222
  • 12
  • 24
  • What 's that mean QGroupImpl.groupImpl.groupMembers. at the end of the argument there is a '.' you invoke another member of the class and pass it as an argument? – erhun Dec 03 '13 at 11:18
  • Please fix line mentioned by @erhun and then someone might be able to help you. – Timo Westkämper Dec 04 '13 at 08:10
  • sry of course it should be "QGroupImpl.groupImpl.groupMembers.contains(user)". Corrected it. Someone any idea why it does not work? – DCO Dec 04 '13 at 09:08
  • I created a ticket for it https://github.com/mysema/querydsl/issues/583 – Timo Westkämper Dec 04 '13 at 22:05
  • Any news on this? The ticket says: CLOSED and fixed in 3.3.0.Beta2. Currently I'm using 3.0.0 Final, and I have a similar problem :( (using spring-data-mongodb 1.4.0.M1 and spring-data-commons 1.7.0.M1) – Benjamin M Jan 16 '14 at 05:24

0 Answers0