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.