-->
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: Automatic Dirty Checking not working...
PostPosted: Sun Sep 11, 2005 1:36 am 
Newbie

Joined: Sun Sep 11, 2005 1:16 am
Posts: 5
Hibernate version: 2.1.8

Mapping documents:

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

<hibernate-mapping>
    <class name="com.flysafe.business.Product" table="PRODUCTS">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="description"/>
        <property name="price"/>
    </class>
</hibernate-mapping>


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

Code:
            Session session = productDao.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();

            products = productDao.findAll();

            ListIterator i = products.listIterator();
            while (i.hasNext())
            {
                Product product = (Product) i.next();
                product.increasePrice(increase);
                //productDao.saveProduct(product);
            }

            tx.commit();
            session.close();


Full stack trace of any exception that occurs: No exception occured.

Name and version of the database you are using: HSQLDB

I'm using hibernate with spring. I was expecting that the above code would update the database because of the "Automatic Dirty Checking" features of hibernate. But it doesn't.

If I uncomment the line productDao.saveProduct(product) which updates the database explicity then things work.

What additional thing must I do to get "Automatic Dirty Checking" to work in this case?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 2:07 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
ADC isn't as that automatic. Nothing get's persisted unless the Session is told so ... that is probably what your doing in productDao.saveProduct(product) .


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 11, 2005 10:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hibernate should detect that change as long as it is the same session that the query and transaction is related to.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Solution...
PostPosted: Sun Sep 11, 2005 6:19 pm 
Newbie

Joined: Sun Sep 11, 2005 1:16 am
Posts: 5
The line of code

Code:
products = productDao.findAll();


called into HibernateTemplate to actually do the query. HibernateTemplate is a class in the Spring Framework. It opened a new session to do the query so that the query was not in the same session and the code that made the modifications.

I fixed the problem by replacing the above Spring based line of code to a pure Hibernate based line of code as follows:

Code:
products = session.find("from Product");


I'm sure there is a way to used the Spring Hibernate support and have this work. Maybe with the Spring transaction support. I'm still pretty new at both Spring and Hibernate.


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.