我有一些JPA注释字段,如下所示:
@Column(name = "SOME_FIELD", updatable = false, nullable = false)
private final String someField;
当实体插入数据库时,这些字段存储在数据库中.它们无法进一步更新.对于Java编程语言,可以将这些字段视为final.
使用EclipseLink静态编织插件,由于某些字节代码检测,可能会延迟加载实体.
我不知道JPA中是否正式允许这样的构造(最终字段),但我喜欢使用它们,因为它们在编译时强制执行这些字段并不是要更新.
在Java 8中,使用这种结构构建的程序运行良好.但是在Java 9中,当涉及EclipseLink静态编织时,我得到以下运行时异常:
Caused by: java.lang.IllegalAccessError: Update to non-static final field xxx.yyy.SomeEntity.someField attempted from a different method (_persistence_set) than the initializer method
这样的错误似乎是由于以下JVM规范:
Otherwise, if the field is final, it must be declared in the current
class, and the instruction must occur in an instance initialization
method () of the current class. Otherwise, an IllegalAccessError
is thrown.
因此,我觉得一些编织工具需要一些更新才能实现这个JVM规范. EclipseLink静态编织工具似乎就是其中之一.
问题:
> JPA中允许使用此处提供的最终字段构造吗?
>是否有一个JDK 9选项来禁止检查最终字段分配,而不仅仅是在类的实例初始化方法()中(作为临时解决方法)?
编辑:
根据JPA规范,JPA中不允许使用最终字段:
The entity class must not be final. No methods or persistent instance variables of the entity class may be final.