首页> Java> java教程> 正文

PlayFramework完整实现一个APP(三)

黄舟
发布: 2016-12-23 16:37:51
原创
1207 人浏览过

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 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 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 comments;

public Post(User author, String title, String content) {
this.comments = new ArrayList ();
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,如有异常会出现下方提示

209.png

以上就是PlayFramework完整实现一个APP(三)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!