Hibernate的@Enumerated作为AtomicReference

huangapple 未分类评论43阅读模式
英文:

Hibernate @Enumerated as AtomicReference

问题

我有一个使用Hibernate进行数据库操作的类需要它是线程安全的我正在尝试重构代码使用原子变量及其操作来替代同步方法以避免由于锁定而导致的性能下降问题涉及大量线程),但我在将带有@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);

在这种情况下是否可以实现AtomicReferenceEnumType的唯一选项是`ORDINAL``STRING`,用于类型属性或整数和类型属性或字符串
英文:

I have a class that uses Hiberante for database operations and need it to be thread-safe. I am trying refactor the code to use Atomic Variables with its Operations instead of having a synchronized method due to locking as it is breaking the application due to slow performance (a lot of threads are in play) on a high volume of requests but I am having issues with a variable with @Enumeration to refactor to AtomicReference due to an exception:

> Attribute [com.gateway.domain.ImportTypeDetail.importFrequency] was
> annotated as enumerated, but its java type is not an enum
> [java.util.concurrent.atomic.AtomicReference]

Code before refactor:

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

I refactored to:

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

Is it possible to implement AtomicReference in the scenario? EnumType only options are ORDINAL and STRING for type property or integer and type property or string respectively.

huangapple
  • 本文由 发表于 2020年4月9日 02:10:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61107236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定