1

我正在尝试为名为 Item 的 JDO 实体创建一个 PK 类。JPA 非常简单,但现在我正在练习 JDO。我正在使用注释配置,这就是这两个类的样子:

    @PersistenceCapable(table="ITEM",identityType = IdentityType.APPLICATION,
    objectIdClass = ItemPK.class,schema="mgr") 
    public class Item {

        @PrimaryKey
        @Persistent(column="code")
        private long code; //WHY public?

        @PrimaryKey
        @Persistent(column="producer")
        private String producer;


        @PrimaryKey
        @Embedded
        private ItemPK id;

        @Persistent(column="price")
        private double price;

        @Persistent(column="name")
        private String name;

        @Persistent(column="description")
        private String description; 

            [... getters/setters...]
    }

我希望将 ItemPK 类用作具有两列(代码、生产者)的主键类。所以这就是这个类的样子:

    @EmbeddedOnly
    @PersistenceCapable(embeddedOnly="true",identityType=IdentityType.APPLICATION)
    public class ItemPK implements Serializable{

        @Persistent
        @PrimaryKey
        public long code;

        @Persistent
        @PrimaryKey
        public String producer;

        @Override
        public String toString() {
                return code+"_"+producer;
        }

        @Override
        public int hashCode() {
        [...Eclipse autogenerated...]
        }

        @Override
            public boolean equals(Object obj) {
        [...Eclipse autogenerated...]
        }
        }

尝试运行代码后得到的结果:

[...Caused  by]
Nested Throwables StackTrace:
Class pl.edu.pw.mini.entity.jdo.Item has been specified with an object-id class pl.edu.pw.mini.entity.jdo.ItemPK which has a field jdoStateManager which isnt Serializable. All non static fields of an objectId class must be serializable.
org.datanucleus.metadata.InvalidPrimaryKeyException: Class pl.edu.pw.mini.entity.jdo.Item has been specified with an object-id class pl.edu.pw.mini.entity.jdo.ItemPK which has a field jdoStateManager which isnt Serializable. All non static fields of an objectId class must be serializable.

据我了解,增强器将 jdoStateManager 添加到 ItemPK,广告它不可序列化。但是由于 ItemPK 是嵌入的,要么它不应该获取 jdoStateManager,要么 JDO 应该知道 jdoStateManager 和常规字段之间的区别。为 2 列主键获取嵌入式类我做错了什么

我不知道如何使这件事起作用,谁能帮助我,并告诉我我在这里做错了什么?

4

1 回答 1

0

文档很好地定义了如何做到这一点 http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/compound_identity.html 并且不涉及使用@Embedded

于 2012-08-02T06:23:33.210 回答