-->
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: Can not update a recor: object already associated
PostPosted: Fri Dec 22, 2006 7:43 pm 
Newbie

Joined: Fri Dec 09, 2005 4:57 am
Posts: 7
Hi

I am not able to update a record into mysql. After the update method is executed, the record remains unchanged. I do not have any problem in inserting a new record. I have checked the table and the primary key is properly defined.

Following are the details of the problem:


Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="com.hunza.kcw.bus.User" table="KCWUSERS">

<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="userId" column="USERID">
<generator class="increment"/>
</id>

<property name="userName">
<column name="USERNAME"/>
</property>

<property name="password">
<column name="PASSWORD"/>
</property>

<property name="email">
<column name="EMAIL"/>
</property>

<property name="lastLogOn">
<column name="LASTLOGON"/>
</property>



<property name="address1">
<column name="ADDRESS1"/>
</property>

<property name="address2">
<column name="ADDRESS2"/>
</property>

<property name="city">
<column name="CITY"/>
</property>

<property name="state">
<column name="STATE"/>
</property>

<property name="zip">
<column name="ZIP"/>
</property>

<property name="phone">
<column name="PHONE"/>
</property>


<property name="shippingAddress1">
<column name="SHIPPINGADDRESS1"/>
</property>

<property name="shippingAddress2">
<column name="SHIPPINGADDRESS2"/>
</property>

<property name="shippingCity">
<column name="SHIPPINGCITY"/>
</property>

<property name="shippingState">
<column name="SIHPPINGSTATE"/>
</property>

<property name="shippingZip">
<column name="SHIPPINGZIP"/>
</property>

<property name="shippingPhone">
<column name="SHIPPINGPHONE"/>
</property>


<property name="nameOnCard">
<column name="NAMEONCARD"/>
</property>

<property name="creditCardNumber">
<column name="CREDITCARDNUMBER"/>
</property>

<property name="expirationDate">
<column name="EXPIRATIONDATE"/>
</property>

<property name="cvv">
<column name="CVV"/>
</property>



<property name="comments">
<column name="COMMENTS"/>
</property>

<property name="sendUpdates">
<column name="SENDUPDATES"/>
</property>

<property name="firstName">
<column name="FIRSTNAME"/>
</property>

<property name="lastName">
<column name="LASTNAME"/>
</property>

</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

I am using the following code to update the record in the database:

Code:
      try {
         logger.debug("Updating user");

         Session session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();
         session.update(user);
         tx.commit();
         HibernateUtil.closeSession();
         logger.debug("User updated successfully");
         logger.debug("User ID: " + user.getUserId());
         result = true;
      }   catch (HibernateException ex) {
            logger.error("Error while updating the user: " + ex);
      }



No Exception is thrown when the above code is executed:

Full stack trace of any exception that occurs:

2006-12-22 17:48:14,587 DEBUG [com.hunza.kcw.db.UserManager] - Updating user
2006-12-22 17:48:14,587 DEBUG [org.hibernate.transaction.JDBCTransaction] - begin
2006-12-22 17:48:14,587 DEBUG [org.hibernate.transaction.JDBCTransaction] - current autocommit status: false
2006-12-22 17:48:14,647 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - ignoring persistent instance
2006-12-22 17:48:14,647 DEBUG [org.hibernate.event.def.DefaultSaveOrUpdateEventListener] - object already associated with session: [com.hunza.kcw.bus.User#4]
2006-12-22 17:48:14,647 DEBUG [org.hibernate.transaction.JDBCTransaction] - commit
2006-12-22 17:48:14,647 DEBUG [org.hibernate.transaction.JDBCTransaction] - committed JDBC Connection
2006-12-22 17:48:14,647 DEBUG [com.hunza.kcw.Hibernate.HibernateUtil] - Closing Session of this thread.
2006-12-22 17:48:14,647 DEBUG [org.hibernate.impl.SessionImpl] - closing session
2006-12-22 17:48:14,647 DEBUG [org.hibernate.jdbc.ConnectionManager] - closing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2006-12-22 17:48:14,647 DEBUG [org.hibernate.connection.DriverManagerConnectionProvider] - returning connection to pool, pool size: 1
2006-12-22 17:48:14,647 DEBUG [com.hunza.kcw.db.UserManager] - User updated successfully
2006-12-22 17:48:14,647 DEBUG [com.hunza.kcw.db.UserManager] - User ID: 4



Can anyone tell why this is happening? I have been using Hibernate for quite a few months, but this problem has completely stumped me.

Thanks

Rauf


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 23, 2006 8:45 am 
Regular
Regular

Joined: Wed Jul 27, 2005 2:33 am
Posts: 118
The actual update query is not fired to the database until the session is flushed.


Code:
try {
         logger.debug("Updating user");

         Session session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();
         session.update(user);
        /*
          * Flush the session so that the update query gets fired.
          * You can as well set the auto-flush property in the hibernate-cfg.xml
          *
          */
         session.flush();

         tx.commit();
         HibernateUtil.closeSession();
         logger.debug("User updated successfully");
         logger.debug("User ID: " + user.getUserId());
         result = true;
      }   catch (HibernateException ex) {
            logger.error("Error while updating the user: " + ex);
      }


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 23, 2006 11:55 am 
Newbie

Joined: Fri Dec 09, 2005 4:57 am
Posts: 7
Thank you, Jaikiran. Flushing session solved the problem.

You get the credit!

Rauf


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.