2012-07-24 66 views
0

我用遊戲框架做網頁樣本消費,並滿足相關的多對一問題。
我的對象關係如下:查詢多邊時如何獲得一個對象的值?

@Entity 
@Table(name="account") 
public class User extends Model{ 

    @Id 
    @Constraints.Required 
    @Formats.NonEmpty 
    @MinLength(4) 
    public String name; 

    @Constraints.Required 
    @Email 
    public String email; 
    public User(String username, String email) { 
     this.name  = username; 
     this.email  = email; 
    } 
} 
@Entity 
@Table(name="note") 
public class Note extends Model{ 
    @Id 
    public Long id; 

    @Constraints.Required 
    public String title; 

    @Required 
    @ManyToOne 
    public User user; 

    public static Model.Finder<Long,Note> find = new Model.Finder<Long, Note>(Long.class, Note.class); 
    public Note(User author,String title,) { 
     this.user   = author; 
     this.title   = title; 
    /** 
    * Retrieve the user's note 
    */ 
    public static List<Note> findByUser(User user) { 
     return find.where().eq("user", user).findList(); 
    } 
    ××this test is at another junit test case ×× 
    @Test 
    public void createNotes() { 
     User bob = new User("bob","[email protected]"); 
     bob.save(); 
     Note note1= new Note(bob, "My notes"); 
     note1.save(); 
     List<Note> bobNotes = Note.findByUser(bob); 
     Assert.assertEquals(1, bobNotes .size()); 
     Note firstNote = bobNotes .get(0); 
     assertNotNull(firstNote); 
     assertEquals(bob, firstNote.user); 
     assertEquals("My notes", firstNote.title); 
     assertEquals("bob", firstNote.user.name); 
     assertEquals("[email protected]", firstNote.user.email); 
    } 

我的問題是:assertEquals("bob", firstNote.user.name)傳遞,但assertEquals("[email protected]", firstNote.user.email);失敗,顯示firstNote.user.email爲空。

我怎樣才能得到用戶的其他領域?

+0

問題是的assertEquals( 「鮑勃」,firstNote.user.name)通過,但 的assertEquals(「[email protected] 「,firstNote.user.email); 失敗並首先顯示註釋.user.email爲空。 所以我怎麼能得到用戶的其他領域,我已經破解谷歌和 計算器,找不到答案,謝謝。 – kaiven 2012-07-24 06:22:09

+0

很奇怪有一個POJO內的單元測試...此外,'@ Entity'註釋丟失你的類。 – 2012-07-24 06:40:22

+0

你確定你的User構造函數沒有錯誤嗎?你可以把它的代碼,請 – 2012-07-24 07:40:47

回答

1

更改findByUser方法如下,問題就消失了:

/** 
* Retrieve the user's note 
*/ 
public static List<Note> findByUser(User user) { 
    return find.join("user").where().eq("user", user).findList(); 
} 
相關問題