Nahradit anotaci OpenJPA @PersistentCollection za @ElementCollection a @Column

Rozhraní OpenJPA vytvoří samostatnou tabulku pro atributy kolekce, které jsou anotovány pomocí org.apache.openjpa.persistence.PersistentCollection. Chcete-li mít stejné chování jako EclipseLink, nahraďte tyto anotace anotacemi @ElementCollection a @Column, jak je definováno ve specifikaci JPA. Pokud odeberete anotaci @PersistentCollection a nepřidáte anotace @ElementCollection a @Column, bude EclipseLink považovat pole jako objekt BLOB a pokusí se vytvořit sloupec pro data v tabulce entity.

Toto pravidlo prochází anotace org.apache.openjpa.persistence.PersistentCollection, takže je můžete ručně migrovat. Chcete-li migrovat anotace, postupujte podle následujícího postupu:

  1. Nahraďte anotaci @org.apache.openjpa.persistence.PersistentCollection za
    @javax.persistence.ElementCollection
    @javax.persistence.Column(name="element")
    
    Pokud v atributu již existuje anotace @Column, nepřidávejte další ani neměňte název.
  2. Změňte pole na kolekce objektů. Namísto primitiv používejte primitivní typy modulů wrapper.

Toto pravidlo například značí anotaci @PersistentCollection v následující třídě entity:


import javax.persistence.Entity;
import javax.persistence.Id;
import org.apache.openjpa.persistence.PersistentCollection;

@Entity
public class IntArrayEntity{
    @Id
    private int id;
    
    @PersistentCollection
    private int[] arrayOfInts;

}

Chcete-li migrovat tuto entitu, nahraďte anotaci a změňte pole int na seznam objektů Integer: List<Integer>.

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class IntArrayEntity{
    @Id
    private int id;
    
    @ElementCollection
    @Column(name="element")
    private List<Integer> arrayOfInts;
}

Informace o tomto problému s migrací OpenJPA na EclipseLink viz příručka Migrace z OpenJPA na EclipseLink JPA: Rozšířená funkčnost.