I think the best option for you are EntityListener
using the @PrePersist
and @PreUpdate
annotations, create the configuration for your entity listener and you will get access to each instance that you want to save, this method is being called each time you are trying to persist or update something with hibernate or spring data repositories
public class EntityToPersistListener{
@PrePersist
@PreUpdate
public void methodExecuteBeforeSave(final EntityToPersist reference) {
//Make any change to the entity such as calculation before the save process
reference.setAmount(xxxx)
}
}
You just need to add an annotation above your entity bean
@Entity
@Table(name = "", schema = "", catalog = "")
@EntityListeners(EntityToPersistListener.class)
public class EntityToPersist implements Serializable {
Check this link for further reference
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…