From f7bfe82b1c1c659183dc2c4e006596469b60d720 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Wed, 14 Mar 2012 00:49:25 -0300 Subject: [PATCH 1/2] modify models, configuration && test for morphia --- app/models/Comment.java | 15 ++++++--------- app/models/Post.java | 18 ++++++++---------- app/models/User.java | 6 ++++-- conf/application.conf | 12 ++++++++++++ conf/dependencies.yml | 2 +- test/ModelTest.java | 19 ++++++++++++++++--- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/app/models/Comment.java b/app/models/Comment.java index d9e41f6..087d32a 100644 --- a/app/models/Comment.java +++ b/app/models/Comment.java @@ -1,23 +1,20 @@ package models; import java.util.Date; - -import javax.persistence.Entity; -import javax.persistence.Lob; -import javax.persistence.ManyToOne; - +import com.google.code.morphia.annotations.Entity; +import com.google.code.morphia.annotations.Reference; import play.data.validation.Required; -import play.db.jpa.Model; +import play.modules.morphia.Model; + @Entity public class Comment extends Model { public Date postedat; - @Lob @Required public String text ; - @ManyToOne + @Reference public Post post; - @ManyToOne + @Reference public User author; @Required public String author_firstname; diff --git a/app/models/Post.java b/app/models/Post.java index cdfafca..a5900b7 100644 --- a/app/models/Post.java +++ b/app/models/Post.java @@ -3,24 +3,22 @@ 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.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) + @Reference public List comments ; public Date createdat; public Date published; @@ -41,6 +39,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..18eb98e 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,7 +34,16 @@ 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); @@ -39,7 +52,7 @@ public void modelTest(){ 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); From 649cdc83581ee88695f54150b98198d4e3f2b334 Mon Sep 17 00:00:00 2001 From: Diego Ramirez Date: Thu, 15 Mar 2012 00:54:21 -0300 Subject: [PATCH 2/2] add @embeeded anotation for comment model --- app/models/Comment.java | 9 ++++----- app/models/Post.java | 3 ++- test/ModelTest.java | 4 +++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/app/models/Comment.java b/app/models/Comment.java index 087d32a..5c2fb4b 100644 --- a/app/models/Comment.java +++ b/app/models/Comment.java @@ -1,20 +1,20 @@ package models; import java.util.Date; + +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.modules.morphia.Model; -@Entity +@Embedded public class Comment extends Model { public Date postedat; @Required public String text ; @Reference - public Post post; - @Reference public User author; @Required public String author_firstname; @@ -26,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 a5900b7..759e9e5 100644 --- a/app/models/Post.java +++ b/app/models/Post.java @@ -4,6 +4,7 @@ import java.util.List; +import com.google.code.morphia.annotations.Embedded; import com.google.code.morphia.annotations.Entity; import com.google.code.morphia.annotations.Reference; @@ -18,7 +19,7 @@ public class Post extends Model { public String text ; @Reference public User author; - @Reference + @Embedded public List comments ; public Date createdat; public Date published; diff --git a/test/ModelTest.java b/test/ModelTest.java index 18eb98e..9d46bfb 100644 --- a/test/ModelTest.java +++ b/test/ModelTest.java @@ -48,7 +48,9 @@ public void modelTest(){ 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