In my customer entity I would like to save a changedByUserId field. I only want to set this field if one of the other fields of customer has really changed. If I allways set it I force EntityManager to update the customer table because I set the changedByUserId field.
@Entity
@Table....
public class Customer {
@Id
private Long cusID;
@Column
private String cusNAME;
@Column
private Date changed;
@Column
private Long changedByUserId;
@PreUpdate
private preUpdate() {
changed = new Date();
// cannot set changedbyUserId here because no entitymanager
// available where i can query the id
}
}
Does anybody know how to tell EntityManager not to check for changes in the changedByUserID field?
You could make a temporary, transient field, which you’d always set to user id, no matter if the entity gets changed or not:
This was entity manager will execute
@PreUpdateonly when one of persistent fields really changed.