我正在尝试制作数据核,与 Mongodb 一起工作(使用 JDO)。成功后
mvn clean compile
mvn exec:java
当我第一次运行时它失败了
caused by: org.datanucleus.exceptions.NucleusUserException: Found Meta-Data for class com.samples.jdo.mongodb.Account but this class is either not enhanced or you have multiple copies of jdo-api.jar in your CLASSPATH!! Make sure all persistable classes are enhanced before running DataNucleus and/or the CLASSPATH is correct.
但是如果我再次运行它就可以正常工作,并且我可以看到数据也保存在 mongodb 中。
// Create a PersistenceManagerFactory for this datastore
PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("datanucleus.properties", this.getClass().getClassLoader());
JDOMetadata jdomd = pmf.newMetadata();
PackageMetadata pmd = jdomd.newPackageMetadata("com.samples.jdo.mongodb");
ClassMetadata cmd1 = pmd.newClassMetadata(Account.class);
cmd1.setTable("Account").setIdentityType(javax.jdo.annotations.IdentityType.DATASTORE);
cmd1.setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_CAPABLE);
ClassMetadata cmd2 = pmd.newClassMetadata(Login.class);
cmd2.setTable("LOGIN").setIdentityType(javax.jdo.annotations.IdentityType.DATASTORE);
cmd2.setPersistenceModifier(ClassPersistenceModifier.PERSISTENCE_CAPABLE);
PersistenceManager pm = pmf.getPersistenceManager();
Account acct = new Account("firstname","lastname", 3);
Login login = new Login("flastname", "xxxx");
acct.setLogin(login);
final JDOEnhancer enhancer = JDOHelper.getEnhancer();
enhancer.setVerbose(true);
enhancer.registerMetadata(jdomd);
enhancer.setClassLoader(this.getClass().getClassLoader());
String[] classes = {"com.samples.jdo.mongodb.Account","com.samples.jdo.mongodb.Login" };
enhancer.addClasses(classes);
enhancer.enhance();
pmf.registerMetadata(jdomd);
pm.makePersistent(acct);
有任何想法吗?
谢谢