Play Framework fully implements an APP (3), frameworkapp

Source: Internet
Author: User

Play Framework fully implements an APP (3), frameworkapp

 

1. Add Post class

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;@Lobpublic String content;@ManyToOnepublic User author;public Post(User author, String title, String content) {this.author = author;this.title = title;this.content = title;}}

@ LobId. The field is of the large text type,@ ManyToOneEach Post can correspond to only one User, and one User can correspond to multiple posts.

 

2. Add Test Cases

        @Testpublic void createPost() {// Create a new user and save itUser user = new User("bob@gmail.com", "####", "Bob").save();// Create a new postnew Post(user, "My first post", "Hello world").save();// Test that the post has been createdassertEquals(1, Post.count());// Retrieve all posts created by userList<Post> posts = Post.find("byAuthor", user).fetch();// TestsassertEquals(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. Add Comment class

@Entitypublic class Comment extends Model {public String author;public Date postedAt;@Lobpublic String content;@ManyToOnepublic Post post;public Comment(Post post, String author, String content) {this.post = post;this.author = author;this.content = content;this.postedAt = new Date();}}

  

4. Add Test Cases

@Testpublic 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. Add Comment to the Post class

@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. Add methods to the Post class

public Post addComment(String author, String content) {    Comment newComment = new Comment(this, author, content).save();    this.comments.add(newComment);    this.save();    return this;}

  

7. Add Test Cases

@Testpublic 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());}

  

 

Run Test. If any exception occurs, the following prompt is displayed.

 

 

 

.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.