0

我使用 play framework 做 web sample excise 并遇到与 manyToOne 相关的问题。
我的对象关系如下:

@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","bob@gmail.com");  
        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("bob@gmail.com", firstNote.user.email);  
    }  

我的问题是:assertEquals("bob", firstNote.user.name)通过但assertEquals("bob@gmail.com", firstNote.user.email);失败并显示firstNote.user.email为空。

如何获取用户的其他字段?

4

1 回答 1

1

改变findByUser方法如下,问题就消失了:

/**  
 * Retrieve the user's note  
 */  
public static List<Note> findByUser(User user) {  
    return find.join("user").where().eq("user", user).findList();  
}  
于 2012-07-26T02:10:30.030 回答