Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Date field is not updated
Date field is not updated [message #758623] Wed, 23 November 2011 23:01 Go to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Howdy Guys!

Just bumped into some interesting stuff. It seems that I cannot update a java.util.Date field (setting a new Date object) if before this setter method I'll try to invoke 'setTime()' on old date object.

I attach the code which shows this behaviour.

I can only assume that it's some kind of prevention mechanism to forbid users to invoke getDateField().setTime(-). In this way, without preparing defensive copy in the getter, user could be able to change the field's state using just the getter.
It seems that EclipseLink is able to find such situations and invalidates all changes made to this field EVEN if the following changes are correct.

Can anyone confirm this behaviour?
Is this JPA specification requirement or just EclipseLink feature?



Entity

@Entity
public class MyEntity {

   @Id
   @GeneratedValue
   private Integer id;

   @Temporal(TemporalType.TIMESTAMP)
   private Date created = new Date();

   public Integer getId() {
      return id;
   }
   
   @Override
   public String toString() {
      return "MyEntity [getId()=" + getId() + ", getCreated()=" + getCreated();
   }
  
   public Date getCreated() {
      // no defensive copy here - return straight reference
      return created;
   } 

   public void setCreated(Date created) {
      this.created = created;
   }
}


Simple service operations (most interesting is the modifyObject(-) method.

@Stateless
public class MyService {

   @PersistenceContext
   EntityManager em;

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public Integer persistObject() {
      MyEntity entity = new MyEntity();
      em.persist(entity);

      System.out.println("[persistObject()]" + entity);

      return entity.getId();
   }

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public MyEntity readObject(Integer id) {
      MyEntity entity = em.find(MyEntity.class, id);

      System.out.println("[readObject()]" + entity);

      return entity;
   }

   @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
   public Integer modifyObject(Integer id) {
      MyEntity entity = em.find(MyEntity.class, id);

      // Try to change date state without changing its reference.
      entity.getCreated().setTime(0L);
      
      // And now try use regular setter to set new date
      Date d = new Date(0L);
      entity.setCreated(d);

      // Pre-update state is correct - Date is 01.01.1970
      System.out.println("[modifyObject()]" + entity);

      return entity.getId();
   }

}


Service methods invocation

Integer id = service.persistObject();
// Date is reflecting 'now' - correct
MyEntity entity = service.readObject(id);

// Pre-update state is correct - 01.01.1970
id = service.modifyObject(id);

// Post-update state is incorrect - back to 'now' instead of 01.01.1970
service.readObject(id);
Re: Date field is not updated [message #758817 is a reply to message #758623] Thu, 24 November 2011 16:05 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

By default EclipseLink assumes Dates are not mutable. This is much more efficient to do.

You can change this by setting,

"eclipselink.temporal.mutable"="true"

In your persistence.xml if you want to modify dates.

You can also use the @Mutable annotation.


James : Wiki : Book : Blog : Twitter
Re: Date field is not updated [message #758828 is a reply to message #758817] Thu, 24 November 2011 16:22 Go to previous messageGo to next message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Thanks James!

So it's EclipseLink decision - not the JPA requirement. Didn't know about @Mutable annotation, tho Smile

Regards.
Re: Date field is not updated [message #758830 is a reply to message #758817] Thu, 24 November 2011 16:22 Go to previous message
Piotr Nowicki is currently offline Piotr NowickiFriend
Messages: 16
Registered: November 2011
Location: Poland
Junior Member
Thanks James!

So it's EclipseLink decision - not the JPA requirement. Didn't know about @Mutable annotation, tho :)

Regards.
Previous Topic:How can I check PersistenceContexts for equality
Next Topic:EclipseLink Maven repository missing
Goto Forum:
  


Current Time: Mon Dec 22 18:30:37 GMT 2014

Powered by FUDForum. Page generated in 0.15208 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software