【问题标题】:Can't get DataNucleus JDO metadata only mapping to work无法让 DataNucleus JDO 元数据仅映射工作
【发布时间】:2017-07-07 07:51:44
【问题描述】:

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)

我错过了什么?

提前致谢。

编辑:

忘记包含我在src/main/resources/META-INF 中输入的persistence.xml

<?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>

【问题讨论】:

    标签: java xml maven jdo datanucleus


    【解决方案1】:

    我找到了解决办法。

    我在Persistence Guide 中发现,如果您计划仅使用 JDO 元数据,您需要使用 mapping-file 属性为您的持久性单元指定一个 package.jdo 文件,而不是提供实际的类名。

    【讨论】:

    • 因为 是定义一个具有注释的类(来自同一指南)。这也仅适用于您使用persistence.xml IIRC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多