-->
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: Unable to update & delete object in single commit
PostPosted: Thu Oct 07, 2010 2:12 pm 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
I'm trying to remove a self-referencing object from my system but the below workflow doesn't allow it:

pseudo code:

-client wants to delete language object (Korean) from the system (the language object's language is itself, Korean)
-client sets object to deleted and calls save(true, languageObject) in my persistence layer
-begin transaction
-set language object's language to English (English will always exist in the DB)
-persist the object with the above change (to remove self reference)
-delete the object (no object, including itself, has a language of Korean now)
-commit the transaction

Code:
    public void save(boolean commit, ValueObject object) {
        if (commit) {
            _session.getTransaction().begin();
        }

        if (object.isDeleted()) {
              preDelete(object);
              _session.delete(object);
        } else {
               _session.persist(object);
        }

            if (commit) {
                _session.getTransaction().commit();
            }
      }

    private void preDelete(ValueObject valueObject) {
        if (valueObject instanceof Language) {
            Language sl = (Language) valueObject;
            sl.setLanguage((LanguageDO) get(LanguageId.EN));
            sl.setDeleted(false);
            save(false, sl);
            sl.setDeleted(true);
        }
    }


This results in a foreign key violation saying the language object itself still has a self-referencing language. I would expect the commit to execute the update first and the delete second. Thus the commit should run clean. Am I misunderstanding how Hibernate works in this fashion? Is there a better way to do this?

And I need to run this as one single transaction so that if there are any errors, I can rollback the entire transaction. So I can't begin a transaction, update the object, commit and then begin, delete, commit.

Thanks!
Shawn


Top
 Profile  
 
 Post subject: Re: Unable to update & delete object in single commit
PostPosted: Fri Oct 08, 2010 5:30 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I suggest you trying to place an explicite flush() call between the update and the delete.


Top
 Profile  
 
 Post subject: Re: Unable to update & delete object in single commit
PostPosted: Fri Oct 08, 2010 11:25 am 
Beginner
Beginner

Joined: Tue Mar 17, 2009 12:19 pm
Posts: 22
That fixed it. Thank you!!


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.