Reputation: 361
I have two model classes. One is Company.java, another is HumanResource.java.
Company.java
@Entity("companies")
public class Company {
@Id
private ObjectId id = new ObjectId();
private String companyName;
private String emailAddress;
private String pictureUrl;
@Reference
private List<HumanResource> humanResources;
...
HumanResource.java
@Entity("humanresources")
public class HumanResource {
@Id
private ObjectId id = new ObjectId();
private String firstName;
private String lastName;
private String emailAddress;
@Reference
private Company company;
...
What I want to achieve is when I save a list of companies to datastore, related list of human resources documents should be inserted automatically.
In addition, I declared
@Id
private ObjectId id = new ObjectId();
in every model class. Is it a good way or should I change it ?
Upvotes: 0
Views: 528
Reputation: 6243
Morphia will not call save()
on those references. You must call save()
on the instances you want to persist. You can pass in a list of instances so you needn't loop, necessarily, but each instance needs to get passed in explicitly.
Upvotes: 1