How to update a Timestamp property when only some entity fields get modified with JPA and Hibernate

I have an entity lets say Employee as below:

Id(primary key)
Firstname
Lastname
DateofBirth
CreatedDate

I need to update the created date only if there is any change in firstname or lastname or dateofbirth.

Any suggestion on how to implement this.
I am using spring boot with jpa.

Thanks in advance

You have multiple options:

  1. You can update the createdDate in the setters:

     public void setFirstName(String firstName) {
     	this.firstName = firstName;
     	this.createdDate = new Date();
     }
    
  2. You can register a FlushEntityEventListener that checks the modified properties, as explained in this article:

     public class EntityUpdateEventListener
     	implements FlushEntityEventListener {
      
     	@Override
     	public void onFlushEntity(FlushEntityEvent event) throws HibernateException {
     		final EntityEntry entry = event.getEntityEntry();
     		final Object entity = event.getEntity();
     		final boolean mightBeDirty = entry.requiresDirtyCheck( entity );
      
     		if(mightBeDirty && entity instanceof RootAware) {
     			RootAware rootAware = (RootAware) entity;
     			if(updated(event)) {
     				Object root = rootAware.root();
     				root.setCreatedDate(new Date());
     			}
     		}
     	}
      
     	private boolean updated(FlushEntityEvent event) {
     		final EntityEntry entry = event.getEntityEntry();
     		final Object entity = event.getEntity();
      
     		int[] dirtyProperties;
     		EntityPersister persister = entry.getPersister();
     		final Object[] values = event.getPropertyValues();
     		SessionImplementor session = event.getSession();
      
     		if ( event.hasDatabaseSnapshot() ) {
     			dirtyProperties = persister.findModified(
     				event.getDatabaseSnapshot(), values, entity, session
     			);
     		}
     		else {
     			dirtyProperties = persister.findDirty(
     				values, entry.getLoadedState(), entity, session
     			);
     		}
      
     		return dirtyProperties != null;
     	}
     }

Can @UpdateTimestamp also be used?

That applies to any entity modification, not to specific entity attributes.

@vlad : this is exactly what I am looking for, thanks for the useful post.
I did almost same as shown in the below code but some how my updatedDate column value is not set.
am i missing something. Row is getting updated but the date value is not set after sql update .
My Entity class contains variable like

public class TechStatus implements RootAware < TechStatus > {

@Id
private String code;

private String actionDescription;

private Timestamp updatedDate;

@Override
public TechStatus root() {
	return this;
}

}

@Override public void onFlushEntity(FlushEntityEvent flushEntityEvent) throws HibernateException {
		final EntityEntry entry = flushEntityEvent.getEntityEntry();
		final Object entity = flushEntityEvent.getEntity();
		final boolean mightBeDirty = entry.requiresDirtyCheck( entity );
		if(mightBeDirty && entity instanceof RootAware) {
			RootAware rootAware = (RootAware) entity;
			if(updated(flushEntityEvent)) {
				Object root = rootAware.root();
				((TechStatus) root).setUpdatedDate(java.sql.Timestamp.from(java.time.Instant.now()));
			}
		}
	}