diff --git a/app/models/Comment.java b/app/models/Comment.java index d9e41f6..5c2fb4b 100644 --- a/app/models/Comment.java +++ b/app/models/Comment.java @@ -2,22 +2,19 @@ import java.util.Date; -import javax.persistence.Entity; -import javax.persistence.Lob; -import javax.persistence.ManyToOne; - +import com.google.code.morphia.annotations.Embedded; +import com.google.code.morphia.annotations.Entity; +import com.google.code.morphia.annotations.Reference; import play.data.validation.Required; -import play.db.jpa.Model; -@Entity +import play.modules.morphia.Model; + +@Embedded public class Comment extends Model { public Date postedat; - @Lob @Required public String text ; - @ManyToOne - public Post post; - @ManyToOne + @Reference public User author; @Required public String author_firstname; @@ -29,9 +26,8 @@ public class Comment extends Model { * @param text * @param postedat */ - public Comment(Post post, User author, String author_name, String text, Date postedat) { + public Comment(User author, String author_name, String text, Date postedat) { super(); - this.post = post; this.author = author; this.text = text; this.postedat = postedat; diff --git a/app/models/Post.java b/app/models/Post.java index cdfafca..759e9e5 100644 --- a/app/models/Post.java +++ b/app/models/Post.java @@ -3,24 +3,23 @@ import java.util.Date; import java.util.List; -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.Lob; -import javax.persistence.ManyToOne; -import javax.persistence.OneToMany; + +import com.google.code.morphia.annotations.Embedded; +import com.google.code.morphia.annotations.Entity; +import com.google.code.morphia.annotations.Reference; import play.data.validation.MaxSize; -import play.db.jpa.Model; +import play.modules.morphia.Model; + @Entity public class Post extends Model { public String title; - @Lob @MaxSize(16000) public String text ; - @ManyToOne + @Reference public User author; - @OneToMany(mappedBy = "post" , cascade = CascadeType.ALL) + @Embedded public List comments ; public Date createdat; public Date published; @@ -41,6 +40,6 @@ public Post(String title, String text, User author, Date createdat) { } @Override public String toString(){ - return id.toString() +"-"+ author.firstname + ": " + title ; + return this.getId().toString() +"-"+ author.firstname + ": " + title ; } } diff --git a/app/models/User.java b/app/models/User.java index 5b01005..8b20db2 100644 --- a/app/models/User.java +++ b/app/models/User.java @@ -1,9 +1,11 @@ package models; -import javax.persistence.Entity; import javax.persistence.ManyToOne; -import play.db.jpa.Model; +import com.google.code.morphia.annotations.Entity; + +import play.modules.morphia.Model; + /** * @author diego diff --git a/conf/application.conf b/conf/application.conf index 88c1c3e..016daf6 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -222,3 +222,15 @@ db.url=jdbc:mysql://127.11.68.129:3306/javablog db.driver=com.mysql.jdbc.Driver db.user=admin db.pass=7kSLN1XYZcyW + +# configure mongodb host and port. Default value: 127.0.0.1:27017 +morphia.db.seeds=127.0.0.1:27017 +# +# configure mongodb authentication +# - username. Default value: empty +morphia.db.username=javablog +# - password. Default value: empty +morphia.db.password=1234 +# +# configure database name. Default value: test +morphia.db.name=javablog diff --git a/conf/dependencies.yml b/conf/dependencies.yml index a0d3376..ce08ae5 100644 --- a/conf/dependencies.yml +++ b/conf/dependencies.yml @@ -5,4 +5,4 @@ require: - play -> openshift 0.1.2 - play -> crud - play -> secure - + - play -> morphia 1.2.4 diff --git a/test/ModelTest.java b/test/ModelTest.java index 1b170de..9d46bfb 100644 --- a/test/ModelTest.java +++ b/test/ModelTest.java @@ -1,6 +1,7 @@ import static org.junit.Assert.*; import java.sql.Date; +import java.util.List; import models.Comment; import models.Post; @@ -8,6 +9,8 @@ import org.junit.Test; +import com.google.code.morphia.Key; + import play.test.UnitTest; @@ -20,7 +23,8 @@ public void modelTest(){ user.save(); //retrieve user data - User newUser = User.find("username = ?", "diego").first(); + //User newUser = User.find("username = ?", "diego").first(); + User newUser = User.q().filter("username =","diego").first(); assertNotNull(newUser); assertEquals("Diego", newUser.firstname); @@ -30,16 +34,27 @@ public void modelTest(){ assertNotNull(post); post.save(); //Retrieve post data - Post newPost = Post.find("author = ? ", newUser).first(); + Key usrKey = User.find("firstname","Diego").getKey(); + Post newPost = Post.find("author", usrKey).first(); + + //Lo que tiene de poderosisimo es que puedo traer una lista de Keys author para trer toda la lista de posta con esos autores + //como hacer un select in + //String[] emails = {"greenlaw110@gmail.com", "greenlaw110@hotmail.com"}; + //List> usrKeys = User.find("email in",emails).asKeyList(); + //List newPosts = Post.q().filter("author in", usrKeys).asList(); + + assertNotNull(newPost); assertEquals("Diego", newPost.author.firstname); //Comment - Comment comment = new Comment(newPost, newUser,"Diego", "este blog no me cabe ni un poco", createdAt); + + + Comment comment = new Comment(newUser,"Diego", "este blog no me cabe ni un poco", createdAt); assertNotNull(comment); comment.save(); //retrieve comment data - Comment newComment = Comment.find("post = ?", newPost).first(); + Comment newComment = Comment.q().filter("post = ?", newPost).first(); assertNotNull(newComment); assertEquals("Diego", newComment.post.author.firstname);