Dan Vega
Dan Vega

Reputation: 1107

Spring Boot MongoDb Collections

I am pretty new to mongo so this is probably just me not understanding how to setup collections. I have 2 domain objects and I have stripped them down to the basics for simplicity.

Post.java

@Document
public class Post {

    @Id
    private BigInteger id;

    private String title;

    private String body;

    private String teaser;

    private String slug;

    private Date postedOn;

    private Author author;

    public Post(){

    }

    // getters & setters

}

Author.java

@Document
public class Author {

    private BigInteger id;
    private String firstName;
    private String lastName;
    private String email;

    @DBRef
    private List<Post> posts;

    public Author(){

    }

    // getters and setters 

}

I have a method that gets called to load some data into the database.

@PostConstruct
private void initDatabase(){

    authorRepository.deleteAll();

    Author dv = new Author();
    dv.setFirstName("Dan");
    dv.setLastName("Vega");
    dv.setEmail("[email protected]");
    authorRepository.save( dv );

    postRepository.deleteAll();

    Post post = new Post();
    post.setTitle("Spring Data Rocks!");
    post.setSlug("spring-data-rocks");
    post.setTeaser("Post Teaser");
    post.setBody("Post Body");
    post.setPostedOn(new Date());
    post.setAuthor(dv);
    postRepository.save(post);

}

When I run mongo from the console and show collections I see both the post and author collections.

When I run db.post.find() the post contains the author object

{ "_id" : ObjectId("5666201fd4c6bcfd2f4caa90"), "_class" : "com.therealdanvega.domain.Post", "title" : "Spring Data Rocks!", "body" : "Post Body", "teaser" : "Post Teaser", "slug" : "spring-data-rocks", "postedOn" : ISODate("2015-12-08T00:11:11.090Z"), "author" : { "_id" : ObjectId("5666201fd4c6bcfd2f4caa8f"), "firstName" : "Dan", "lastName" : "Vega", "email" : "[email protected]" } }

But when I run db.author.find() I don't see the post collection in there.

{ "_id" : ObjectId("5666201fd4c6bcfd2f4caa8f"), "_class" : "com.therealdanvega.domain.Author", "firstName" : "Dan", "lastName" : "Vega", "email" : "[email protected]" }

Does anyone know what I am missing?

Upvotes: 1

Views: 2661

Answers (1)

khanou
khanou

Reputation: 1454

When you save you author object there is no post inside.

Create your Author, and create you post as you do.

Then set the post just created into the author's post list and save it.

This should do the trick

@PostConstruct
private void initDatabase(){

  authorRepository.deleteAll();

  Author dv = new Author();
  dv.setFirstName("Dan");
  dv.setLastName("Vega");
  dv.setEmail("[email protected]");
  authorRepository.save( dv );

  postRepository.deleteAll();

  Post post = new Post();
  post.setTitle("Spring Data Rocks!");
  post.setSlug("spring-data-rocks");
  post.setTeaser("Post Teaser");
  post.setBody("Post Body");
  post.setPostedOn(new Date());
  post.setAuthor(dv);
  postRepository.save(post);

  // add the code below
  dv.posts = new ArrayList<Post>();
  dv.posts.add(post);
  authorRepository.save( dv );

}

Upvotes: 1

Related Questions