Naveen Kocherla
Naveen Kocherla

Reputation: 419

@version annotation not working properly in jpa/eclipselink

I have a base entity class

where one of the fields(Timestamp) has @version attribute, so Jpa will automatically increment the value.

But, sometimes the timestamp value is not getting updated, the edited date is less than the created date, but this should never happen theoretically.

I am not setting the edited value programmatically.

Here is my entity class:

public class BaseEntity implements  Serializable {  

@Column(name = "CREATED")
private Timestamp created;

@Column(name = "EDITED")
@Version
private Timestamp edited;

// other fields, getters and setters

}

Have read many articles about this @version annotation, but I don't understand why this value will not update, what are the reasons behind this.

If some one help me, would be great

Thanks

Upvotes: 0

Views: 1239

Answers (1)

XiCoN JFS
XiCoN JFS

Reputation: 628

Here are some basic guidelines:

  • Check if you imported the right package javax.persistence.Version.
  • Use obj = entityManager.merge(obj); when saving changes to already persisted entities.
  • Never change the value of your version-field manually. The entity-manager will take care of it.

For further debugging edit your persitence.xml and add following lines to properties:

<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
<property name="eclipselink.logging.sql.level" value="FINE"/>

This will enable fine logging and generate a lot of output. You'll be able to see the update queries and what exactly happens.

Upvotes: 3

Related Questions