-->
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: dirty updates
PostPosted: Thu Apr 12, 2007 9:32 am 
Newbie

Joined: Thu Apr 12, 2007 9:03 am
Posts: 2
Servus,

i'm looking for several hours in the hibernate documentation and this forum to find a way selecting my object twice.
I select a document through a webinterface and edit its values.
After submitting the page I want to check if there are empty values and if so replace them with the values from the document stored in the database.
While selecting the object hibernate performs an update.
Is there an option to disable this automatic dirty checking for this object?

simple code explanation
Code:
//select the document
doc = docmgr.getDocument(1);
// website edit a value (the fileData to an empty string)
doc.setFileData("");
...
// now select the original doc from db
// on this point hibernate performs the update!
// and my fileData is also ""
doc2 = docmgr.getDocument(1);


Thanks for helping

Hibernate version:
hibernate 3.2.2

Name and version of the database you are using:
MySQL 5.0.26

Mapping documents:
Code:
<hibernate-mapping>
    <class name="model.Document" table="Document" catalog="tubedata">
        <comment></comment>
        <id name="documentId" type="int">
            <column name="documentId" />
            <generator class="increment" />
        </id>
        <property name="fileName" type="string">
            <column name="fileName" not-null="true">
                <comment></comment>
            </column>
        </property>
        <property name="fileData" type="binary">
            <column name="fileData" not-null="true">
                <comment></comment>
            </column>
        </property>
    </class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Code:
   public Document getDocument(int docId) {
      Document doc = null;
      Session session = HibernateUtil.getSession();
      session.beginTransaction();
      try {
         Criteria crit = session.createCriteria(Document.class);
         crit.add(Expression.eq("documentId", docId));
         doc = (Document)crit.uniqueResult();
         session.getTransaction().commit();
      } catch (HibernateException e) {
         session.getTransaction().rollback();
         throw e;
      }
      return doc;
   }


Debug level Hibernate log excerpt:
INFO [btpool0-2] (DocumentEditController.java:48) - ********************************************START********************************************
DEBUG [btpool0-2] (JDBCTransaction.java:54) - begin
DEBUG [btpool0-2] (ConnectionManager.java:419) - opening JDBC connection
DEBUG [btpool0-2] (JDBCTransaction.java:59) - current autocommit status: false
DEBUG [btpool0-2] (AbstractBatcher.java:358) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [btpool0-2] (AbstractBatcher.java:393) - update tubedata.Document set fileName=?, fileData=? where documentId=?
Hibernate: update tubedata.Document set fileName=?, fileData=? where documentId=?
DEBUG [btpool0-2] (BatchingBatcher.java:44) - Executing batch size: 1
DEBUG [btpool0-2] (AbstractBatcher.java:366) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [btpool0-2] (AbstractBatcher.java:358) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG [btpool0-2] (AbstractBatcher.java:393) - select this_.documentId as documentId5_0_, this_.fileName as fileName5_0_, this_.fileData as fileData5_0_ from tubedata.Document this_ where this_.documentId=?
Hibernate: select this_.documentId as documentId5_0_, this_.fileName as fileName5_0_, this_.fileData as fileData5_0_ from tubedata.Document this_ where this_.documentId=?
DEBUG [btpool0-2] (AbstractBatcher.java:374) - about to open ResultSet (open ResultSets: 0, globally: 0)
DEBUG [btpool0-2] (AbstractBatcher.java:381) - about to close ResultSet (open ResultSets: 1, globally: 1)
DEBUG [btpool0-2] (AbstractBatcher.java:366) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [btpool0-2] (JDBCTransaction.java:103) - commit
DEBUG [btpool0-2] (JDBCTransaction.java:116) - committed JDBC Connection
DEBUG [btpool0-2] (ConnectionManager.java:402) - aggressively releasing JDBC connection
DEBUG [btpool0-2] (ConnectionManager.java:439) - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
INFO [btpool0-2] (DocumentEditController.java:50) - ********************************************END********************************************
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 13, 2007 7:03 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
I'd just use a detached object for editing.

after loading the document just call
session.evict(doc);
and hibernate won't care about this object any longer. Then make your changes and reload your original document (say doc2).
Then copy all non-empty values form doc to doc2 and you're done (if I didn't misunderstand you about what you wanted to do).


Top
 Profile  
 
 Post subject: Conclusion: Works for me!
PostPosted: Fri Apr 13, 2007 8:34 am 
Newbie

Joined: Thu Apr 12, 2007 9:03 am
Posts: 2
Thank you very much.
I simply did not mention the evict() method.

Have a nice day!


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.