Menu

trouble with Hibernate3BeanReplicator

Help
sy man
2009-03-11
2013-04-25
  • sy man

    sy man - 2009-03-11

    I have a Product class and Component class.Product can have Components ie a Set.
    I would like to retrieve a Product from database, change the ids of the Product and associated Components
    and save to database.I am using Hibernate.
    When I do a deepCopy I get an error since the Component's id is sitll being copied on a deep copy.
    Hence, an attempt to write it to database fails.
    This is the pseudocope,any help would be appreciated.

    public class ProductDAOTest extends TestCase {
        //Store to DB
        Product product = new Product("My product");
            product.addComponent(new Component("c1"));
            product.addComponent(new Component("c2"));
            long id = productDAO.store(product);
            hibernateTemplate.flush();
            hibernateTemplate.clear();
           
            //Get from db
            Product retrievedProduct = productDAO.get(id);
           
            ////Replicate
        Product prodCopy =productDAO.deepCopy(retrievedProduct);
            
            /*********  FAILS *********************/
            long idCopy = productDAO.store(prodCopy);
        hibernateTemplate.flush();
            hibernateTemplate.clear();
            
    }
    public class ProductDAO
       {
         public Product deepCopy(Product prodFromDB) {

        Product copy = null;
        try {

        //Override IDs
        HibernatePropertyFilter propertyFilter = new HibernatePropertyFilter() {
                public boolean propagate(String propertyName,                        Method readerMethod) {
                    return !"id".equals(propertyName);
                }
            };

            //Customizing Property handling
            Class<?>[] entityClasses = { Product.class, Component.class };
            Set<Class<?>> entityClassSet = new HashSet<Class<?>>(Arrays.asList(entityClasses));

            //entityclassset,propertiess to be propogate ,veto
            HibernateBeanReplicator replicator = new Hibernate3BeanReplicator(            entityClassSet, null, propertyFilter);
            HibernatePropertyFilter propertyFilter2 =replicator.getHibernatePropertyFilter();
            copy = (Product) replicator.deepCopy(prodFromDB);

        } catch (Exception e) {
            e.printStackTrace();
        return copy;
        }
    }

     
    • sy man

      sy man - 2009-03-11

      okay,answering my own question...

      Need to use copy(obj,Class) and not deepCopy() for overriding default copying of all properties.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.