info.unterrainer.commons.httpserver.daos.UpdateQueryBuilder Maven / Gradle / Ivy
package info.unterrainer.commons.httpserver.daos;
import java.time.LocalDateTime;
import java.util.function.Function;
import jakarta.persistence.EntityManager;
import info.unterrainer.commons.jreutils.DateUtils;
import info.unterrainer.commons.rdbutils.Transactions;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class UpdateQueryBuilder
extends BasicQueryEntityManagerBuilder
> {
@Getter
protected final BasicJpqlDao
dao;
protected final P entity;
protected V withEntityManager(final Function func) {
if (entityManager == null)
return Transactions.withNewTransactionReturning(dao.emf, em -> func.apply(em));
return func.apply(entityManager);
}
/**
* Update the given entity.
*
* @return the entity after the update
*/
public P execute() {
return withEntityManager(em -> {
LocalDateTime time = DateUtils.nowUtc();
entity.setEditedOn(time);
return em.merge(entity);
});
}
}