简体   繁体   English

我怎样才能坚持使用@Formula?

[英]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.我正在使用@Formula计算表的一些属性,我希望它们保持不变,但我无法让它们反映在数据库中,也就是说,它们保持不变。 Is there a way to persist calculated properties?有没有办法保留计算的属性?

You cannot use @Formula annotation for this purpose, because how it's explained in the documentation :您不能为此目的使用@Formula注释,因为它在文档中是如何解释的:

You can use a SQL fragment (aka formula) instead of mapping a property into a column.您可以使用 SQL 片段(又名公式)而不是将属性映射到列中。 This kind of property is read-only (its value is calculated by your formula fragment)这种属性是只读的(它的值是由你的公式片段计算的)

I guess the @ColumnTransformer annotation is what you need.我猜@ColumnTransformer注释是你需要的。

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).这里read的是 sql 片段,它将在password字段初始化期间计算, write的是 sql 片段,将在password字段保存期间计算(结果值将保存在pswd列中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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