-->
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: Problem copying a related collection
PostPosted: Thu Mar 15, 2012 10:47 am 
Newbie

Joined: Thu May 05, 2011 6:33 am
Posts: 16
Hi,
I have got three entity

Code:
@Entity
public class FestivitySource implements Serializable{
...
   @ManyToMany(fetch = FetchType.LAZY)
   List<Place> listSede;
...}

@Entity
public class Festivity{
       @ManyToMany(fetch = FetchType.LAZY)
   List<Place> listSede;
}

@Entity
public class Place{
...
}


I use FestibitySource as source of my data. I populate, with my business logic, starting from FestivitySource the FestivityEntity.

so i do this:

Code:
Holiday holiday = new Holiday.Builder().withYear(DateUtils.getYear(festivityYear)).build();
         holidayService.save(holiday);
         for (FestivitySource festivitySource : list) {
            logger.debug("festivitySource {}", festivitySource);
            if (festivitySource.isEnable() && festivitySource.isInNextYear()) {

               Date currentYearDate;
               if (StringUtils.isNotEmpty(festivitySource.getComputingBean())) {
                  FestivityComputing festivityComputing = (FestivityComputing) applicationContext
                        .getBean(festivitySource.getComputingBean());
                  currentYearDate = festivityComputing.getDate(festivityYear, festivitySource);
               } else {
                  currentYearDate = defaultComputing.getDate(festivityYear, festivitySource);
               }
               Festivity festivity = new Festivity.Builder().withDescrizione(festivitySource.getDescrizione())
                     .[color=#BF0000]withListSede(festivitySource.getListSede()[/color]).withDataFestivita(currentYearDate).build();
               [color=#BF0000]festivityService.save(festivity);
[/color]
            }
         }



When I save my entity the collection is removed from source, but added to the target, as manual explain.

How can I copy?

I use hibernate 3.6.0.Final with spring 3.0.5 using Spring hibernate template and OpenSessionInView filter.

Does anybody has suggestion?

_________________
Luca Preziati


Top
 Profile  
 
 Post subject: Re: Problem copying a related collection
PostPosted: Fri Mar 16, 2012 10:39 am 
Newbie

Joined: Thu May 05, 2011 6:33 am
Posts: 16
I have solved with this code

Code:


public static final List getListCopy(List source) {

      List result = new ArrayList();

      for (Object obj : source) {
         try {
            result.add(org.apache.commons.beanutils.BeanUtils.cloneBean(obj));
         } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }

      return result;
   }



The problem is that Hibernate as default move collection. You need to have a copy, not a referenced copy of collection

_________________
Luca Preziati


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.