I'm using the play framework 2.3.3 with Ebean and Java. I have the following models (I implement getters and setters):
@Entity
public class Follow {
@ManyToOne
private User follower;
@ManyToOne
private User followed;
}
and
@Entity
public class User {
@Id
@GeneratedValue
private long id;
@OneToMany(mappedBy = "followed")
private Set<Follow> followers;
@OneToMany(mappedBy = "follower")
private Set<Follow> following;
public void addFollowing(Follow f) { following.add(f); }
public void addFollower(Follow f) { followers.add(f); }
}
And to test this relationship, I have:
@Test
public void userFollowTest() {
User a = new User();
User b = new User();
Follow follow = new Follow(a, b); // a follows b
a.addFollowing(follow);
b.addFollower(follow);
Ebean.beginTransaction();
Ebean.save(a);
Ebean.save(b);
Ebean.save(follow);
Ebean.commitTransaction();
Assert.assertEquals("Number of users", 2, User.find.all().size());
Assert.assertEquals("Number of following of user A", 1, User.find.byId(1L).getFollowing().size());
Assert.assertEquals("Number of followers of user B", 1, User.find.byId(2L).getFollowers().size());
Assert.assertEquals("Number of follows", 1, Follow.find.all().size());
}
But this works:
Assert.assertEquals("Number of users", 2, User.find.all().size());
Assert.assertEquals("Number of following of user A", 1, a.getFollowing().size());
Assert.assertEquals("Number of followers of user B", 1, b.getFollowers().size());
Assert.assertEquals("Number of follows", 1, Follow.find.all().size());
Assert.assertTrue("User a is follower and User b is followed", follow.getFollower().getId() == 1 &&
follow.getFollowed().getId() == 2);
The problem is that, despite the users and the follow being created in the database, the second and third assertions fail (this is, the user doesn't have any follow). I've tried to first create the users and then the follow and it still doesn't work.