Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package kam.albert.domain.test.case1;
- import kam.albert.domain.test.case1.MyPost.MyComment;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.data.mongodb.core.MongoOperations;
- import org.springframework.data.mongodb.core.query.Criteria;
- import org.springframework.data.mongodb.core.query.Query;
- import org.springframework.data.mongodb.core.query.Update;
- import org.springframework.stereotype.Component;
- @Component
- public class TestThatFails {
- private static final String COLLECTION_NAME = "testing";
- @Autowired
- private MongoOperations ops;
- /**
- * @param args
- */
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext(
- "test-context.xml"
- );
- TestThatFails app = ctx.getBean(TestThatFails.class);
- app.test();
- }
- public void test() {
- // create a post and add a comment
- String postId = "post1";
- MyPost myPost = new MyPost(postId);
- myPost.addComment("1", "my first comment");
- // store it
- this.ops.insert(myPost, COLLECTION_NAME);
- // create a second comment
- MyComment comment2 = myPost.addComment("2", "my second comment");
- // update the comments, push the second comment
- this.ops.updateFirst(
- Query.query(Criteria.where("_id").is(postId)),
- new Update().push("myComments.comments", comment2),
- COLLECTION_NAME
- );
- // retrieve the newly updated bean2
- // (strangely, fetching the old bean1 will work just fine. just change the is("bean2") into is("bean1"))
- MyPost domain = this.ops.findOne(
- Query.query(Criteria.where("_id").is(postId)),
- MyPost.class,
- COLLECTION_NAME
- );
- System.out.println("\nprinting comment types :");
- for (Object comment : domain.myComments.comments) {
- System.out.println("\t" + comment + " --> " + comment.getClass());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement