-->
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: Basic question related to transaction.commit
PostPosted: Tue Oct 18, 2011 3:56 pm 
Newbie

Joined: Fri Sep 02, 2011 9:28 am
Posts: 4
Hi
I have a basic doubt
For student and address table, following code would insert rows in both the tables
MANY TO ONE MAPPING IN XML

Code:
    <class name="com.mypackage.student.Student" table="STUDENT">
        <meta attribute="class-description">This class contains student details.</meta>
        <id name="studentId" type="long" column="STUDENT_ID">
            <generator class="native" />
        </id>
        <property name="studentName" type="string" not-null="true" length="100" column="STUDENT_NAME" />
        <many-to-one name="studentAddress" class="com.mypackage.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" />
    </class>


Code:

public class Main {

   public static void main(String[] args) {
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction transaction = null;
      try
      {
         transaction = session.beginTransaction();
         Address address1 = new Address("OMR Road", "Chennai", "TN", "600097");
         Address address2 = new Address("Ring Road", "Banglore", "Karnataka", "560000");
         Student student1 = new Student("Eswar2", address1);
         Student student2 = new Student("Joe2", address2);
         
         //Test Object
         Student student3 = new Student("Joe", address2);
         
         session.save(student1);
         session.save(student2);
         transaction.commit();  // If this line is commented and program is ran 2 times and then uncommented, the ID is still getting incremented
      } catch (HibernateException e)
      {
         transaction.rollback();
         e.printStackTrace();
      } finally
      {
         
         Integer count = ( (Integer) session.createQuery("select count(*) from Address").iterate().next() ).intValue();
         System.out.println(count);
         session.close();
      }
      
      //Integer count = (Integer) session.createCriteria("select count(*) from Address").uniqueResult();


   }

}


Can someone tell me if I comment the transaction.commit and run the program twice then the insert happens from the last session ID
For eg, if Max for student id and address id is 2, 2 respectively and I run the above program twice with transaction.commit and then run it again un-commenting that line, then value = 5,5 is inserted and not 3,3
So in the database the ID is
1,2,5 ( with 3 and 4 missing)
Is it expected behavior? can someone explain why its happening this way and how to avoid it?


Top
 Profile  
 
 Post subject: Re: Basic question related to transaction.commit
PostPosted: Tue Oct 18, 2011 4:37 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
You are not saying which database you are using, but the "native" generator should use a database-generated Id and then it is expected behavior and you can't avoid it. ID generation can't be rolled back even if the actual insertion is rolled back. The reason is that the database must support concurrent transactions and some may commit and some may rollback. There are lots of discussions about this in the internet, for example:

http://bugs.mysql.com/bug.php?id=6714
http://stackoverflow.com/questions/4493 ... t-rollback


Top
 Profile  
 
 Post subject: Re: Basic question related to transaction.commit
PostPosted: Tue Oct 18, 2011 10:31 pm 
Newbie

Joined: Fri Sep 02, 2011 9:28 am
Posts: 4
Yes, I am using mysql
Thanks for the links
Looks like its specific to database and nothing to do with hibernate


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.