首页 > 解决方案 > 使用 Eclipselink 在 entityManager.clear() 上的字段异常的多个可写映射

问题描述

我们正在定制一个外部 JEE 项目(没有基础源代码修改,只有配置和继承)。我遇到以下异常:

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.6.2.v20151217-774c696): org.eclipse.persistence.exceptions.DescriptorExce
ption
Exception Description: Multiple writable mappings exist for the field [MY_TABLE.ERROR_CODE].  Only one may be defined
 as writable, all others must be specified read-only.
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[errorCodeString-->MY_TABLE.ERROR_CODE]
Descriptor: RelationalDescriptor(my.package.somwhere.DerivedClass --> [DatabaseTable(MY_TABLE)])

Runtime Exceptions:
---------------------------------------------------------

        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:593)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.getAbstractSession(EntityManagerFactoryDelegate.java:205)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryDelegate.createEntityManagerImpl(EntityManagerFactoryDelegate.java:305)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:337)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:303)
        at org.jboss.as.jpa.container.TransactionScopedEntityManager.createEntityManager(TransactionScopedEntityManager.java:187)
        at org.jboss.as.jpa.container.TransactionScopedEntityManager.getEntityManager(TransactionScopedEntityManager.java:91)
        at org.jboss.as.jpa.container.AbstractEntityManager.clear(AbstractEntityManager.java:331)
        at my.code.some.function.somwhere...

问题可能与以下事实有关,即确实有多个列映射ERROR_CODE。这是原始映射:

@Entity
@Table("name="MY_TABLE")
public class BaseClass implements Serializable {
    ...
    @Column(name="TRJ_ERROR_CODE")
    private Integer errorCode;
}

哪个被覆盖,列类型从更改NUMBERVARCHAR2

@Entity
@DiscriminatorValue("X")
@AttributeOverrides({
        @AttributeOverride(name = "errorCode", column = @Column(name = "TRJ_ERROR_CODE", updatable = false, insertable = false))
})
public class DerivedClass extends BaseClass {
   ...
   @Column(name = "TRJ_ERROR_CODE", length = 200)
   private String errorCodeString;
}
<!--ORM.XML-->
    <entity class="my.package.somwhere.DerivedClass">
        <attributes>
            <transient name="errorCode"/>
        </attributes>
    </entity>

该问题仅在enitityManager.clear()通话时发生。持久实体可以正常工作。调试 eclipselink SessionCustomizer 只显示一个映射 - 第二个来自哪里?我怎样才能禁用原来的?我无法找到与 a org.eclipse.persistence.mappings.DirectToFieldMapping、 not aOneToManyOneToOne关系有关的任何示例。

使用此代码段注入实体管理器:

@PersistenceContext(unitName = SomeConstants.PERSISTANCE_JTA_UNIT_CODE, type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;

标签: javajpaormeclipselink

解决方案


推荐阅读