我使用play框架来做web样本切除并遇到与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
为空。
如何获取用户的其他字段?
答案 0 :(得分:1)
按如下所示更改findByUser
方法,问题就消失了:
/**
* Retrieve the user's note
*/
public static List<Note> findByUser(User user) {
return find.join("user").where().eq("user", user).findList();
}