0

I have an entity that has a date field annotated with @Future. I create the record/transaction initially with a given date (let's say tomorrows date 5/31/21) using repository.save(data). - Works Fine!

public class data{

  @Future
  private Date date;

  private String status;

}

5/31/2021

data data = new data(LocalDateTime.now(), "initialstatus")
repository.save(data);

I then come in on 6/1/21 and update a different field called "status", but when I go to make the update I get an error that the transaction can't be committed, and I am guessing because it's trying to save the 5/31/21 date in the @Future date field on 6/1/21 (which is not a future date at this point) while trying to update the status field.

6/1/2021

data data = repository.findById(id).get();
data.setStatus("updatedstatus");
repository.save(data);
2021-05-30 09:46:13.597 DEBUG 10984 --- 
[nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolved
[org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; 
nested exception is javax.persistence.RollbackException: 
Error while committing the transaction]

Once I remove the @Future, I can update the status field, so my question is how can I update just the status field and not the date field where it's failing because the date is now in the past.

jarlh
  • 42,561
  • 8
  • 45
  • 63
New Developer
  • 61
  • 1
  • 8

3 Answers3

0

Seems like this can be done if you need to validate only on insert using validation groups. Does this answer your question: Hibernate validations on save (insert) only?

  • Yeah, groups could work, I just didn't want to have to spin up a group to do a simple thing (or at least what I was hoping was simple) – New Developer May 30 '21 at 17:27
0

Try to set updatable attribute of the @Column annotation to false:

@Column(updatable= false)
@Future
private Date date;

The updatable attribute instruct Hibernate to omit this column from the generated UPDATE SQL statement.

Alien
  • 15,141
  • 6
  • 37
  • 57
0

You can use @DynamicUpdate. Entity will use dynamic sql generation where only changed columns get referenced.

@DynamicUpdate
public class data{

  @Future
  private Date date;

  private String status;

}