OneToMany relationship and persisting target objects
In my application, I have a OneToMany relationship between a Job and a JobTime. So the Job can have multiple JobTime instances associated to it.
Here's the code for that relationship between both entities. I created this relationship using the Roo "field reference" command. The only change I made was changing the @ManyToMany in the Job class to a @OneToMany.
Code:
@RooJavaBean
@RooToString
@RooEntity(table = "WOJOBS", versionField = "", identifierType = JobPk.class)
public class Job {
...
@OneToMany(cascade=CascadeType.ALL, mappedBy="job")
private Set<JobTime> jobTimes = new HashSet<JobTime>();
...
}
Code:
@RooJavaBean
@RooToString
@RooEntity(table = "WOJOBTIME", versionField = "", identifierType = JobTimePk.class)
public class JobTime {
...
@ManyToOne
@JoinColumns({ @JoinColumn(name="TMNUM", referencedColumnName="JONUM", insertable=false, updatable=false), @JoinColumn(name="TMLINE", referencedColumnName="JOLINE", insertable=false, updatable=false) })
private Job job;
...
}
So this looks fine up to this point. In my code I have a service class that is used for creating instances of JobTime. Simply persisting a new JobTime doesn't associate it back to the job. I have to either do something like "jobTime.setJob(job)" or add a function to the Job class for adding a new jobTime.
That function would look like this in the Job class. This method would avoid me having to explicitly call jobTime.setJob because then I would just call "merge" on the job from my service class and the jobTime would get peristed as well.
Code:
public void addJobTime(JobTime jobTime) {
jobTime.setJob(this);
jobTimes.add(jobTime);
}
I'm just wondering if this is something that could be added to the ITD by Roo so that it's already done.