简体   繁体   中英

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM