问题描述
我有一个数据库,其中有一些带有加密列的表.到目前为止,所有这些列都是String类型的.我正在使用JPA的@Converter
加密和解密表中的列.但是,我也知道,我可以使用Hibernate的@ColumnTransformer
在读写操作时实现相同的加密和解密目标.我的问题是它们有何不同?从速度,可维护性,将来的改进等方面来看,哪种方法更好?现在,@Converter
可以很好地处理String类型的数据.我不确定它是否可以使用Integer类型,或者我将不得不创建一个新的Converter类.
I have a database which has some tables with encrypted columns. As of now, all these columns are of String type. I am using JPA's @Converter
to encrypt and decrypt a column in a table. However, I also know, that I can use Hibernate's @ColumnTransformer
to achieve the same objective of encryption and decryption while read and write operations. My question is how are they different and which is a better approach to use in terms of speed, maintainability, future enhancement, etc? Right now, @Converter
is working fine with String type of data. I am not sure it will work with Integer type or I will have to create a new Converter class.
推荐答案
它们非常相似,并且在许多情况下可以用于完全相同的事物,但是解决方案的实现略有不同.
They're very similar and in many cases can be used for exactly the same thing, but the implementations of the solutions varies a bit.
一个主要区别是一个是Hibernate特定的,另一个是JPA规范的一部分.如果要编写与规范匹配的代码,请使用@Converter
,并且可以在代码中使用除Hibernate之外的其他实现.
One primary difference is that one is Hibernate specific and the other is part of the JPA specification. If you want to write code matching the specification, use @Converter
and you'll be able to use other implementations besides Hibernate with your code.
另一个主要区别是@ColumnTransformer
在数据库级别运行,而@Converter
从数据库中按原样读取值,然后在应用程序端将其转换为其他值.当数据库执行某些操作时,这可能为@ColumnTransformer
提供性能优势.
Another major difference is that @ColumnTransformer
operates on the database level whereas @Converter
reads the value from the database as is, then converts it to something else on the application side. This may provide performance advantages for @ColumnTransformer
as the database performs some operations.
此外,您实际上可以创建自动应用于给定类型的所有值.例如,在您不想在许多地方手动添加@Converter
注释的重构情况下,这可能是有利的.
Additionally you can actually create an AttributeConverter
that applies automatically to all values of a given type. This can be advantageous for example in refactoring situations where you don't want to manually add a @Converter
annotation to many places.
因此它们都可以用来执行相似的操作,并且两者都不比另一个隐式地更好.
So they can both be used to perform similar things and neither is implicitly better than the other.
这篇关于在属性加密和解密方面,JPA的AttributeConverter和Hibernate的ColumnTransformer之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!