1

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?

1 Answer 1

1

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 );

}
Sign up to request clarification or add additional context in comments.

2 Comments

Gave that a try but its complaining about something. pastebin.com/raw.php?i=r199RLDP
I see what you did there. I moved that to the constructor but this seems to be working now. Thanks for the help! Totally missed that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.