Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Development/JPA 2.0/new collection mappings

Element Collections

JPA 2.0 Root | bug 248293

Summary

JPA 2.0 introduces support for collections of Embeddables and primitives through the @ElementCollection annotation. Support for ElementCollection of primitives will be easily supported through our BasicCollection and BasicMap support and will only require annotation processing updates. Support for lists of Embeddables should be supportable through the AggregateCollectionMapping. Support for Maps of Embeddables will be more difficult and will require completion of the Map Collections support feature.

@OrderBy must also be supported on these collection types.

See section 2.1.5, 9.1.2, 9.1.4, 9.1.7, 9.1.11 and 9.1.35 for details.



Functional Requirements

Design

Core changes to accomodate JPA 2.0 requirements

In JPA 2.0 Embeddable may play two distinct roles: it could be either @Embedded or be a target of @ElementCollection. Moreover, the same embeddable class may play these two roles simultaneously in the same persistence unit several times: it may be embedded in one or more master classes and be a target of ElementCollection one or more times (with different CollectionTables).

The core equivalent of the first role is AggregateObjectMapping, which requires the target descriptor to have AggregateDescriptor type; the second is AggregateCollectionMapping which requires the target descriptor to have AggregateCollection type. AggregateObject descriptor is cloned by AggregateObjectMapping targeting it, therefore it could be used by several different aggregating descriptors. AggregateCollectionDescriptor is not cloned by AggregateCollectionMapping and therefore may be a target of only one AggregateCollectionMapping.

To accomodate JPA 2.0 requirements for Embeddable the following changes in the core were made:

  1. The target of AggregateCollectionMapping may be an AggregateDescriptor;
  2. Each AggregateCollectionMapping clones the target descriptor (just like AggregateObjectMapping does) and customizes it:
    1. if the original descriptor type was AggregateObjectDescriptor then clone descriptor type changed AggregateCollectionDescriptor;
    2. optionally maps to each field of the target table(s) a new field (that allows each AggregateCollectionMapping to use it's own target table(s)).

These changes allow to always map JPA 2.0 Embeddable to core AggregateObjectDescriptor.

Note that all original core functionality is still available.

Metadata processing changes

A new metadata processing class, ElementCollectionAccessor will be built. Since we have already provided a solution for supporting direct collections and direct maps (through EclipseLink's @BasicMap and @BasicCollection) with the BasicCollectionAccesor and BasicMapAccessor, will create a new common level class DirectCollectionAccessor which will house the common processing code that will used by all three accessors.

With this re-use we quickly get support for ElementCollection's to basic collections and basic maps. ElementCollectionAccessor will further need to include metadata processing to support the usage of an Embeddable class within the collection. In this scenario, the metadata processing code will build an AggregateCollectionMapping.

The usage of an embeddable also allows for attribute and association overrides to be applied to the element collection (in the same way that they can be specified for an @Embedded mapping). Note: Since embeddable objects can now include nested embeddable objects, attribute and association overrides must support the dot notation name, e.g. 'location.venue'

The dot notation names can also be specified at the entity level to apply to mappings from a mapped superclass. An override from an entity, say 'records.location.venue' will override an attribute override specified in the 'records' attribute with the name 'location.venue'.

Examples

Example 1

Annotations

@ElementCollection
@Column(name="DESIGNATION")
// CollectionTable will default in this case, Entity name + "_" + attribute name
// JoinColumns will default in this case which are different from BasicCollection collection table default
private Collection<String> designations;

XML

<element-collection name="designations">
   <column name="DESIGNATION"/>
</element-collection>

Example 2

Annotations

@ElementCollection
@MapKeyColumn(name="Q_DATE")
@Column(name="QUOTE")
@CollectionTable(name="EXPERT_QUOTES", joinColumns=@JoinColumn(name="EBC_ID"))
public Map<Date, String> getQuotes() {
    return quotes;
}

XML

<element-collection name="quotes">
  <column name="QUOTE"/>
  <map-key-column name="Q_DATE"/>
  <collection-table name="EBC_QUOTES">
    <join-column name="EBC_ID"/>
  </collection-table>
</element-collection>

Example 3

Annotations

// An element collection representing an aggregate collection mapping.
@ElementCollection
// CollectionTable will default
// Column is not applicable here.
// JoinColumns will default.
private Collection<Record> records;
 
...
 
@Embeddable
public class Record {

XML

<element-collection name="records"/>

Testing

Work Required

  1. Develop model for testing
    approx 10 days
  2. Update Annotation Processing
    approx 5 days - processing @ElementCollection annotation / and XML for primitive
    approx 10 days - processing @ElementCollection annotation / and XML for Embeddables
    approx 5 days - integrating Map collections support for Embeddables

Open Issues

  1. How does the new JPA @CollectionTable relate to the existing EclipseLink @CollectionTable and what does it mean for users upgrading from one to the other.

Back to the top