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. 【经验之谈】Git使用之Windows环境下配置

    前言 安装 配置 关于git使用的几个问题 后记 关于代码托管,以前用过vss和svn,看博客或论坛的时候,经常有人提到github,有很多著名的开源软件都托管在github,想来肯定不错(莫笑),当 ...

  2. [Keras] mnist with cnn

    典型的卷积神经网络. Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ 0., 0. ...

  3. [c++] Copy Control

    C++ allows the programmer to define how objects are to be copied, moved, assigned and destroyed. Tog ...

  4. PJAX实现

    一.浏览器历史API 浏览器历史就像一堆卡片,如下所示:

  5. C#枚举类型的常用操作总结

    枚举类型是定义了一组"符号名称/值"配对.枚举类型是强类型的.每个枚举类型都是从system.Enum派生,又从system.ValueType派生,而system.ValueTy ...

  6. 轻松搞定Win8 IIS支持SVC 从而实现IIS寄宿WCF服务

    写在前面 为了尝试在IIS中寄宿WCF服务,需要配置IIS支持SVC命令,于是便有了在DOS命令中用到ServiceModelReg.exe注册svc命令. 坑爹的是注册成功后就开始报错.无奈之下两次 ...

  7. android获得ImageView图片的等级

    android获得ImageView图片的等级问题 要实现的功能如下图,点击分享能显示选中与不选中状态,然后发送是根据状态来实现具体分享功能. 在gridview中有5个子项,每个子元素都有两张图片A ...

  8. Net设计模式实例之桥接模式( Bridge Pattern)

    一.桥接模式简介(Brief Introduction) 桥接模式(Bridge Pattern),将抽象部分与它的实现部分分离,使的抽象和实现都可以独立地变化. Decouple an abstra ...

  9. SQL Server时间粒度系列----第8节位运算以及设置日历数据表节假日标志详解

    本文目录列表: 1.位运算 2.设置日历数据表节假日标志 3.总结语 4.参考清单列表   位运算   SQL Server支持的按位运算符有三个,分别为:按位与(&).按位或(|).按位异或 ...

  10. ios7.1 in-house app的发布方法

    iOS7.1版本的in-house app必须发布到https站点才能下载安装,原来的连接: itms-services://?action=download-manifest&url=htt ...