2

At the moment I have an Hibernate entity class as follows:

@Entity
@Table(name = "entity")
public class Entity implements Serializable {

    private static final long serialVersionUID = 2040757598327793105L;

    @Id
    @Column
    private int id;

    @Column
    private String data;    

    @Column(name = "last_modified")
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastModified;
}

I've found that even when the non-timestamp fields are not modified (i.e. the data field) a call to merge still updates the timestamp. I would like the timestamp to only update when other data fields have changed.

Is there anyway I can prevent calls to merge making a SQL UPDATE when all other data fields are not modified, or do I have to explicitly check for this myself in the code?

Ricardo Gladwell
  • 3,770
  • 4
  • 38
  • 59

1 Answers1

3

Update (thanks to comment):

Since v4 of Hibernate @Entity annotation is deprecated and for allowing dynamic updates you should use @DynamicUpdate(true) (in conjunction with @SelectBeforeUpdate(true))


If you want to prevent unmodified fields to be included in UPDATE queries, add this on your entity:

@org.hibernate.annotations.Entity(dynamicUpdate=true) // update only changed fields
public class ...
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Is there not a JPA property for this or do I have to use the hibernate version of the @Entity annotation? – Ricardo Gladwell Jan 11 '12 at 11:23
  • 1
    you'd have to use both. No JPA standard AFAIK – Bozho Jan 11 '12 at 11:51
  • 3
    Just wonder if there is a way I can configure Hibernate to globally enable dynamic update so that I don't have to use the Hibernate annotations. – Ricardo Gladwell Jan 11 '12 at 13:31
  • 2
    Since v4 of Hibernate @Entity annotation is deprecated and for allowing dynamic updates you should use @DynamicUpdate(true) (in conjunction with @SelectBeforeUpdate(true)) – Mariusz Mar 23 '13 at 15:52