DataNucleus JDO制图指南声明:
因此,您可以单独或通过注释提供元数据 注释加上ORM XML元数据覆盖,或通过JDO XML元数据 完全,或通过JDO XML元数据加ORM XML元数据覆盖,或 最后通过元数据API。
强调我的。
我从JDO tutorial获取了以下课程:
package org.datanucleus.samples.jdo.tutorial;
public class Product
{
String name = null;
String description = null;
double price = 0.0;
public Product(String name, String desc, double price)
{
this.name = name;
this.description = desc;
this.price = price;
}
}
并创建了以下package.jdo
:
<?xml version="1.0" encoding="utf-8"?>
<jdo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/jdo/jdo"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/jdo/jdo http://xmlns.jcp.org/xml/ns/jdo/jdo_3_1.xsd"
version="3.1">
<package name="org.datanucleus.samples.jdo.tutorial">
<class name="Product" identity-type="datastore" table="product">
<inheritance>
<discriminator strategy="class-name"/>
</inheritance>
<datastore-identity>
<column name="product_ID"/>
</datastore-identity>
<field name="name">
<column name="name" jdbc-type="STRING" allows-null="true"/>
</field>
<field name="description">
<column name="description" jdbc-type="STRING" allows-null="true"/>
</field>
<field name="price">
<column name="price" jdbc-type="DOUBLE" allows-null="true"/>
</field>
</class>
</package>
</jdo>
并将其放入我的maven项目中的src/main/resources/META-INF
(as per documentation)。
现在我不清楚是否需要仅使用元数据执行增强步骤,但是当我这样做时,我得到以下输出:
DataNucleus Enhancer completed with success for 0 classes. Timings : input=16 ms, enhance=0 ms, total=16 ms. Consult the log for full details
DataNucleus Enhancer completed and no classes were enhanced. Consult the log for full details
我写了这个测试应用程序:
package org.datanucleus.samples.jdo.tutorial;
import javax.jdo.JDOHelper;
import javax.jdo.Transaction;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
public class Test {
public static void main(String[] args) {
PersistenceManagerFactory factory;
factory = JDOHelper.getPersistenceManagerFactory("Tutorial");
PersistenceManager manager = factory.getPersistenceManager();
Transaction t = manager.currentTransaction();
try {
t.begin();
Product obj = new Product("Test Product", "Test Product Description", 9.99);
manager.makePersistent(obj);
t.commit();
} finally {
if (t.isActive()) {
t.rollback();
}
manager.close();
}
}
}
并在使用mvn exec:java
运行时遇到这些错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:294)
at java.lang.Thread.run (Thread.java:748)
Caused by: org.datanucleus.api.jdo.exceptions.ClassNotPersistenceCapableException: The class "org.datanucleus.samples.jdo.tutorial.Product" is not persistable. This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data/annotations for the class are not found.
at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException (NucleusJDOHelper.java:473)
at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent (JDOPersistenceManager.java:717)
at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent (JDOPersistenceManager.java:738)
at org.datanucleus.samples.jdo.tutorial.Test.main (Test.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:294)
at java.lang.Thread.run (Thread.java:748)
Caused by: org.datanucleus.exceptions.ClassNotPersistableException: The class "org.datanucleus.samples.jdo.tutorial.Product" is not persistable. This means that it either hasnt been enhanced, or that the enhanced version of the file is not in the CLASSPATH (or is hidden by an unenhanced version), or the Meta-Data/annotations for the class are not found.
at org.datanucleus.ExecutionContextImpl.assertClassPersistable (ExecutionContextImpl.java:5113)
at org.datanucleus.ExecutionContextImpl.persistObjectInternal (ExecutionContextImpl.java:1887)
at org.datanucleus.ExecutionContextImpl.persistObjectWork (ExecutionContextImpl.java:1830)
at org.datanucleus.ExecutionContextImpl.persistObject (ExecutionContextImpl.java:1685)
at org.datanucleus.api.jdo.JDOPersistenceManager.jdoMakePersistent (JDOPersistenceManager.java:712)
at org.datanucleus.api.jdo.JDOPersistenceManager.makePersistent (JDOPersistenceManager.java:738)
at org.datanucleus.samples.jdo.tutorial.Test.main (Test.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:498)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:294)
at java.lang.Thread.run (Thread.java:748)
我错过了什么?
提前致谢。
编辑:
忘记包含我放入persistence.xml
的{{1}}:
src/main/resources/META-INF
答案 0 :(得分:1)
我找到了解决方案。
我在Persistence Guide中发现,如果您打算只使用JDO元数据,则需要使用package.jdo
属性为持久性单元指定mapping-file
文件,而不是提供实际的类名。