Unexpected behaviour upon update. Bug?

#1

This sample does not work as I expect.  Why?  Is it a bug? Below is the complete program.

Expect: 3
Get: 4

import java.io.Serializable;
import java.util.List;
import javax.persistence.*;

@Entity
public class Update implements Serializable
  {
    @Id
    int i;
    int v;

    public static void main (String[] args)
      {
        EntityManagerFactory emf =
            Persistence.createEntityManagerFactory("ODB.odb");
        EntityManager em = emf.createEntityManager();

        em.getTransaction().begin();
        Update simple = new Update();
        simple.i = 0;
        simple.v = 3;
        em.persist(simple);
        em.getTransaction().commit();

        simple.v = 4;
        em.getTransaction().begin();
        simple = em.createQuery("SELECT s FROM Update s" , Update.class)
            .getResultList().listIterator().next();
        System.out.println(simple.v);
      }
  }
#2

It seems to work as expected. The entity object is cached in the EntityManager as a managed entity object and it is returned with the update from the query.

Changes in one EntityManager instance are invisible in other EntityManager instances until commit, but visible in the EntityManager in which they occurred.

ObjectDB Support

Reply