using hibernate with Java Spring, I have two database tables, Projects and ProjectNotes. Project can have many Notes.
@Entity
@Table(name="projects")
public class Project {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany(mappedBy="project", cascade=CascadeType.ALL)
private List<ProjectNote> projectNotes;
}
and I want to add a new note to the project. Clearly i can do this.
Project project = projectRepository.findOne(id);
ProjectNote pn = new ProjectNote();
pn.setText("Hello");
pn.setProject(project);
project.getProjectNotes().add(pn);
projectRepository.save(project);
and it works. Is that any different than using the notes repository instead
Project project = projectRepository.findOne(id);
ProjectNote pn = new ProjectNote();
pn.setText("Hello");
pn.setProject(project);
projectNotesRepository.save(pn);
Is either more efficient or more correct than the other?
EDIT: another thought. I'm also using @LastModifiedBy/Date on the project. I guess the first method will alter the project lastModified information, whilst the second method will not. But I have not tried it!