将 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 迁移问题的信息,请参阅 Migration from OpenJPA to EclipseLink JPA: Extended Functionality 指南。