If you use a Long, App Engine will try to create the ID for you, and it will throw the error above if you try to set it yourself. The most straightforward solution here is to use a String like you had originally and add a constant prefix so it's guaranteed to not start with a number.
As an aside, JDO and JPA are both open standards for Java persistence. If you're interested in code portability, you can use either standard. Given the issues that you're having, you may want to experiment with the JDO interface since it's better documented for App Engine. - Jason On Sun, Aug 30, 2009 at 5:38 PM, Larry Cable <[email protected]> wrote: > > the problem is simple: > > @Entity > public class Company implements Serializable { > // ... > public Company(String irsEIN) { > ein = irsEIN; > // ... > } > > @Id String ein; // the IRS EIN for the company ... this is a > unique id and a good one to use for PK? > > // ... > } > > > ... no it isn't because you get an exception when you commit one of > these suckers: > > WARNING: java.lang.IllegalArgumentException: Name may not start with a > digit. > at com.google.appengine.api.datastore.Key.<init>(Key.java:112) > at com.google.appengine.api.datastore.Key.<init>(Key.java:89) > at com.google.appengine.api.datastore.KeyFactory.createKey > (KeyFactory.java:72) > at com.google.appengine.api.datastore.KeyFactory.createKey > (KeyFactory.java:57) > at com.google.appengine.api.datastore.Entity.<init>(Entity.java:114) > at > org.datanucleus.store.appengine.DatastoreFieldManager.storeStringPKField > (DatastoreFieldManager.java:511) > at > org.datanucleus.store.appengine.DatastoreFieldManager.storeStringField > (DatastoreFieldManager.java:421) > at org.datanucleus.state.AbstractStateManager.providedStringField > (AbstractStateManager.java:1023) > at com.charityblossom.dao.entities.Charity.jdoProvideField > (Charity.java) > at com.charityblossom.dao.entities.Charity.jdoProvideFields > (Charity.java) > at org.datanucleus.state.JDOStateManagerImpl.provideFields > (JDOStateManagerImpl.java:2715) > at > org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject > (DatastorePersistenceHandler.java:180) > at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent > (JDOStateManagerImpl.java:3185) > at org.datanucleus.state.JDOStateManagerImpl.flush > (JDOStateManagerImpl.java:4513) > at org.datanucleus.ObjectManagerImpl.flushInternal > (ObjectManagerImpl.java:2814) > at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java: > 2754) > at > org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java: > 2893) > at org.datanucleus.TransactionImpl.internalPreCommit > (TransactionImpl.java:369) > at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:256) > at org.datanucleus.jpa.EntityTransactionImpl.commit > (EntityTransactionImpl.java:104) > > ... > > So, you think, "no problem I actually want it as a number anyway..." > so you change the code like so: > > @Entity > public class Company implements Serializable { > // ... > public Company(Long irsEIN) { > ein = irsEIN; > // ... > } > > @Id Long ein; // the IRS EIN for the company ... this is a unique > id and a good one to use for PK? > > // ... > } > > Problem solved? .... > > WARNING: javax.persistence.PersistenceException: Attempt was made to > manually set the id component of a Key primary key. If you want to > control the value of the primary key, set the name component instead. > at > org.datanucleus.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException > (NucleusJPAHelper.java:264) > at org.datanucleus.jpa.EntityTransactionImpl.commit > (EntityTransactionImpl.java:122) > > Nope! ... > > Now I want to use JPA because I want my code to be portable, so I > really dont want to care about JDO, > DataNucleus or the App Engine Datastore ... > > This exception error message is meaningless to me ... even after > reading the App Engine and DataNucleus > documentation ... > > Also, on a slightly related issue, should I have to obtain an > EntityManager transaction for each instance > of this class I wish to persist? > > I'd appreciate any suggestions anyone might have? > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google App Engine for Java" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en -~----------~----~----~----~------~----~------~--~---
