-->
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.  [ 7 posts ] 
Author Message
 Post subject: how to prove cascade="none" is working for many-to
PostPosted: Tue Jul 18, 2006 1:07 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi

I have a mapping that says the cascade="none" for parentCategory property
but when I test it, I can't seem to prove cascade="none", it always
gets persisted into DB as below

E.g.

Mapping

Code:
<hibernate-mapping>

    <class   name="org.ecomfuse.model.Category"
            table="category">
      ...
     <many-to-one
               name="parentCategory"
                       column="parent_category_id"
                   cascade="none"
                   outer-join="false">
        </many-to-one>
       ...
</hibernate-mapping>       

Tests
Code:
   
public void testParentCategoryNoneCascade() throws Exception {
       
    Category category = dao.getCategory(new Long(1));
    Category parentCategory = dao.getCategory(new Long(4));
   
    assertNull( category.getParentCategory());

    category.setParentCategory(parentCategory);

    dao.saveCategory(category);

    hibernateTemplate.flush();
    hibernateTemplate.clear();

    Category retrievedCategory = dao.getCategory(new Long(1));
   
    // I would have expected assert below to be null but apparently
    // this is not the case
    assertNotNull( retrievedCategory.getParentCategory());
   
}


Can someone show me the right test to prove that the parentCategory
end is not persisted to database due to cascade="none"?


Thanks,


Sam


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 2:28 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
You can test by modifying some property of parentCategory and by asserting that this did not get updated. While modifying make sure that the objects are detached, otherwise it would get updated because of "automatic dirty checking."

Your test case is not working because by default for many-to-one tag update=true. If you set update=false, your test case would pass. As you can see this has nothing to do with the cascade option.

If a new category is used for parentCategory, that does not exist in the database, normally an exception would be thrown with cascade="none" option.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 2:41 am 
Regular
Regular

Joined: Tue May 16, 2006 3:32 am
Posts: 117
Sorry, for your test case it should be insert=false, instead of update since the values are yet to be inserted. Modifying update/insert is just for your understanding.


Top
 Profile  
 
 Post subject: reply
PostPosted: Tue Jul 18, 2006 3:28 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Thank you Jayesh

;)

Sam


Top
 Profile  
 
 Post subject: reply
PostPosted: Tue Jul 18, 2006 11:34 pm 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi,
In case anyone is interested (or can see something wrong with my code),
below is the code for the test I come up with

Code:
/**
  * Tests to verify the cascade="none" mapping behaviour of parent property
  * such that modifying the properties of a parent category retrieved from
  * a category, then save the category will have no effect to parent
  * category
  *
  * @throws Exception
*/
public void testParentCategoryNoneCascade() throws Exception {
       
       
        sessionFactory = hibernateTemplate.getSessionFactory();
        session = SessionFactoryUtils.getNewSession(sessionFactory);
        Transaction tr = session.beginTransaction();
       
        Category category = dao.getCategory(new Long(2));
        Category parentCategory = category.getParentCategory();
       
        // Call below doesn't detach persistent objects. parentCategory still got persisted
        // hibernateTemplate.flush();
       
        // Detach parentCategory so 'dirty checking' will not persist parentCategory's state
        session.evict(parentCategory);
       
        parentCategory.setName("cake");

        dao.saveCategory(category);
       
        tr.commit();
        session.close();
       
         Category retrievedParentCategory = dao.getCategory(new Long(4));
         String parentCategoryName = retrievedParentCategory.getName();
         log.warn("retrievedParentCategory name: " + parentCategoryName);
         
         // Proves that parentCategory is not persisted due to cascade="none"
         assertFalse( parentCategoryName.equals("cake"));

    }


Note: dao = a class that extends BaseDaoHibernate
hibernateTemplate = HibernateTemplate from Spring


Top
 Profile  
 
 Post subject: reply
PostPosted: Fri Aug 04, 2006 5:50 am 
Beginner
Beginner

Joined: Tue Apr 25, 2006 10:46 am
Posts: 28
Hi,

Just to share my experience. The code I presented for
proving cascade="none" does not work as when
I change cascade="save-update", test still pass.

If anyone has done this, please let me know.

Thanks,


Top
 Profile  
 
 Post subject: Test presumption is wrong
PostPosted: Mon Aug 07, 2006 10:55 pm 
Beginner
Beginner

Joined: Thu May 05, 2005 11:12 pm
Posts: 26
Hi. Just looking at your code it appears as though your test isn't testing what you think it is.

When you issue category.setParentCategory( parentCategory ) you are in fact changing category. You are adding the id '4' to the column that holds the parent category reference.

You then save it. Naturally, when you retrieve it the id will be set. You have in fact not performed any update to parentCategory, so when category is saved, there is no cascade to perform on 'the parentCategory end'.

Andrew


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.