我正在尝试为名为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列主键获取嵌入式类而做错了什么

我不知道如何使这件事起作用,任何人都可以帮助我,并告诉我我在这里做错了什么吗?

最佳答案

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

09-11 18:32