title: How to get updated values from Spring JPA save with a table that has a trigger
body: <p>The issue I'm having is when I use Spring JPA to persist to a table with a trigger. The trigger populates a field with a default value if that field is NULL on INSERT.</p>
  <pre><code>CREATE TABLE Example (
      ID INTEGER IDENTITY PRIMARY KEY,
      col int
  );
  
  CREATE TRIGGER PUBLIC.TR_INSERT BEFORE INSERT ON PUBLIC.Example
      REFERENCING new ROW AS newrow FOR EACH ROW
  BEGIN ATOMIC
    IF newrow.col is null THEN
    SET newrow.col = 1;
    END IF;
  END
  </code></pre>
  <p>my test, which fails because the value in the entity is still NULL and not the updated value from the trigger.  I'm guessing the save doesn't expect any values to be changing besides the ID.</p>
  <pre><code>@AllArgsConstructor
  @NoArgsConstructor
  @Builder
  @Data
  @Entity
  @Table(name = &quot;example&quot;)
  public class Example {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
  
    @Column(name = &quot;col&quot;)
    private Integer col;
  }
  </code></pre>
  <pre><code>public interface ExampleRepository extends JpaRepository&lt;Example, Long&gt; {
  }
  
  </code></pre>
  <pre><code>@Transactional
  @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  class ExampleRepositoryIntTest {
    @Autowired
    ExampleRepository uut;
  
    @Test
    public void testNullIsChangedToDefault() {
      Example ex = Example.builder().col(null).build();
      Example saved_ex = uut.save(ex);
      assertEquals(&quot;1&quot;, saved.getCol()); // &lt;-- Fails...returned value is &quot;null&quot;
    }
  }
  </code></pre>
  <p>I've been able to persist the data correctly using the JdbcTemplate but that seems like a step backwards.</p>
  <p>I'm using SpringBoot 2.4.5 libs and HSqlDB 2.6</p>