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

查看:440
本文介绍了是否可以向@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.

推荐答案

您可以使用转换器.例如,您可以像他一样实现一个Converter: 使用转换器 他使用xml配置. 如果要使用批注,只需在git存储库中查看以下两个Java类:使用注释 因此,您可以使用注释 @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天全站免登陆