Annotation Interface ColumnTransformer


@Target({FIELD,METHOD}) @Retention(RUNTIME) @Repeatable(ColumnTransformers.class) public @interface ColumnTransformer
Specifies custom SQL expressions used to read and write to the column mapped by the annotated persistent attribute in all generated SQL involving the annotated persistent attribute.
  • A write() expression must contain exactly one JDBC-style '?' placeholder.
  • A read() expression may not contain JDBC-style placeholders.

For example:

 @Column(name="credit_card_num")
 @ColumnTransformer(read="decrypt(credit_card_num)"
                    write="encrypt(?)")
 String creditCardNumber;
 

A column transformer write() expression transforms the value of a persistent attribute of an entity as it is being written to the database.

  • If there is a matching read() expression to undo the effect of this transformation, then we're entitled to consider the in-memory state of the Java entity instance as synchronized with the database after a SQL insert or update is executed.
  • On the other hand, if there's no matching read() expression, or if the read expression does not exactly undo the effect of the transformation, the in-memory state of the Java entity instance should be considered unsynchronized with the database after every SQL insert or update is executed.

In the second scenario, we may ask Hibernate to resynchronize the in-memory state with the database after each insert or update by annotating the persistent attribute @Generated(event={INSERT,UPDATE}, writable=true). This results in a SQL select after every insert or update.

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    The name of the mapped column, if a persistent attribute maps to multiple columns.
    A custom SQL expression used to read from the column.
    A custom SQL expression used to write to the column.
  • Element Details

    • forColumn

      String forColumn
      The name of the mapped column, if a persistent attribute maps to multiple columns. Optional if a persistent attribute is mapped to a single column
      Default:
      ""
    • read

      String read
      A custom SQL expression used to read from the column.
      Default:
      ""
    • write

      String write
      A custom SQL expression used to write to the column. The expression must contain exactly one JDBC-style '?' placeholder.
      Default:
      ""