-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to approach a certain test case
PostPosted: Wed Jun 07, 2006 7:57 am 
Newbie

Joined: Wed Jun 07, 2006 7:34 am
Posts: 1
I am writing a test case to test that my foreign key constraints are making a certain row delete.

I can see from my database that the row is deleting as it should, however I am confused as to where I am going wrong with handling the sessions as to why I can still access a row in hibernate when it should be deleted.

Basically i am calling the session, inserting an object. Then recalling the session, deleting a necessary attribute (should cause a cascade effect). Then recalling the session and trying to load the object.

Where abouts am I going wrong? Is it with the handling of the sessions?

Code:
public void testCreateAndCheckKeyConstraints() throws Exception
   {
      try
      {
         Theme theme = new ThemeHelperTest().generateTheme();
         int themeId = theme.getId();
         System.out.println("THEME ID :  " + themeId);
         
         Session session = HibernateSession.getSessionFactory().getCurrentSession();
         session.beginTransaction();
         Theme returnedTheme = (Theme)session.load(Theme.class, new Integer(themeId));
         System.out.println("RETURNED ID : " + returnedTheme.getId());
         PictureSet pictureSet = returnedTheme.getPictureSet();
         session.delete(pictureSet);
         session.getTransaction().commit();
         
                        // Surely this loading call should throw an error
                        // as the previous paragraph has deleted a foreign
                        // key which in turn cascades to this table.
                        // However it is still returning the object?
         session = HibernateSession.getSessionFactory().getCurrentSession();
         session.beginTransaction();
         Theme returnedTheme2 = (Theme)session.load(Theme.class, new Integer(themeId));
         System.out.println("RETURNED THEME 2 ID : " + returnedTheme2.getId());
         session.getTransaction().commit();
         assertTrue(false);
      }
      catch(HelperException e)
      {
         assertTrue(true);
      }
   }


Cheers.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 7:24 pm 
Beginner
Beginner

Joined: Sat Jun 03, 2006 6:23 pm
Posts: 28
Code:
Theme returnedTheme2 = (Theme)session.load(Theme.class, new Integer(themeId));
System.out.println("RETURNED THEME 2 ID : " + returnedTheme2.getId());


This is what makes using proxies possible: if you get only the primary key from the proxy, it needs not be initialized.

Basically what most likely happens to you is:

1. with load() you get a proxy
2. you get the key of the proxy, so the actual db row is never loaded

Try this:
Hibernate.initialize(returnedTeheme2);

Or access any non-key field, that should also initialize the proxy. Or alternatively, you can disable proxies.

Also note that the exception thrown will most likely not be of type HelperException, that you are trying to catch.

Roland


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 7:26 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Ooops, never mind my post, Roland is correct.

Or you could use get() instead of load(), that will return null if it can't find the object in the DB.

_________________
Cheers,
Shorn.


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