我有一些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.