2

I have the following JPA map setup using EclipseLink 2.5.1

@Entity
@Table(name = "ACCOUNTS")
public class Account extends AbstractAggregateRoot<Long> {
...
    private AccountMetadata metadata = new AccountMetadata();
}

@Embeddable
@Access(AccessType.PROPERTY)
public class AccountMetadata extends Metadata {

    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name="ACCOUNT_METADATA",
            joinColumns=@JoinColumn(table="ACCOUNTS", referencedColumnName="ID", name="ACCOUNT_ID"))
    @Column(name= "MD_VALUE")
    public Map<Key,String> getMetadata() {
        return this.metadata;
    }

    public void setMetadata(Map<Key,String> md) {
        this.metadata = md;
    }
}

@Embeddable
public class Metadata implements Serializable {

....

    protected Map<Key,String> metadata = new HashMap<>();

    @Embeddable
    @Access(AccessType.FIELD)
    public static class Key {
        @Enumerated(EnumType.STRING)
        @Column(name = "KEY_NS")
        private Namespace ns;
        @Column(name = "KEY_NAME")
        private String name;

    ...

    }    
}

When caching is enabled everything maps fine. When I disable caching by adding the following to the persistence XML

<shared-cache-mode>NONE</shared-cache-mode>

I receive the following error when attempting to commit.

Caused by: Exception [EclipseLink-69] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A NullPointerException was thrown while extracting a value from the instance variable [ns] in the object [oracle.cloudstorage.common.domain.Metadata$Key].
Internal Exception: java.lang.NullPointerException
Mapping: org.eclipse.persistence.mappings.DirectToFieldMapping[ns-->KEY_NS]
Descriptor: RelationalDescriptor(oracle.cloudstorage.common.domain.Metadata$Key --> [])
    at org.eclipse.persistence.exceptions.DescriptorException.nullPointerWhileGettingValueThruInstanceVariableAccessor(DescriptorException.java:1277)
    at org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor.getAttributeValueFromObject(InstanceVariableAttributeAccessor.java:88)
    at org.eclipse.persistence.mappings.DatabaseMapping.getAttributeValueFromObject(DatabaseMapping.java:615)
    at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.compareObjects(AbstractDirectMapping.java:407)
    at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.compareForChange(AbstractDirectMapping.java:381)
    at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.createObjectChangeSetThroughComparison(DeferredChangeDetectionPolicy.java:186)
    at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.createObjectChangeSet(DeferredChangeDetectionPolicy.java:146)
    at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.calculateChanges(DeferredChangeDetectionPolicy.java:91)
    at org.eclipse.persistence.descriptors.changetracking.DeferredChangeDetectionPolicy.calculateChangesForExistingObject(DeferredChangeDetectionPolicy.java:56)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.calculateChanges(UnitOfWorkImpl.java:664)
    at org.eclipse.persistence.internal.sessions.RepeatableWriteUnitOfWork.writeChanges(RepeatableWriteUnitOfWork.java:438)
    at org.eclipse.persistence.internal.jpa.EntityManagerImpl.flush(EntityManagerImpl.java:863)

Has anyone else experienced this problem? I have tried different caching parameters and the only one that seems to work is enabling cache

<shared-cache-mode>DISABLE_SELECTIVE</shared-cache-mode>

and adding this to the top of the entity

@Cache(refreshAlways=true)
shoop
  • 21
  • 3

1 Answers1

0

I had the same problem. It seems that Eclipselink is very buggy whe working with @ElementCollection of @Embedded.

Probably the implementation relies on internal cache to access that objects instead of keeping them in UnitOfWork, thus if you disable the cache you will obtain NPE. Enabling of cache does not guarantee that you will not obtain NPE: it also thrown when working with large datasets.

I've submitted 2 BUGs related to this problem: https://bugs.eclipse.org/bugs/show_bug.cgi?id=441498 https://bugs.eclipse.org/bugs/show_bug.cgi?id=442228

For the moment a work-around is only use @ElementCollections for primitive member types and map objects with @OneToMany.

Atul Dwivedi
  • 1,452
  • 16
  • 29