搜索
首页Javajava教程PlayFramework完整实现一个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 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中文网(www.php.cn)!


声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
和小红书差不多的app有哪些和小红书差不多的app有哪些Sep 22, 2022 am 11:26 AM

类似小红书的app有:1、绿洲,是一款新浪微博出品的移动端时尚社交应用软件;2、杂志迷,是一款手机各类杂志供大家阅读的APP;3、美丽修行,是一款提供护肤解决方案及化妆品购买决策的产品;4、凹凹啦,是一款手机化妆品点评软件,为用户提供最实用、最新鲜、最全方位的护肤、化妆点评和分享信息;5、抹茶美妆,一款实用的化妆品交流社区;6、美丽说,一款以分享为主题的趣味社交平台。

橙色软件是什么购物app橙色软件是什么购物appOct 13, 2022 pm 02:01 PM

橙色软件指的是“淘宝”。淘宝是随时随地轻松购物app,为用户提供新的购物渠道,享受更多优惠活动,让用户不用出门就可以享受优惠购物平台,方便买卖家之间的沟通,购物更加方便。淘宝能满足人们生活中的各种需求:搜索和浏览商品、加入购物车、下单支付、物流查询、客服交流、发表购物评论、分享优质商品等等,还能通过本地功能,找到附近的生活优惠信息。

微软改进了 Windows 11 的 Smart App Control,但你可能无法使用它微软改进了 Windows 11 的 Smart App Control,但你可能无法使用它Apr 17, 2023 pm 04:40 PM

微软于今年早些时候推出了Windows11安全功能SmartAppControl。当时,微软将其描述为“阻止不受信任或具有潜在危险的应用程序”的保护工具。当时该工具的独特之处在于它决定了它是否应该在Windows11设备上运行。SmartAppControl一开始以评估模式运行;正是在这种模式下,应用程序决定是否应该打开它。微软确实透露用户可以在Windows安全应用程序的应用和浏览器控制部分启用智能应用控制。仅根据Microsoft的说法,SmartAppC

防诈骗的app叫什么防诈骗的app叫什么Sep 02, 2022 pm 03:54 PM

防诈骗的app叫“国家反诈中心”,是一款帮助用户预警诈骗信息、快速举报诈骗内容、提升防范意识的反电信诈骗应用。它的“反诈预警、身份验证、App自查、风险预警”等核心功能可以最大限度减少民众被骗的可能性;可以对那些诈骗电话或信息快速向平台举报,帮助他人减少遇到类似的情况;能够帮助用户随时监控各种恶意软件,让各种骗局无路可走,给每个用户带来一个非常安全的生活环境。

苏州地铁用什么app扫码进站苏州地铁用什么app扫码进站Nov 09, 2022 pm 05:29 PM

苏州地铁用“苏e行”app扫码进站。苏e行app ,提供苏州地铁扫码乘车服务,先乘车,后付款,结合苏州城市文化及地铁出行场景,围绕“地铁大脑”构建公共交通智慧出行,利用 LBS 、物联网、大数据、人工智能等先进技术为乘客提供扫码过闸、乘车服务、附近地铁商业、同时为苏州本地商家提供用户引流、活动推广等服务,构建互联网化的苏州城市生活服务。

如何在iPhone上查看和删除App Store购买历史记录如何在iPhone上查看和删除App Store购买历史记录Apr 13, 2023 pm 06:31 PM

AppStore是iOS相关应用程序的市场。在这里,您还可以找到免费应用程序和付费应用程序。应用程序可以是游戏应用程序、提高工作效率的应用程序、实用应用程序、社交媒体应用程序以及更多类别的应用程序。您可能已经从AppStore下载或购买了许多适用于iPhone的应用程序。现在您可能想知道如何查看在App Store中购买的应用程序的购买历史记录。有时,出于隐私原因,您可能需要从购买历史记录中隐藏某些购买。在本文中,我们将指导您查找购买历史记录以及如何根据需要从购买历史记录中删除/隐藏购买。第1部

猴山是什么app猴山是什么appSep 06, 2022 pm 02:38 PM

猴山指的是“AcFun”,是一个视频播放软件。AcFun以视频为载体,逐步发展出基于原生内容二次创作的完整生态,是中国弹幕文化的发源地。AcFun以“认真你就输了”为文化导向,倡导轻松欢快的亚文化,受广大二次元用户的深度喜爱,入驻超多原创视频的作者,覆盖面超广阔。

tiktok是什么apptiktok是什么appJan 07, 2021 am 11:16 AM

tiktok是抖音短视频国际版,是字节跳动旗下短视频社交app软件,于2017年5月上线,愿景是“激发创造,带来愉悦”。用户可以通过这款软件选择歌曲,拍摄音乐短视频,形成自己的作品,会根据用户的爱好,来更新用户喜爱的视频。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器