-->
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: Weird update
PostPosted: Tue Feb 23, 2010 3:07 pm 
Newbie

Joined: Tue Feb 23, 2010 2:43 pm
Posts: 2
Hi all, this is my first post on this forums. I'm trying to persist a simple entity. Look at the following code:
Code:
      Configuration config = new Configuration();
      config.configure();
      
      SessionFactory factory = config.buildSessionFactory();
      Session session = factory.openSession();
      Transaction tx = session.beginTransaction();
      
      Date birthDate = new GregorianCalendar(1979, 8, 20).getTime();
      Date entranceDate = new GregorianCalendar().getTime();
      Student student = new Student("David", "Marco",
            "300001", birthDate, "001", entranceDate);
      
      session.save(student);
      tx.commit();
      

      student.setSsn("SSN300001");
      session.flush();
      session.close();


As you'll see, the managed 'student' object is modified through a set method after the transaction is commited, but out of the the scope of any transaction. Thereafter, the code makes an explicit call to Session.flush(). Hibernate produces the following output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(id) from STUDENT
Hibernate: insert into STUDENT (FIRST_NAME, LAST_NAME, SSN, BIRTHDATE, STD_NO, ENTRANCE_DATE, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update STUDENT set FIRST_NAME=?, LAST_NAME=?, SSN=?, BIRTHDATE=?, STD_NO=?, ENTRANCE_DATE=? where id=?


But the database is never updated!! It remains with the values supplied to the entity when the transaction was commited. Why?? Isn't the session object synchonized with the database and therefore their managed entities as well?

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Weird update
PostPosted: Tue Feb 23, 2010 3:14 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
Firstly, u are doing something which u shouldn't :-)
But well to answer that
try this in your configuration file:
<property name="connection.autocommit">true</property>
And u might see that the database actually did get changed.
But again u are not using transactions properly in your toy.


Top
 Profile  
 
 Post subject: Re: Weird update
PostPosted: Tue Feb 23, 2010 3:28 pm 
Newbie

Joined: Tue Feb 23, 2010 2:43 pm
Posts: 2
Hi tseringshrestha. I'm conscient of the missuse of the transaction, I wanted only verify that entities bounded to the current session were synchonized on Session.close() by default. Adding your given property to the configuration file, the application performs the sql update statement successfully.

Thanks!


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.