-->
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: Same session second time right attempt also fails
PostPosted: Thu Jun 14, 2007 9:16 am 
Newbie

Joined: Fri Apr 06, 2007 7:08 am
Posts: 4
In a single session, in any (same/or different) transaction, if my save attempts fails first time (because of constraint violation), second time i will set value properly and i should be able to save'.

but second time also my save fails (even though values are properly set)

Code:
package example;

import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class PersonTest {
   @SuppressWarnings("unchecked")
   public static void main(String args[]) {
      SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
      Session session = sessionFactory.openSession();
      session.setFlushMode(FlushMode.MANUAL);

      Person person = new Person();
      person.setId(2);
      person.setName("Kumar");
      
      log("--- First Attempt");
      try{
         session.beginTransaction();
      
         session.save(person);
         session.flush();
         
         session.getTransaction().commit();
      }catch(Exception e) {
         log("--- fail "+e);
      }
      
      log("--- Second Attempt");
      try{
         person.setAge(24);
         session.beginTransaction();
         
         session.save(person);
         log("Age "+person.getAge());
         session.flush();
         
         session.getTransaction().commit();
      }catch(Exception e) {
         log("--- fail "+e);
      }
      
      session.close();
   }
   
   private static <T> void log(T t) {
      System.out.println(t);
   }
}


appreciate your help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 12:52 am 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
Try session.evict(person) before doing the second transaction.

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 15, 2007 3:15 am 
Senior
Senior

Joined: Thu May 17, 2007 2:31 am
Posts: 194
Location: Sri Lanka
Hi

Send your mapping file and class file


in second time use saveOrUpdate() instead of save()



Amila

(Don't forget to rate if helps)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 18, 2007 8:49 am 
Newbie

Joined: Fri Apr 06, 2007 7:08 am
Posts: 4
Guys, i am trying with this (sorry for unclear)

in the first attempt (which is going to fail defintely)

Code:
try{
  ....
catch(Exception e) {
  failure = true;
  session.clear();
}



in the second attempt (it should pass, since for not-null column we are setting value)

Code:
try {
  person.setAge(24);
  if(failure)
    person = session.merge(person);
  session.save(person);
  session.flush();
}catch(...) {

}

i am trying to apply this in our project(one save will trigger 23 tables- all associations and several childrens), once i did & tested sufficiently i will let you know more


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.