Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityManager doesn't refresh the data after querying

My current project uses HSQLDB2.0 and JPA2.0 .

The scenario is: I query DB to get list of contactDetails of person. I delete single contactInfo at UI but do not save that data (Cancel the saving part).

I again do the same query, now the result list is 1 lesser than previous result coz I have deleted one contactInfo at UI. But that contactInfo is still available at DB if I cross check.

But if I include entityManager.clear() before start of the query, I get correct results every time.

I dont understand this behaviour. Could anyone make it clear for me?

like image 755
Satya Avatar asked Mar 14 '11 06:03

Satya


1 Answers

Rather than querying again, try this:

entityManager.refresh(person);

A more complete example:

EntityManagerFactory factory = Persistence.createEntityManagerFactory("...");
EntityManager em = factory.createEntityManager();
em.getTransaction().begin();

Person p = (Person) em.find(Person.class, 1);
assertEquals(10, p.getContactDetails().size()); // let's pretend p has 10 contact details
p.getContactDetails().remove(0);
assertEquals(9, p.getContactDetails().size());

Person p2 = (Person) em.find(Person.class, 1);
assertTrue(p == p2); // We're in the same persistence context so p == p2
assertEquals(9, p.getContactDetails().size());

// In order to reload the actual patients from the database, refresh the entity
em.refresh(p);
assertTrue(p == p2);
assertEquals(10, p.getContactDetails().size());
assertEquals(10, p2.getContactDetails().size());

em.getTransaction().commit();
em.close();
factory.close();
like image 63
Behrang Avatar answered Oct 13 '22 11:10

Behrang