Unexpected EclipseLink shared cache behavior with java.util.Date fields

352 Views Asked by At

I'm kicking the tires on EclipseLink(2.3.0) and found something that seems severely wrong. The net of my problem is that when an Entity is returned from a shared cache hit, java.util.Date(and perhaps others) aren't copied as I would expect. This has some pretty nasty consequences as Date fields aren't immutable, and as such can't be shared.... but then again maybe I'm missing something? I'll note that I don't have any properties set, but I am using the -javaagent weaver in a JSE environment.

Test snippet:

// Setup
EntityManager em = emf.createEntityManager();
Person p = new Person(2);
java.util.Date d = new java.util.Date();
p.setDate(d);

em.getTransaction().begin();
em.persist(p);
em.getTransaction().commit();
em.close();

// Get a fresh em
EntityManager em1 = emf.createEntityManager();

Person p1 = em1.find(Person.class, p.getId()); // cache hit
java.util.Date d1 = p1.getDate();

assertFalse(p == p1); // Make sure we have different entities
// The next line fails! For some odd reason different Entities are sharing the same date.
assertFalse(d == d1); // make sure each Entity instance has different date instances
1

There are 1 best solutions below

4
On BEST ANSWER

This is described in the EclipseLink docs here: http://wiki.eclipse.org/Introduction_to_EclipseLink_Application_Development_(ELUG)#Mutability

Short answer: most non serialized Basics are treated as immutable, but can be configured to be treated as mutable using the EclipseLink @Mutable annotation: http://eclipse.org/eclipselink/documentation/2.4/jpa/extensions/a_mutable.htm