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.  [ 2 posts ] 
Author Message
 Post subject: Multipile delete/save don't work. Why? (PostgresSql 8.x)
PostPosted: Thu Apr 16, 2009 5:29 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi, I just tried to test my database and my code with some JUnits.

Cause I'm using a wrapper-class to hide the hibernate stuff from other classes. I normally do the testcases twice.

First just plain, second with the help of the wrapper class.
But that's only important to understand, why I try do the same operations more than once.

To reproduce the behaviour of the failure, I just execute the plain part twice. (Normally first plain and than wrapper code or vice versa, but this should be easier to understand.)


(Database is newly created before)
Code:
  for (int i = 0; i < 2; i++) {
      /* direct */
      
          this.s = HibernateUtil.openSession();
      
      Criteria c = this.s.createCriteria(CountryModel.class);
      org.hibernate.Transaction t = this.s.beginTransaction();
      final int rows = c.list().size();
      this.s.save(country);
      Assert.assertEquals(rows + 1, c.list().size(), 0);
      t.commit();
      this.s.close();

      this.s = HibernateUtil.openSession();
      c = this.s.createCriteria(CountryModel.class);

      t = this.s.beginTransaction();
      this.s.delete(country);
      Assert.assertEquals(rows, c.list().size(), 0);
      t.commit();
      this.s.close();
       }


Strange is that the first time it works fine, but the second time I'm getting
an "org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update."

According to the database, it violates the foreign key constraint of this:


Code:
@OneToOne(cascade = javax.persistence.CascadeType.ALL, fetch = FetchType.LAZY)
    @Cascade(value = CascadeType.DELETE_ORPHAN)
    private Unternehmensmodell unternehmensmodell;


cause the "Unternehmensmodell" with the given id does not exists. I tried with and without " CascadeType.DELETE_ORPHAN", but it didn't made an difference.

Cause it runs ones, the mappings should be OK.



By the way it fails at the save statement.

I would guess that hibernate, isn't aware that the above delete-statement has cascaded and do not use another insert for the ("Unternehmensmodell") the next time.

Is this true? How can I avoid this?

Otherwise I have no idea left, why the second time fails.

Cause db access is done by the user via a gui, I can't exclude that case, as hypothetical.

Thanks a lot in advance.

Greetings Michael


Top
 Profile  
 
 Post subject: Re: Multipile delete/save don't work. Why? (PostgresSql 8.x)
PostPosted: Fri May 08, 2009 10:18 am 
Regular
Regular

Joined: Thu Apr 14, 2005 10:39 am
Posts: 115
Hi,

the trick is that for the second save you have to use merge and not update or saveOrUpdate.

Even that isn't the expected behaviour of merge and saveOrUpdate. That's the workaround at the moment. But keep in mind that you have to use the returned instance:
Code:
country = (CountryModel) s.merge(country);


See
http://opensource.atlassian.com/projects/hibernate/browse/HHH-1661

for more details.

Greetings Michael

PS: I' lucky that the forum is back online, even some post seems to have been lost during restoration.


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