本文介绍了如何在运行时获得DiscriminatorValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
We have the following classes
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) // optional annotation as this is default
@DiscriminatorColumn(name = "apType", discriminatorType = DiscriminatorType.STRING, length = 255)
@DiscriminatorValue("AP")
public class ApplicationProcess {
}
而且
And this
@Entity
@DiscriminatorValue("APS")
public class ApplicationProcessScheme extends ApplicationProcess {
}
现在我需要在运行时知道 ApplicationProcess
是否为 DiscriminatorValue
AP或APS
。
因为这是由jpa自动处理的,所以我无法得到这个值。
Now I need to know at runtime if the ApplicationProcess
is of DiscriminatorValue
AP or APS
.Since this is automatically handled by jpa, I have no way of getting this value.
我们调用一个方法,它需要一个 ApplicationProcess
作为参数,我想避免使用 instanceof
来检查它是什么类型。如果我可以做一些类似的事情,会更酷。
We are calling a method that takes an ApplicationProcess
as parameter, and I want to avoid using instanceof
to check what type it is. Would be cooler if I could do something like
applicationProcess.getApType().equals("AP");
推荐答案
您可以将您的鉴别器映射为只读属性:
You can map your discriminator as a read-only property:
public class ApplicationProcess {
...
@Column(name = "apType", insertable = false, updatable = false)
private String apType;
}
这篇关于如何在运行时获得DiscriminatorValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!