Guest User

Untitled

a guest
Feb 25th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. Spring boot 1.5.10
  2. MongoDB 3.4.3
  3. Java 1.8
  4.  
  5. public class Comment {
  6.  
  7. @Id
  8. private String id;
  9. private String author;
  10. private String text;
  11.  
  12. ... constructor, getter and setter
  13.  
  14. public class Post {
  15.  
  16. @Id
  17. private String id;
  18. private String author;
  19. private String title;
  20. private String text;
  21. private long like;
  22. private List<Comment> comments;
  23.  
  24. ... constructor, getter and setter
  25.  
  26. public interface PostRepository extends MongoRepository<Post, String> {
  27.  
  28. }
  29.  
  30. @SpringBootApplication
  31. @EnableWebMvc
  32. public class PostRestApplication implements CommandLineRunner{
  33.  
  34. @Autowired
  35. private PostRepository postRepository;
  36.  
  37. public static void main(String[] args) {
  38. SpringApplication.run(PostRestApplication.class, args);
  39. }
  40.  
  41. @Override
  42. public void run(String... arg0) throws Exception {
  43.  
  44. postRepository.deleteAll(); //works
  45.  
  46. List<Comment> comments = new ArrayList<>();
  47. comments.add(new Comment("1", "Tom", "test"));
  48.  
  49. postRepository.save(new Post("2", "Clark", "title",
  50. "test", 2, comments)); //works, write on db
  51.  
  52. for(Post post:postRepository.findAll()){ //get error on findAll()
  53. System.out.println(post.toString());
  54. }
  55.  
  56. }
  57. }
  58.  
  59. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  60. at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57) ~[spring-data-commons-1.13.10.RELEASE.jar:na]
  61. at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  62. at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
  63. at com.sun.proxy.$Proxy62.findAll(Unknown Source) ~[na:na]
  64. at com.example.postRest.repository.impl.PostRepositoryImpl.findAll(PostRepositoryImpl.java:24) ~[classes/:na]
  65. ... (continue)
Add Comment
Please, Sign In to add comment