的DataNucleus将JDO映射指南指出:无法获得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>
,并把它放在src/main/resources/META-INF
(as per documentation)在我的Maven项目。
现在我不清楚我是否需要执行与元数据增强步骤只为好,但是当我做我得到以下输出:
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)
w ^我错过了什么?
在此先感谢。
编辑:
漏填了persistence.xml
,我把在src/main/resources/META-INF
:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1">
<!-- JDO tutorial "unit" -->
<persistence-unit name="Tutorial">
<class>org.datanucleus.samples.jdo.tutorial.Inventory</class>
<class>org.datanucleus.samples.jdo.tutorial.Product</class>
<class>org.datanucleus.samples.jdo.tutorial.Book</class>
<exclude-unlisted-classes/>
<properties>
<property name="javax.jdo.option.ConnectionDriverName" value="org.postgresql.Driver"/>
<property name="javax.jdo.option.ConnectionURL" value="jdbc:postgresql://host/postgres"/>
<property name="javax.jdo.option.ConnectionUserName" value="user"/>
<property name="javax.jdo.option.ConnectionPassword" value="secret"/>
<property name="datanucleus.schema.autoCreateAll" value="true"/>
</properties>
</persistence-unit>
</persistence>
,因为用于定义具有注释的类(来自同一指南)。此外,这只适用于使用'persistence.xml' IIRC –