2014-08-31 55 views
2

我正在使用Ebean和Java的play framework 2.3.3。 我有以下型號(我實現getter和setter):Ebean/Play框架關係未更新

@Entity 
public class Follow { 

    @ManyToOne 
    private User follower; 

    @ManyToOne 
    private User followed; 
} 

@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); } 
} 

,並測試了這種關係,我有:

@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()); 
} 

但這個工程:

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); 

問題是,儘管在數據庫中創建了用戶和後續內容,但第二個和第三個斷言失敗(這是用戶沒有任何後續操作)。 我試圖先創建用戶,然後遵循,但它仍然無法正常工作。

回答

1

已解決。看來解決方案是在實體Follow中創建一個ID字段。 我試圖把這兩個字段作爲id,但Ebean不接受它,所以我最終使用了long類型的id。