I have a one-to-many mapping from a Position object to a Skill object. I have everything working good enough with Hibernate.
I have Manager objects that use BeanUtils.copyProperties() to copy the values from my POJOs (Hibernate objects) to Forms. I also loop through the children (i.e. the Skills List) and transform all the POJOs (Skills) to SkillForms (code below).
The issue I'm having is if I transform all my child objects to forms, and then transfer everything back - I get the following exception:
[junit] Caused by: net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 1, of class: us.co.adams.apptracker.persistence.PositionSkill
This actually is only a problem in my Manager Unit Tests - because I'm getting the Position and adding a new Skill. Then Hibernate thinks I'm trying to add a new one, rather than just updating the old one. In my webapp, this will never happen b/c I never do a get and save in the same session - but it would be nice to be able to do this in my unit tests. Is this possible?
The reason I'm converting the children to Forms is so I can use Struts the Struts Validator on them. I'd guess this would be a problem with any webapp framework. Either you're tightly coupling your web layer with your persistence layer by passing the POJOs to the UI, or you give up Hibernate's nice Session management by copying the POJOs to other objects. What the best practice here (regardless of Struts)? What's the best practice with Unit Tests?
Code:
public Object getPosition(String positionId) throws Exception {
positionForm = new PositionForm();
position = dao.getPosition(Long.valueOf(positionId));
if (log.isDebugEnabled()) {
log.debug("position from dao: " + position);
}
// copy form properties to position
BeanUtils.copyProperties(positionForm, position);
// loop through all the skills and convert POJOs -> Forms
List skills = positionForm.getSkills();
for (int i=0; skills != null && i < skills.size(); i++) {
PositionSkillForm form = new PositionSkillForm();
BeanUtils.copyProperties(form, skills.get(i));
positionForm.getSkills().set(i, form);
}
if (log.isDebugEnabled()) {
log.debug("got position with properties: " + positionForm);
}
return positionForm;
}
This isn't really necessary on the "get", but it is necessary