Play Framework 完整实现一个APP(三)
1.添加Post类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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. 添加测试用例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | @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类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @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.添加测试用例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | @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
1 2 3 4 5 6 7 8 9 10 | @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类中添加方法
1 2 3 4 5 6 | public Post addComment(String author, String content) { Comment newComment = new Comment( this , author, content).save(); this .comments.add(newComment); this .save(); return this ; } |
7.添加测试用例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | @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,如有异常会出现下方提示
.
标签:
Java学习系列
, PlayFramework
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!