是否可以向@Column 注释添加功能? [英] Is it possible to add functionality to the @Column annotation?

查看:32
本文介绍了是否可以向@Column 注释添加功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以向 JPA 中的 @Column 注释添加附加功能.具体来说,我想要做的是使用 @ProtectedColumn 注释标记敏感数据列:这将告诉持久性框架应用某种类型的数据保护(加密、标记化,无论...) 将值存储到实际数据存储中时将其转换为值,然后在从数据存储中读取值时反转该过程.

I'm wondering whether it is possible to add additional functionality to the @Column annotation in JPA. Specifically, what I would like to do is tag columns of sensitive data with an @ProtectedColumn annotation: this would then tell the persistence framework to apply some type of data protection (encryption, tokenization, whatever...) to the values when storing them into the actual data store, and then reverse that process when reading the values from the data store.

所以我可能有一个包含此代码的 Customer 类:

So I might have a Customer class that included this code:

@Column(value="Name")
private String name;

@ProtectedColumn(value="CreditCardNumber", protectionType="ultra")
private String creditCardNumber;

这不是存储实际的信用卡号,而是存储保护类型为ultra"(无论是什么)的信用卡号的保护结果.

Instead of storing the actual credit card number, this would then store the result of protecting the credit card number with the protection type "ultra" (whatever that may be).

显然,我不想重新实现 @Column 注释中已经存在的所有数据库访问功能:我只想扩展它的功能.我知道注释不能直接扩展(参见 为什么不可能在 Java 中扩展注释?),但在我看来,可能在它到达 @Column 注释之前拦截该值,所以也许字段定义如下所示:

Obviously, I don't want to re-implement all the database access functionality already present in the @Column annotation: I just want to extend its functionality. I know that annotations are not directly extensible (see Why is not possible to extend annotations in Java?), but it seems to me that it might be possible to intercept the value before it gets to the @Column annotation, so perhaps the field definition looks like this:

@Protected(protectionType="ultra")
@Column(value="CreditCardNumber")
private String creditCardNumber;

所以我的第一个问题是这在理论上是否可行:如果是这样,我很感激有关如何以这种方式组合/扩展注释的任何指示.

So my first question is whether this is even theoretically possible: if so, I'd appreciate any pointers on how to combine/extend annotations in this way.

推荐答案

您可以使用转换器.例如,您可以像他那样实现一个转换器:使用转换器他使用xml配置.如果你想使用注解,只需看看这个 git 存储库中的这两个 java 类:jpa converter with注释因此,您可以使用注释@Convert(converter = JPACryptoConverter.class)(鉴于 JPACryptoConverter 是 AttributeConverter 的子代).

You can use a converter. For example you can implement a Converter like he did: use converter He uses xml configuration. If you want to use annotations, just have a look at these two java classes in this git repository:jpa converter with annotation Therefore you can use the annotation @Convert(converter = JPACryptoConverter.class) (Given that JPACryptoConverter is a child of AttributeConverter).

这篇关于是否可以向@Column 注释添加功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆