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.  [ 4 posts ] 
Author Message
 Post subject: Call to Delete Doesn't Delete Object
PostPosted: Wed Nov 04, 2009 3:06 pm 
Newbie

Joined: Wed Nov 04, 2009 2:42 pm
Posts: 4
I'm using NHibernate 2.1 and am slowly drudging through it.

I created a helper class to create a factory and am now attempting to change my repository to make calls through NHibernate. I can query data and get results but now I'm trying to delete one of my objects and it won't delete from the database. Just to let you know, I am connecting to a DB2 database on the ISeries, my show SQL is set to true and the delete SQL doesn't show. NHibernate is very powerful but as a beginner, I wish it was easier to get up to speed.

The code to get my data:

Code:

//Here I'm setting the Item source of my Telerik grid to bind the grid to my persistent class
_grdProdData.ItemsSource = precs.GetMyRecs();

//Below is the code in my persistent class to return records of type MyDAO
public IList<MyDAO> GetMyRecs()
        {         
            IList<MyDAO> myDataList = null;

            using (ISession session = NHibernateHelperCGC.OpenSession())
            {
                myDataList = session.CreateQuery("from MyDAO p where p.Customer = :Customer ")
                    .SetParameter("Customer ", "49").List<MyDAO>();
            }

            return myDataList ;
        }

//Below is the event that fires from my grid when I tell it to delete
public void DeleteExecuted(object sender, ExecutedRoutedEventArgs e)
        {                                               
            if (e.OriginalSource is Button)
            {
                Button btn = (Button)e.OriginalSource;
                MessageBox.Show(((MyDAO)btn.DataContext).SequenceNumField.ToString());

//tell the respository to delete my data access object which doesn't work.
                precs.DeleteMyDAO((MyDAO)btn.DataContext); //The DataContext is of type MyDAO
                IList<MyDAO> tst = (IList<MyDAO>)_grdProdData.ItemsSource;
                tst.Remove((MyDAO)btn.DataContext); //Delete from the actual List so the grid will reflect the change

                _grdProdData.Rebind();
            }
        }

//Function in Hibernate Helper class to delete object
public static void Delete(Object Entity)
        {
            using(ISession session = SessionFactory.OpenSession())
            {
                using(session.BeginTransaction())
                {
                    session.Delete(Entity);
                    session.Transaction.Commit();
                }
            }
        }



Top
 Profile  
 
 Post subject: Re: Call to Delete Doesn't Delete Object
PostPosted: Fri Nov 06, 2009 9:09 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Hard to say. Code looks ok. Can you enable logging with level DEBUG and see if something is there ?

_________________
--Wolfgang


Top
 Profile  
 
 Post subject: Re: Call to Delete Doesn't Delete Object
PostPosted: Mon Nov 09, 2009 1:47 pm 
Newbie

Joined: Wed Nov 04, 2009 2:42 pm
Posts: 4
wolli wrote:
Hard to say. Code looks ok. Can you enable logging with level DEBUG and see if something is there ?


Thank you for the response. After going into this somewhat further, I found that because the query to retrieve the data uses a different session than the one to delete, it will not delete. If I leave the session open (the one I used to query) until I'm ready to delete, it then works. I have learned that this behavior is because they are not within the same unit of work so when I call the delete on the object, it determines that the object is detached.

I am fine with this behavior except that it doesn't raise an access violation letting me know that it is detached. If it did, I could then re-attach it or perform some other function. Instead, nothing is known so it never deletes. This has it's obvious concerns. Any ideas or thoughts on this?


Top
 Profile  
 
 Post subject: Re: Call to Delete Doesn't Delete Object
PostPosted: Tue Nov 10, 2009 3:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
The problem is not that the object is detached. I'm working with detached objects, too, and it works fine. I assume your problem is that the object still is in the other session. If you clear that session after loading or at least remove that object from the session, it should work.

_________________
--Wolfgang


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