Rafael Ruiz Tabares
Rafael Ruiz Tabares

Reputation: 741

Spring JPA/ Hibernate - how update when object is not modified

I can't update the entity because there are no changes but I need update its column updated o be used as tracking column. Below update entity column which I wish updating automatically even there is no changes.

Entity

@Column( name = "UPDATED" , nullable = true )
@DateTimeFormat( iso = ISO.DATE_TIME )
private LocalDateTime updated;

@PreUpdate
public void updatedEntry(){
    this.updated = LocalDateTime.now();
}

Spring JPA repository

@Repository
public interface ProductRepository extends PagingAndSortingRepository<Product, BigDecimal>
{}

And below code is where I update values although sometimes the object won't modified because retrieved values are the same.

product.setBarcode( clfProduct.getBarcode() );
product.setTaxcode( clfProduct.getTaxcode() );
product.setPrice( clfProduct.getPrice() );
product.setMinOrder( clfProduct.getMinOrder() );
product.setMsrpPrice( clfProduct.getMsrpPrice() );
product.setStock( stock );

productRepository.save(product);

Upvotes: 1

Views: 866

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81988

Just make the updated column a normal column you update, instead of using the @PreUpdate annotation.

Upvotes: 1

Related Questions