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?