在models包下
例子代碼
@Entity
public class Post extends Model {
public String title;
public String content;
public Date postDate;
@ManyToOne
public Author author;
@OneToMany
public List<Comment> comments;
//一些原來的service層的方法,play framework采用的是模型域充血模型
}
可見所有的filed都是public的沒有提供get,set方法,一切都只是為了開發(fā)的速度,和代碼的簡潔。
extends Model我們將不用寫id,將會(huì)繼承得到它。并且Model提供了很多很方便的方法。
我們經(jīng)常會(huì)用到的是它的 save() and find().方法,記住他們都是Model的static 方法,static方法在play framework中隨處可見
我們可以這么用 find("byTitleAndContent", title, content).first(); 返回我們想要的那個(gè)Post實(shí)體
我們再來看一個(gè)更加復(fù)雜,但是很有用的方法
為了檢索tags多個(gè)tag標(biāo)簽的文章,取的是并集。
find( "select distinct p from Post p join p.tags as t where t.name in (:tags) group by p.id, p.author, p.title, p.content,p.postedAt having count(t.id) = :size" ).bind("tags", tags).bind("size", tags.length).fetch();
Note that we can’t use the Post.find(“…”, tags, tags.count) signature here. It’s just becausetags is already a vararg.
Model類全是靜態(tài)方法,而且簡單直接,我們應(yīng)該仔細(xì)研究它,它值得如此,如果我們希望快速開發(fā)web。
the tag cloud
List<Map> result = Tag.find( "select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name order by t.name" ).fetch();