I have a two objects ReleaseDBO and ProjectDBO (DBO=DatabaseObject) and I have a DTO ReleaseDTO that contains the release id and project id.
I am trying to implement a method that will save new ones or existing ones like saveOrUpdate().
My first try was to create a detached instance and merge it like so....
Code:
ReleaseDBO rel = Converter.convert(releaseDto);
ProjectDBO project = session.load(ProjectDBO.class, releaseDto.getProjectId());
project.addRelease(rel);
session.merge(rel);
Naturally, this complains about persisting a detached instance because of project.addRelease(rel);
I also cannot do the following code because a release has to have a project(not-null constraint) and so this fails during commit
Code:
ReleaseDBO rel = Converter.convert(releaseDto);
session.update(rel);
ProjectDBO project = session.load(ProjectDBO.class, releaseDto.getProjectId());
project.addRelease(rel);
Next, I thought of loading first but this is really bad because now my version is not used and compared for validity......
Code:
ReleaseDBO rel = Db.get().get(ReleaseDBO.class, releaseDto.getId());
if(rel == null)
rel = new ReleaseDBO();
Converter.convert(releaseDto, rel);
ProjectDBO project = Db.get().load(ProjectDBO.class, releaseDto.getProjectId());
project.addRelease(rel);
So to go over what I am trying to do again....
1. I would like to save new Releases
2. I would like to save changes to old Releases
3. I have to add a release to a project since every release must have an instance of a project(this is what causes most of my problems)
The only solution I can get so far is the last one above BUT with the addition of my own version checking which seems kind of ugly. Is there a better way to do this?
thanks,
dean