EntityManager refresh problem

#1

Hi,

in the following code you can see an example with EntityManager.refresh(). The example throws an assertion error (The Assert statement is Assert.assertEquals("entity 1", simpleEntity1.getFieldA());).

In my opinion there should not occur an assertion error. With hibernate there is no assertion error. I have tested the example with several objectdb versions (2.5.4, 2.5.6_05 and 2.5.7).  Do you have an explanation for the assertion error?

--------------------
- SimpleTest.java
--------------------
package example;

import java.io.File;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnitUtil;

import org.junit.Assert;
import org.junit.Test;

import entities.HashMapEntity;
import entities.SimpleEntity;

public class SimpleTest {

    @Test
    public void refreshTest() {
        EntityManager entityManager = null;

        try {
            entityManager = createEntityManager("d:\\Temp\\refreshTest.odb");

            entityManager.getTransaction().begin();

            SimpleEntity simpleEntity1 = new SimpleEntity();
            simpleEntity1.setFieldA("entity 1");
            simpleEntity1.setFieldB(1);
            entityManager.persist(simpleEntity1);

            simpleEntity1.setFieldA("entity 1b");
            entityManager.refresh(simpleEntity1);
            Assert.assertEquals("entity 1", simpleEntity1.getFieldA());

        } finally {
            closeEntityManager(entityManager);
        }
    }
   
    private EntityManager createEntityManager(String dbFileName) {
        File odbFile = new File(dbFileName);
        odbFile.delete();
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(odbFile.getAbsolutePath());

        return emf.createEntityManager();
    }

    private void closeEntityManager(EntityManager entityManager) {
        if (entityManager != null) {
            entityManager.close();
            entityManager.getEntityManagerFactory().close();
        }
    }  
}


--------------------
- SimpleEntity.java
--------------------
package entities;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class SimpleEntity {

    @Id @GeneratedValue private Long primaryKey;
    private String fieldA;
    private int fieldB;

    public String getFieldA() {
        return fieldA;
    }

    public void setFieldA(String fieldA) {
        this.fieldA = fieldA;
    }

    public int getFieldB() {
        return fieldB;
    }

    public void setFieldB(int fieldB) {
        this.fieldB = fieldB;
    }

    public Long getPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(Long primaryKey) {
        this.primaryKey = primaryKey;
    }

}

 

 

 

 

 

 

#2

The reason that the refresh operation has no effect in this test is that refresh works by retrieving data from the database and in this test the refreshed object has not been stored yet in the database.

If we change the code and add a flush operation then refresh should work:

            SimpleEntity simpleEntity1 = new SimpleEntity();
            simpleEntity1.setFieldA("entity 1");
            simpleEntity1.setFieldB(1);
            entityManager.persist(simpleEntity1);
            entityManager.flush();

            simpleEntity1.setFieldA("entity 1b");
            entityManager.refresh(simpleEntity1);

Testing this revised test with the last ObjectDB build failed, so a new build was released now in which this revised test passes.

ObjectDB Support

Reply