Play Framework 完整实现一个APP(三)
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(三)的更多相关文章
- Play Framework 完整实现一个APP(十一)
添加权限控制 1.导入Secure module,该模块提供了一个controllers.Secure控制器. /conf/application.conf # Import the secure m ...
- Play Framework 完整实现一个APP(五)
程序以及基本可用了,需要继续完善页面 1.创建页面模板 创建文件 app/views/tags/display.html *{ Display a post in one of these modes ...
- Play Framework 完整实现一个APP(二)
1.开发DataModel 在app\moders 下新建User.java package models; import java.util.*; import javax.persistence. ...
- Play Framework 完整实现一个APP(十四)
添加测试 ApplicationTest.java @Test public void testAdminSecurity() { Response response = GET("/adm ...
- Play Framework 完整实现一个APP(十三)
添加用户编辑区 1.修改Admin.index() public static void index() { List<Post> posts = Post.find("auth ...
- Play Framework 完整实现一个APP(十二)
1.定制CRUD管理页面 > play crud:ov --layout 替换生成文件内容 app/views/CRUD/layout.html #{extends 'admin.html' / ...
- Play Framework 完整实现一个APP(十)
1.定制Comment列表 新增加Comment list页面,执行命令行 > play crud:ov --template Comments/list 会生成/app/views/Comme ...
- Play Framework 完整实现一个APP(九)
添加增删改查操作 1.开启CRUD Module 在/conf/application.conf 中添加 # Import the crud module module.crud=${play.pat ...
- Play Framework 完整实现一个APP(八)
创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...
随机推荐
- canvas学习笔记
html5的新标签:canvas; 作用:标签定义图形,比如图表和其他图像:标签只是图形容器,您必须使用脚本来绘制图形.默认大小:宽300px,高150px; 背景知识:概念最初由苹果公司提出的,用于 ...
- 快刀斩乱麻之 Katana
Katana-武士刀,寓意:快.准.狠! 按照常规,我们一般编写的 ASP.NET 应用程序会部署在 IIS 上(有点傻的描述),在 ASP.NET 应用程序中,我们会大量使用 HttpContext ...
- js 对cookie 的操作
<!DOCTYPE html> <html> <head> <script> function setCookie(cname,cvalue,exday ...
- PHP面试题目搜集
搜集这些题目是想在学习PHP方面知识有更感性的认识,单纯看书的话会很容易看后就忘记. 曾经看过数据结构.设计模式.HTTP等方面的书籍,但是基本看完后就是看完了,没有然后了,随着时间的推移,也就渐渐忘 ...
- Android之debug---menu的getActionView()return null
MainActivity代码 @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this a ...
- Global eval. What are the options?
David Flanagan最近写了一个关于全局eval的简单表达式,可以用一行式子表示: var geval = this.execScript || eval; 尽管看起来很简短,但是跨浏览器的兼 ...
- 理解领域模型Domain Model
定义 业务对象模型(也叫领域模型 domain model)是描述业务用例实现的对象模型.它是对业务角色和业务实体之间应该如何联系和协作以执行业务的一种抽象.业务对象模型从业务角色内部的观点定义了业务 ...
- 项目积累html标签
今天遇到一个不太常用都标签,网上以后慢慢记下项目中用到都东西. 1.<em> 标签 告诉浏览器把其中的文本表示为强调的内容.对于所有浏览器来说,这意味着要把这段文字用斜体来显示. 在文本中 ...
- 用NPOI从DataBase到Excel
NPOI的C# Helper代码 public static void WriteExcel(DataTable dt, string filePath) { ) { HSSFWorkbook wk ...
- 验证ASP.NET页生命周期时间的触发顺序
操作步骤: 在项目中创建文件lifeCycleValidation.aspx文件: 在文件lifeCycleValidation.aspx中添加代码,创建一个label控件,名字为lbText,代码如 ...