I am in the process of migrating a project from OJB to Hibernate 2.0.2. Hibernate is currently configured to use JDBC transactions and no caching. I have encountered a scenario where I have a Hibernate Session open for a period of time doing only reads. After having loaded the contents of a given table, one of the records is modified from an additional Hibernate Session elsewhere. After the modification is saved (and I have confirmed that the changes where persisted by looking at the db), a query by the read session will still return the original values. A simplified example containing both the persistable object and a main() to test it follows.

-craig

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

import java.util.List;

/**
 * @hibernate.class
 */
public class Foo {

    private long id;
    private String bar;

    public Foo() {
    }

    public Foo(String bar) {
        this.bar = bar;
    }

    /**
     * @hibernate.id
     * generator-class="increment"
     */
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    /**
     * @hibernate.property
     */
    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }

    public String toString() {
        return this.bar;
    }

private static SessionFactory sessionFactory;

public static void main(String[] args) {
try {
Foo.sessionFactory = new Configuration().addClass(Foo.class).buildSessionFactory();
Session readSession = Foo.sessionFactory.openSession();
createFoo();


// I would now expect to see "FooBar", and I do
readSession.flush(); // Extra flush just for the heck of it
System.out.println("Queried " + readSession.createCriteria(Foo.class).list().get(0));
readSession.flush(); // Extra flush just for the heck of it


updateFoo();

// I would now expect to see "Modified", but it is still "FooBar"
readSession.flush(); // Extra flush just for the heck of it
System.out.println("Queried " + readSession.createCriteria(Foo.class).list().get(0));
readSession.flush(); // Extra flush just for the heck of it


            deleteFoo();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
    }

    private static void createFoo() throws HibernateException {
        Session session = Foo.sessionFactory.openSession();
        Foo foo = new Foo("FooBar");
        System.out.println("Creating " + foo);
        session.save(foo);
        session.flush();
        session.close();
    }

    private static void updateFoo() throws HibernateException {
        Session session = Foo.sessionFactory.openSession();
        Foo foo = (Foo) session.createCriteria(Foo.class).list().get(0);
        foo.setBar("Modified");
        System.out.println("Modifying to " + foo);
        session.update(foo);
        session.flush();
        session.close();
    }

    private static void deleteFoo() throws HibernateException {
        Session session = Foo.sessionFactory.openSession();
        List foos = session.createCriteria(Foo.class).list();
        for (int i = 0; i < foos.size(); i++) {
            Foo foo = (Foo) foos.get(i);
            System.out.println("Deleting " + foo);
            session.delete(foo);
        }
        session.flush();
        session.close();
    }

}



-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to