Search code examples
javakeyjdodatanucleuscomposite

Composite Keys in DataNucleus JDO implementation (field jdoStateManager)


I'm trying to create a PK class for a JDO Entity named Item. It's was soo simple with JPA, but now im practicing JDO. I'm using anotation configuration and this is how the two classes look like:

    @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...]
    }

I want the ItemPK class to be used as a Primary Key class with thoose two columns (code,producer). So this is how the class looks like:

    @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...]
        }
        }

What I do get after trying to run the code:

[...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.

As I understand the enhancer adds jdoStateManager to a ItemPK, ad it is not Serializable. But as ItemPK is embedded, either it should not get the jdoStateManager, or JDO should know the difference between jdoStateManager and a regular field. What am I doing wrong to get an embedded class for a 2-column Primary Key

I have no Idea how to make this thing work, can anyone help me, and tell me what am I doing wrong here?


Solution

  • The docs define perfectly well how to do that http://www.datanucleus.org/products/accessplatform_3_1/jdo/orm/compound_identity.html and it doesn't involve use of @Embedded