声明的持久性策略“OneToMany”不支持字段类型

时间:2010-03-22 22:08:28

标签: jpa openjpa

我们是JPA的新手并尝试设置一个非常简单的一对多关系,其中一个名为Message的pojo可以有一个由名为GROUP_ASSOC的连接表定义的整数组ID列表。这是DDL:

CREATE TABLE "APP"."MESSAGE" (
        "MESSAGE_ID" INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1)
    );

ALTER TABLE "APP"."MESSAGE" ADD CONSTRAINT "MESSAGE_PK" PRIMARY KEY ("MESSAGE_ID");

CREATE TABLE "APP"."GROUP_ASSOC" (
        "GROUP_ID" INTEGER NOT NULL,
        "MESSAGE_ID" INTEGER NOT NULL
    );

ALTER TABLE "APP"."GROUP_ASSOC" ADD CONSTRAINT "GROUP_ASSOC_PK" PRIMARY KEY ("MESSAGE_ID", "GROUP_ID");

ALTER TABLE "APP"."GROUP_ASSOC" ADD CONSTRAINT "GROUP_ASSOC_FK" FOREIGN KEY ("MESSAGE_ID")
    REFERENCES "APP"."MESSAGE" ("MESSAGE_ID");

这是pojo:

@Entity
@Table(name = "MESSAGE")
public class Message {
    @Id
    @Column(name = "MESSAGE_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int messageId;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)
    private List groupIds;

    public int getMessageId() {
        return messageId;
    }
    public void setMessageId(int messageId) {
        this.messageId = messageId;
    }
    public List getGroupIds() {
        return groupIds;
    }
    public void setGroupIds(List groupIds) {
        this.groupIds = groupIds;
    }
}

我知道这是错误的,因为groupIds属性没有@Column映射到GROUP_ASSOC.GROUP_ID,但希望这说明了我们要做的事情。当我们运行以下测试代码时,我们得到<openjpa-1.2.3-SNAPSHOT-r422266:907835 fatal user error> org.apache.openjpa.util.MetaDataException: The type of field "pojo.Message.groupIds" isn't supported by declared persistence strategy "OneToMany". Please choose a different strategy.

Message msg = new Message();
List groups = new ArrayList();
groups.add(101);
groups.add(102);
EntityManager em = Persistence.createEntityManagerFactory("TestDBWeb").createEntityManager();
em.getTransaction().begin();
em.persist(msg);
em.getTransaction().commit();

帮助!

1 个答案:

答案 0 :(得分:3)

当您使用JPA时,您应该考虑对象和对象之间的关系,您应该将对象模型而不是ids映射到关系模型(可以映射List JPA 2.0中@ElementCollection的基本值,但我之前所说的仍然适用。)

在这里,(假设这确实是MessageGroupAssoc之间的一对多关系,而不是多对多 MessageGroup实体之间的关系)你应该有这样的东西:

@Entity
@Table(name = "MESSAGE")
public class Message implements Serializable {
    @Id
    @Column(name = "MESSAGE_ID")
    @GeneratedValue(strategy = GenerationType.IDENTITY)    
    private Long messageId;

    @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST)    
    private List<GroupAssoc> groupAssocs = new ArrayList<GroupAssoc>();

    public Long getMessageId() {
        return messageId;
    }
    public void setMessageId(Long messageId) {
        this.messageId = messageId;
    }

    public List<GroupAssoc> getGroupAssocs() {
        return groupAssocs;
    }
    public void setGroupAssocs(List<GroupAssoc> groupAssocs) {
        this.groupAssocs = groupAssocs;
    }

    // equals() and hashCode()
}

GroupAssoc的另一个实体。

PS:你的DDL真的看起来像MESSAGEGROUP之间的(M:N)关系(或者我不理解GROUP_ASSOC的PK约束)但是你没有' t显示GROUP_ID上的任何FK约束,所以我不是100%肯定。但如果是这种情况,那么您应该使用@ManyToMany代替@OneToMany