-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: POJOs -> ActionForms -> POJOs results in NonUniqueObje
PostPosted: Tue Dec 16, 2003 12:06 am 
Beginner
Beginner

Joined: Sun Oct 05, 2003 9:07 am
Posts: 47
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2003 8:31 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The real problem is that in your graph snapshot (back from form) you have two instances of the same Skill (id speaking) because your copy isn't smart enough.
I don't have proper solution to that problem. I don't know much saveOrUpdateCopy() but it may solve this issue.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.