The problem that I found is that JPA behavior is that always has to have a change in an entity to trigger a PreUpdate with do a save(). What I want is a way to find a entity in the db and them say update the date using PreUpdate.
My code looks like this:
@NoArgsConstructor
@AllArgsConstructor
@Data
@DynamicUpdate
@Entity
@Table(name = "Table")
public class TableEntity {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "uuid2"
)
@Column(name = "Id", nullable = false)
private String id;
@Column(name = "Something")
private String something;
@Column(name = "Fecha")
private LocalDateTime fecha;
@PreUpdate
private void update() {
setFecha(LocalDateTime.now());
}
}
Repository is blank repository without any custom method. The service that I have has an autowired of the repository. Then I have a method like this:
@Transactional
public InformationDTO save(String id) {
TableEntity entity = capRepository.findById(id);
entity.setSomething("mock");
repo.save(entity);
[...]
return new InformationDTO();
}
If I do this, the PreUpdate will trigger and happy days, but if instead of that, I do this method,
@Transactional
public InformationDTO save(String id) {
TableEntity entity = capRepository.findById(id);
repo.save(entity);
[...]
return new InformationDTO();
}
this won't trigger the PreUpdate.
Is there any way to trigger the PreUpdate without any modification of the entity?
You can just call update
yourself.