java - 有没有办法直接在 ENUM 上声明 @Convert

标签 java jpa

我正在使用@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/

相关文章:

java - 是什么导致我的代码中出现堆栈溢出?

java - Jackson 多态类型处理 - 属性被丢弃

java - JPA如何推断属性的数据类型

java - jCombobox JPA HQL内连接错误

java - 如何避免空表出现 "SELECT max(rank) FROM test"的空结果?

java - 组织.apache.tomcat.dbcp.dbcp.SQLNestedException : Cannot create JDBC driver of class ''

Java:使用数组变量的方法

java - Spring Boot针对不同的h2测试使用不同的data.sql文件

java - 如何使用 Hibernate 获取关联的实体

java - 如何调用从 main 方法返回字符串的静态 String 方法?