的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 –