Play Framework 完整实现一个APP(三)
2015-10-28 11:35
239 查看
1.添加Post类
@Lob 标识,字段是一个large text的类型,@ManyToOne 标识每个Post只能对应一个User,一个User可以对应多个Post
2. 添加测试用例
3.添加Comment类
4.添加测试用例
5.在Post类中添加Comment
6.在Post类中添加方法
7.添加测试用例
运行Test,如有异常会出现下方提示
![](http://images2015.cnblogs.com/blog/569355/201510/569355-20151028113630122-884368329.png)
.
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,如有异常会出现下方提示
![](http://images2015.cnblogs.com/blog/569355/201510/569355-20151028113630122-884368329.png)
.
相关文章推荐
- Object C语法学习
- android 处理器crash刊物
- 开发中遇到loadnib初始化的时候提示为空
- Android中的各种访问权限Permission含义
- ios 开发中 动态库 与静态库的区别
- iOS —label自动换行
- android中在广播中弹出pop框的局限性
- 为Android应用程序读取/dev下设备而提权
- android:Adapter中设置textview字体颜色
- Xcode7 上传AppStore出现 ERROR ITMS-90474,ERROR ITMS-90475错误
- iOS 调试技巧
- iOS 开发笔记 cocoapods 配置遇到的问题
- iOS Technology Overview_Introduction
- 关于android布局的两个属性dither和tileMode
- CocosPods学习网站 -- 链接
- 解决android:background背景图片被拉伸问题
- Android机型适配之痛[干货分享]
- Android中的PopupWindow详解
- Android-自定义PopupWindow
- Android 数据库打包随APK发布