首页 > 解决方案 > Hibernate @Enumerated 为 AtomicReference

问题描述

我有一个使用 Hiberante 进行数据库操作的类,并且需要它是线程安全的。我正在尝试重构代码以使用原子变量及其操作,而不是由于锁定而使用同步方法,因为由于大量请求的性能缓慢(很多线程正在运行)而导致应用程序中断,但我是由于异常,使用 @Enumeration 重构为 AtomicReference 的变量存在问题:

属性 [com.gateway.domain.ImportTypeDetail.importFrequency] 被注释为枚举,但其 java 类型不是枚举 [java.util.concurrent.atomic.AtomicReference]

重构前的代码:

@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private ImportFrequency importFrequency;

我重构为:

@Column(name = Constants.Columns.IMPORT_FREQUENCY)
@Enumerated(EnumType.STRING)
private AtomicReference<ImportFrequency> importFrequency = new AtomicReference<>(ImportFrequency.UNKNOWN);

是否可以在场景中实现 AtomicReference?仅 EnumType 选项分别ORDINAL用于STRING类型属性或整数和类型属性或字符串。

标签: javahibernateatomicatomicreference

解决方案


推荐阅读