1.添加Post类

package models;

import java.util.*;
import javax.persistence.*;
import play.db.jpa.*; @Entity
@Table(name = "blog_post")
public class Post extends Model {
public String title;
public Date postedAt; @Lob
public String content; @ManyToOne
public User author; public Post(User author, String title, String content) {
this.author = author;
this.title = title;
this.content = title;
}
}

@Lob 标识,字段是一个large text的类型,@ManyToOne 标识每个Post只能对应一个User,一个User可以对应多个Post

2. 添加测试用例

        @Test
public void createPost() {
// Create a new user and save it
User user = new User("bob@gmail.com", "####", "Bob").save(); // Create a new post
new Post(user, "My first post", "Hello world").save(); // Test that the post has been created
assertEquals(1, Post.count()); // Retrieve all posts created by user
List<Post> posts = Post.find("byAuthor", user).fetch(); // Tests
assertEquals(1, posts.size());
Post firstPost = posts.get(0);
assertNotNull(firstPost);
assertEquals(user, firstPost.author);
assertEquals("My first post", firstPost.title);
assertEquals("Hello world", firstPost.content);
assertNotNull(firstPost.postedAt);
}

  

3.添加Comment类

@Entity
public class Comment extends Model {
public String author;
public Date postedAt; @Lob
public String content; @ManyToOne
public Post post; public Comment(Post post, String author, String content) {
this.post = post;
this.author = author;
this.content = content;
this.postedAt = new Date();
}
}

  

4.添加测试用例

@Test
public void postComments() {
// Create a new user and save it
User bob = new User("bob@gmail.com", "secret", "Bob").save(); // Create a new post
Post bobPost = new Post(bob, "My first post", "Hello world").save(); // Post a first comment
new Comment(bobPost, "Jeff", "Nice post").save();
new Comment(bobPost, "Tom", "I knew that !").save(); // Retrieve all comments
List<Comment> bobPostComments = Comment.find("byPost", bobPost).fetch(); // Tests
assertEquals(2, bobPostComments.size()); Comment firstComment = bobPostComments.get(0);
assertNotNull(firstComment);
assertEquals("Jeff", firstComment.author);
assertEquals("Nice post", firstComment.content);
assertNotNull(firstComment.postedAt); Comment secondComment = bobPostComments.get(1);
assertNotNull(secondComment);
assertEquals("Tom", secondComment.author);
assertEquals("I knew that !", secondComment.content);
assertNotNull(secondComment.postedAt);
}

  

5.在Post类中添加Comment

@OneToMany(mappedBy="post", cascade=CascadeType.ALL)
public List<Comment> comments; public Post(User author, String title, String content) {
this.comments = new ArrayList<Comment>();
this.author = author;
this.title = title;
this.content = title;
this.postedAt = new Date();
}

  

6.在Post类中添加方法

public Post addComment(String author, String content) {
Comment newComment = new Comment(this, author, content).save();
this.comments.add(newComment);
this.save();
return this;
}

  

7.添加测试用例

@Test
public void useTheCommentsRelation() {
// Create a new user and save it
User bob = new User("bob@gmail.com", "secret", "Bob").save(); // Create a new post
Post bobPost = new Post(bob, "My first post", "Hello world").save(); // Post a first comment
bobPost.addComment("Jeff", "Nice post");
bobPost.addComment("Tom", "I knew that !"); // Count things
assertEquals(1, User.count());
assertEquals(1, Post.count());
assertEquals(2, Comment.count()); // Retrieve Bob's post
bobPost = Post.find("byAuthor", bob).first();
assertNotNull(bobPost); // Navigate to comments
assertEquals(2, bobPost.comments.size());
assertEquals("Jeff", bobPost.comments.get(0).author); // Delete the post
bobPost.delete(); // Check that all comments have been deleted
assertEquals(1, User.count());
assertEquals(0, Post.count());
assertEquals(0, Comment.count());
}

  

运行Test,如有异常会出现下方提示

.

Play Framework 完整实现一个APP(三)的更多相关文章

  1. Play Framework 完整实现一个APP(十一)

    添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...

  2. Play Framework 完整实现一个APP(五)

    程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...

  3. Play Framework 完整实现一个APP(二)

    1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...

  4. Play Framework 完整实现一个APP(十四)

    添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...

  5. Play Framework 完整实现一个APP(十三)

    添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...

  6. Play Framework 完整实现一个APP(十二)

    1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...

  7. Play Framework 完整实现一个APP(十)

    1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...

  8. Play Framework 完整实现一个APP(九)

    添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...

  9. Play Framework 完整实现一个APP(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

随机推荐

  1. 跨域post请求实现方案小结--转

    [名词解释] 跨域:https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript 同源策略 ...

  2. Handle类与线程

    首先声明Handle对象和该类的handleMessage方法: Handler BarHandler = new Handler(){ @Override public void handleMes ...

  3. 百度eCharts体验

    前言 从昨天开始给项目里添加一些图表对比功能,上一个项目里使用的是Highcharts,本打算继续用Highcharts做的,昨天试了下做出来的效果不太好,主要也是因为看的多了没什么新鲜感了,于是便尝 ...

  4. jQuery 插件为什么要return this.each()

    jQuery.fn.test2= function(){ this.css("background","#ff0");//这里面的this为jquery对象,而 ...

  5. js基础篇——变量

    a.变量类型 变量类型 构造函数 举例 类型检测typeof 字符串 function String() var t = "chua"; var m = new String(&q ...

  6. .NET Core New csproj 如何发布可执行文件

    一.前言 .NET工具链在最新的Preview3版本中,引入了新的MSBuild项目系统,项目文件又回归了.csproj的XML文件来管理,项目文件.包引用.程序集引用..NET Core工具集.发布 ...

  7. The SQL Server Service Broker for the current database is not enabled

    把一个数据恢复至另一个服务器上,出现了一个异常: The SQL Server Service Broker for the current database is not enabled, and ...

  8. Matches正则使用提取内容

    用VS新建WinForm程序,窗体上是三个文本框和一个按钮.可以自己构造正则表达式,自己修改匹配内容 正则表达是要提取的部分为hewenqitext 代码如下: using System; using ...

  9. membership与成员资格

    membership成员资格是ASP.NET 成员资格为您提供了一种验证和存储用户凭据的内置方法.因此,ASP.NET 成员资格可帮助您管理网站中的用户身份验证.它包含以下功能 创建新用户和密码. 将 ...

  10. 错误:下列软件包有未满足的依赖关系: openssh-server : 依赖: openssh-client (= 1:7.1p1-4)

    解决办法:尝试了很久才解决,这个是我发现最有用的,完美的解决了我的困难 后续过程参考:http://blog.csdn.net/jszhangyili/article/details/8881807 ...