Advertisement
Guest User

Untitled

a guest
Mar 14th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.96 KB | None | 0 0
  1. package kam.albert.domain.test.case1;
  2.  
  3. import kam.albert.domain.test.case1.MyPost.MyComment;
  4.  
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.data.mongodb.core.MongoOperations;
  9. import org.springframework.data.mongodb.core.query.Criteria;
  10. import org.springframework.data.mongodb.core.query.Query;
  11. import org.springframework.data.mongodb.core.query.Update;
  12. import org.springframework.stereotype.Component;
  13.  
  14. @Component
  15. public class TestThatFails {
  16.  
  17.     private static final String COLLECTION_NAME = "testing";
  18.  
  19.     @Autowired
  20.     private MongoOperations ops;
  21.  
  22.     /**
  23.      * @param args
  24.      */
  25.     public static void main(String[] args) {
  26.         ApplicationContext ctx = new ClassPathXmlApplicationContext(
  27.             "test-context.xml"
  28.         );
  29.         TestThatFails app = ctx.getBean(TestThatFails.class);
  30.         app.test();
  31.     }
  32.  
  33.     public void test() {
  34.         // create a post and add a comment
  35.         String postId = "post1";
  36.         MyPost myPost = new MyPost(postId);
  37.         myPost.addComment("1", "my first comment");
  38.  
  39.         // store it
  40.         this.ops.insert(myPost, COLLECTION_NAME);
  41.  
  42.         // create a second comment
  43.         MyComment comment2 = myPost.addComment("2", "my second comment");
  44.  
  45.         // update the comments, push the second comment
  46.         this.ops.updateFirst(
  47.             Query.query(Criteria.where("_id").is(postId)),
  48.             new Update().push("myComments.comments", comment2),
  49.             COLLECTION_NAME
  50.         );
  51.  
  52.         // retrieve the newly updated bean2
  53.         // (strangely, fetching the old bean1 will work just fine. just change the is("bean2") into is("bean1"))
  54.         MyPost domain = this.ops.findOne(
  55.             Query.query(Criteria.where("_id").is(postId)),
  56.             MyPost.class,
  57.             COLLECTION_NAME
  58.         );
  59.  
  60.         System.out.println("\nprinting comment types :");
  61.         for (Object comment : domain.myComments.comments) {
  62.             System.out.println("\t" + comment + " --> " + comment.getClass());
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement