efejdlek
efejdlek

Reputation: 39

How can i persist with @Formula?

I'm calculating some properties of a table with @Formula, and I want them to persist but I can't get them reflected in the database, that is, they persist. Is there a way to persist calculated properties?

Upvotes: 1

Views: 849

Answers (1)

SternK
SternK

Reputation: 13041

You cannot use @Formula annotation for this purpose, because how it's explained in the documentation:

You can use a SQL fragment (aka formula) instead of mapping a property into a column. This kind of property is read-only (its value is calculated by your formula fragment)

I guess the @ColumnTransformer annotation is what you need.

This is an example from the documentation:

@Column(name = "pswd")
@ColumnTransformer(
    read = "decrypt( 'AES', '00', pswd  )",
    write = "encrypt('AES', '00', ?)"
)
private String password;

Here read is the sql snippet that will be calculated during the password field initialization and write is the sql snippet that will be calculated during the password field saving (the result value will be saved in the pswd column).

Upvotes: 1

Related Questions