我正在使用@Convert(converter = CoverConverter.class)
在将属性值存储到数据库之前转换属性值,反之亦然。我必须在使用特定 ENUM 的每个地方声明此注释。我们称之为TestENUM
目前。
但是这个特定的 ENUM 在 100 多个地方使用,我不确定我组中的其他开发人员在使用这个 ENUM 时是否会记得添加这个注释。
所以我想知道是否有一种方法可以直接注释 ENUM,无论在哪里使用它,它都会在持久化到数据库之前进行转换,反之亦然,当通过 JPA 从数据库获取时反之亦然
例如:
@Convert(convert = true)
public enum TestENUM {}
最佳答案
您应该能够创建转换器并使用 autoApply 属性:
If the autoApply element is specified as true, the persistence provider must automatically apply the converter to all mapped attributes of the specified target type for all entities in the persistence unit except for attributes for which conversion is overridden by means of the Convert annotation (or XML equivalent).
http://docs.oracle.com/javaee/7/api/index.html?javax/persistence/Convert.html
@Converter(autoApply = true)
public class MyEnumConverter implements Converter{
}
关于java - 有没有办法直接在 ENUM 上声明 @Convert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351968/