首頁 >Java >java教程 >PlayFramework完整實作一個APP(三)

PlayFramework完整實作一個APP(三)

黄舟
黄舟原創
2016-12-23 16:37:511260瀏覽

 1.新增Post類別

package models;

import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;

@Entity
import play.db.jpa.*;

@ ")
public class Post extends Model {
public String title;
public Date postedAt;

@Lob
public String content;

@ManyToOne
public User Userauthored User comof ) {
this.author = author;
this.title = title;
this.content = title;
}
}

@Lob 標識,字段為每個字段是@ManyTotextost的類型,@ManyTotextost對應一個User,一個User可以對應多個Post

 

2. 新增測試案例

       @Test
public void createPost() {
// Create a new user @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 List posts = Post.find" user).fetch();

// Tests
assertEquals(1, posts.size());
Post firstPost = posts.get(0);
assertNotNull(firstPost);
asserp. ;
assertEquals("My first post", firstPost.title);
assertEquals("Hello world", firstPost.content);
assertNotNull(firstPost.postedAt);
}新增Comment類

@Entity
public class Comment extends Model {

public String author;

public Date postedAt;

@Lob

public String content;


@ManyToOne
public String. content) {
this.post = post;
this.author = author;
this.content = content;
this.postedAt = new Date();
}
}

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

   //new Post a first comment

   //new Post a first comment

  產品, post").save();

   new Comment(bobPost, "Tom", "I knew that !").save();

   // Retrieve all comments

   List ", bobPost).fetch();


   // Tests
   assertEquals(2, bobPostComments.size());

   Comment firstCommentsNotp.ComirstComments);
   assertEquals("Jeff" , 錯誤。 ondComment);
   assertEquals("Tom ", secondComment.author);
   assertEquals("I knew that !", secondComment.content);
   assertNotNull(secondComment.postedAt);
}
. OneToMany( mappedBy="post", cascade=CascadeType.ALL)
public List comments;

public Post(User author, String title, String content) {
this.comments = new ArrayList(); .author = author;
this.title = title;
this.content = title;
this.postedAt = new Date();
}


 〜

}


 『 String author, String content) {
   Comment newComment = new Comment(this, author, content).save();
   this.comments.add(newComment);
this
  

7.新增測試案例

@Test
public void useTheCommentsRelation() {
   // 建立一個新使用者並儲存
   User bob = new User("bob@gmail.com", "secret", "Bob").save(); / 建立新文章
   Post bobPost = new Post(bob, "我的第一篇文章", "Hello world").save();

   // 發表第一則留言
  Commbob". , "Nice post");
   bobPost.addComment("Tom", "我知道!");

   // 計數事物
   tEquals(1, User.count()); )) ;
   assertEquals(2, Comment.count());

   // 檢索Bob 的貼文
   bobPost = Post.find("byAuthor", bob).first();   / / 導覽至comments
   assertEquals(2, bobPost.comments.size());
   assertEquals("Jeff", bobPost.comments.get(0).author);
捲髮
   
// 檢查所有註解是否已刪除
   assertEquals(1, User.count());
   assertEquals(0, Post.count());
     assertEquals(0, Post.count());
   



 

運行測試,如果異常會出現下方提示


 以上就是PlayFramework完整實現一個APP(三)的內容,更多相關內容請關注PHP)網(www.phpcnP)網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn