將 OpenJPA @PersistentCollection 註釋取代為 @ElementCollection 和 @Column

OpenJPA 為集合屬性建立個別表格,這些屬性是以 org.apache.openjpa.persistence.PersistentCollection 標註。若要在 EclipseLink 中有相同的行為,請將這些註釋取代為 @ElementCollection@Column 註釋,如 JPA 規格所定義。如果您移除 @PersistentCollection 註釋,但未新增 @ElementCollection@Column 註釋,EclipseLink 會將該欄位視同 BLOB 來處理,並嘗試為該實體表格中的資料建立直欄。

此規則掃描 org.apache.openjpa.persistence.PersistentCollection 註釋,因此您可以手動移轉它們。 如果要移轉註釋,請執行下列步驟:

  1. @org.apache.openjpa.persistence.PersistentCollection 註釋取代為
    @javax.persistence.ElementCollection
    @javax.persistence.Column(name="element")
    
    如果屬性中已有 @Column 註釋,請不要新增另一個或變更其名稱。
  2. 變更陣列為物件集合。使用基本封套類型而非基本類型。

例如,此規則會標示下列實體類別中的 @PersistentCollection 註釋:


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;

}

若要移轉此實體,請取代該註釋,並將 int 陣列變更為 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;
}

如需此 OpenJPA 至 EclipseLink 移轉問題的相關資訊,請參閱 從 OpenJPA 移轉至 EclipseLink JPA:延伸功能手冊。