我尝试显示画廊照片的一部分=> 这是我做search action
. 这个结果有avatar
like apicture
和infomation
like username or email
。
我在关系中创造了Photo.java
类似child
的东西Users.java
@ManytoOne
这是我的代码:
照片.java----
@Entity
public class Photo extends Model{
@Id
public Long id;
public String path;
@ManyToOne
@JoinColumn(name = "user_id")
public Users user;
}
用户.java-----
@Entity
public class Users extends Model{
@Id
public Long id;
@Constraints.Required
public String username;
@Constraints.Required
public String email;
@Constraints.Required
public String password;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
public List<Photo> photo = new ArrayList<Photo>();
public Users(){}
public Users(String username,String email,String password){
this.username=username;
this.email=email;
this.password=password;
}
}
搜索.java -----
public static Result search(){
DynamicForm form = form().bindFromRequest();
String name = form.get("name");
Finder<Long, Users> find = new Finder<Long, Users>(Long.class, Users.class);
List<Users> users = find.where().like("username", '%'+ name +'%').findList();
if (form.get("name")=="" || users.isEmpty() || users==null){
return ok(search_again.render());
}
else{
return ok (search_result.render(users));
}
}
search_result.scala.html----
@(users : List[Users])
@main(nav= "search"){
<h3>Result</h3>
<a href="@routes.Search.blank"><input class="button" type="button" value="Back to Search"></a>
<a href="@routes.Application.index"><input class="button" type="button" value="Back to Home"></a>
<p>Found @users.size() result(s) : </p>
<div class="sresult">
@for(user <- users){
<div id="sresult">
<div id="haha"><img src="@routes.Assets.at("upload/"+user.photo.path)"></div>
//Error here. Why "user.photo.path" not working ?
<p>
@user.username</a></br>
@user.password</a></br>
@user.email</a>
</p>
</div>
}
</div>
}
为什么“user.photo.path”不起作用?对我来说有什么理想吗?