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. iOS开发之使用Runtime给Model类赋值

    本篇博客算是给网络缓存打个基础吧,本篇博客先给出简单也是最容易使用的把字典转成实体类的方法,然后在给出如何使用Runtime来给Model实体类赋值.本篇博客会介绍一部分,主要是字典的key与Mode ...

  2. php广告图片循环播放 幻灯片效果

    <!DOCTYPE> <html> <head> <meta http-equiv="content-type" content=&quo ...

  3. CodeIgniter-Lottery - php ci 抽奖辅助函数

    CodeIgniter-Lottery - php ci 抽奖辅助函数 Github https://github.com/xjnotxj/CodeIgniter-Lottery 用法 1. 移入文件 ...

  4. 关于从JSP页面插入数据到数据库中乱码问题的解决

    问题描述:最近我在写一个j2ee的留言板系统模块,遇到了一个非常让我头大的问题,当我从JSP页面输入数据后,通过hibernate中的业务逻辑类HQL语句把这个数据插入到本地的mysql数据库中,可是 ...

  5. 重启SQL Server——总是好事?

    在实际工作中,我经常看到——有时人们定期重启SQL Server!我们都希望接受,SQL Server的定期重启并不真的是一个好主意.但在今天的文章里,我想进一步讨论下,当你定期重启你的SQL Ser ...

  6. QT 中 关键字讲解(emit,signal,slot)

    Qt中的类库有接近一半是从基类QObject上继承下来,信号与反应槽(signals/slot)机制就是用来在QObject类或其子类间通讯的方法.作为一种通用的处理机制,信号与反应槽非常灵活,可以携 ...

  7. C#开发微信门户及应用(44)--微信H5页面开发的经验总结

    在我们开发微信页面的时候,需要大量用到了各种呈现的效果,一般可以使用Boostrap的效果来设计不同的页面,不过微信团队也提供很多这方面的资源,包括JSSDK的接口,以及Weui的页面样式和相关功能页 ...

  8. 初入网络系列笔记(1)TCP/IP基础

    一.借鉴说明,本博文借鉴以下博文 1.BlueTzar,TCP/IP四层模型, http://www.cnblogs.com/BlueTzar/articles/811160.html 2.叶剑峰,漫 ...

  9. jquery改变链接移上光标时的颜色实例

    效果体验http://hovertree.com/texiao/jquery/18/ 完整代码如下: <!DOCTYPE html> <html> <head> & ...

  10. 使用nuget打包类库并发布

    前言 NuGet 是免费.开源的包管理开发工具,专注于在 .NET 应用开发过程中,简单地合并第三方的组件库.今天的目的就是记录一下如何打包一个类库,并发布到官网.在开始之前需要在www.nuget. ...