gpt4 book ai didi

java - 无法仅使 DataNucleus JDO 元数据映射工作

转载 作者:行者123 更新时间:2023-11-30 06:37:33 27 4
gpt4 key购买 nike

DataNucleus JDO 映射指南指出:

So you can provide the metadata via annotations solely, or via annotations plus ORM XML Metadata overrides, or via JDO XML Metadata solely, or via JDO XML Metadata plus ORM XML Metadata overrides, or finally via a Metadata API.

强调我的。

我从 JDO tutorial 学习了以下类(class):

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>

最佳答案

我找到了解决方案。

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

关于java - 无法仅使 DataNucleus JDO 元数据映射工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44965432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com