EclipseLink を使って、メール送信フォームを実装していたところ、
ボタン連打で、以下のエラーが発生しました。
対処したので、対処方法を記載します。
EclipseLink の Version
- EclipseLink 2.6.3
<!-- Eclipse Link -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.3</version>
</dependency>
エラーの内容
Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.6.3.v20160428-59c81c5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [xyz.monotalk.festivals4partypeople.models.rdb.entity.ContactMailTaskManage] is mapped to a primary key column in the database. Updates are not allowed.
at org.eclipse.persistence.exceptions.ValidationException.primaryKeyUpdateDisallowed(ValidationException.java:2551)
at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.writeFromObjectIntoRowWithChangeRecord(AbstractDirectMapping.java:1265)
at org.eclipse.persistence.internal.descriptors.ObjectBuilder.buildRowForUpdateWithChangeSet(ObjectBuilder.java:1769)
EntityManager に persist 要求が積まれたが、persist する前に、次の persist 要求が積まれて、
key 値採番が重複して発生しているように思われました。1
[1] key 値の採番のロジックによっては発生しないかもしれません。
@GeneratedValue(strategy = GenerationType.IDENTITY)
をつけたEntity でデフォルト設定の EclipseLink で発生しました。
対処方法
persistance.xml に以下を追加しました。
ウィービングによる内部最適化 を 無効にするようです。2
[2] ウィービングというのがそもそもなんだかよくわかりませんが。。
<property name="eclipselink.weaving.internal" value="false"/>
しかし、動作確認したところ、発生確率は下がりましたが、未だエラーとなります。
EntityManager#clear() を persist() の後に実行するようにしたところ、 エラーが発生しなくなりました。
参考記事
-
java - Merge an entity, change its id, merge again, cause “mapped to a primary key column in the database. Updates are not allowed” error - Stack Overflow
eclipselink.weaving.internal
の参考記事 -
EclipseLink JPA『is mapped to a primary key column in the database. Updates are not allowed.』
EntityManage#clear() の参考記事
たまにJPAはハマりポイントがあるなと思います。
以上です。
コメント