0

I use play framework to do web sample excise and meet a problem related to manyToOne.
My object relationship is as follow:

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

My question is: assertEquals("bob", firstNote.user.name) is passed but assertEquals("bob@gmail.com", firstNote.user.email); fails and shows firstNote.user.email as null.

How can I get the user's other field ?

kaiven
  • 11
  • 2
  • the question is assertEquals("bob", firstNote.user.name) is passed,but assertEquals("bob@gmail.com", firstNote.user.email); failed and show firstNote.user.email is null. so how could I get the user's other field, I have hacked google and stackoverflow and could not find answer ,thanks. – kaiven Jul 24 '12 at 06:22
  • It's strange to have an unit test inside a POJO... Also, `@Entity` annotations are missing on your classes. – ndeverge Jul 24 '12 at 06:40
  • are you sure you don't have an error in your User constructor. Could you put its code please – Seb Cesbron Jul 24 '12 at 07:40
  • thanks for reply,I has modified the description.@nico_ekito,I miss some declares. @ Seb Cesbron,I has changed the constructor,thanks. – kaiven Jul 24 '12 at 09:26
  • Please post the source to the constructor `new User("bob","bob@gmail.com");` – Samuel Jul 24 '12 at 10:14
  • @Samuel: I had post the constructor ,thanks. – kaiven Jul 24 '12 at 10:48

1 Answers1

1

Change the findByUser method as follows and the problem is gone:

/**  
 * Retrieve the user's note  
 */  
public static List<Note> findByUser(User user) {  
    return find.join("user").where().eq("user", user).findList();  
}  
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
kaiven
  • 11
  • 2