猿问

连接 PersistenceManagerFactory 和 Persistence.xml 时出错

我的目标是使用 datanucleus,java 中的 h2 数据库进行 CRUD 操作。但卡在连接 PersistenceManagerFactory 和 persistence.xml


我尝试过不同版本的 datanucleus-core、h2database、datanucleus-api-jdo。我目前参考的是官方文档:http ://www.datanucleus.org/products/accessplatform/jdo/getting_started.html


主代码文件


import javax.jdo.JDOHelper;

import javax.jdo.PersistenceManager;

import javax.jdo.PersistenceManagerFactory;


public class Main {

    public static void main(String[] args)

    {

        PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("Tutorial");

        PersistenceManager pm = pmf.getPersistenceManager();


        pm.currentTransaction().begin();

        try {

            Users user = new Users();

            user.setId(1);

            user.setUser("himmat");

            user.setEmail("xyz@gmail.com");

            user.setMobileNo("7896585687");

            pm.currentTransaction().commit();

        }finally {

            if(pm.currentTransaction().isActive())

            {

                pm.currentTransaction().rollback();

            }

            pm.close();

        }

    }

}


持久性.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>Users</class>

        <exclude-unlisted-classes/>

        <properties>

            <property name="javax.jdo.option.ConnectionURL" value="jdbc:h2:mem:nucleus1"/>

            <property name="javax.jdo.option.ConnectionUserName" value="sa"/>

            <property name="javax.jdo.option.ConnectionPassword" value=""/>

            <property name="datanucleus.schema.autoCreateAll" value="true"/>

        </properties>

    </persistence-unit>

</persistence>


交互式爱情
浏览 162回答 1
1回答

慕勒3428872

您可以使用属性而不是 persistence.xml 实际上,我已经使用属性做了一个类似的例子。另一个问题是你可能缺少一些依赖项,我正在共享 pom.xml。尝试使用你可能会得到结果。如果您使用 Maven,这很容易做到。您还需要针对官方文档中显示的内容进行增强。http://www.datanucleus.org/products/accessplatform/jdo/getting_started.html为此,您需要遵循http://www.datanucleus.org/products/accessplatform_3_2/jdo/enhancer.htmlPOM文件<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <groupId>com.automated</groupId>&nbsp; &nbsp; <artifactId>DemoJDO</artifactId>&nbsp; &nbsp; <version>1.0-SNAPSHOT</version>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.datanucleus</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>datanucleus-api-jdo</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>5.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.h2database</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>h2</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.4.199</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.datanucleus</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>datanucleus-rdbms</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>5.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.jdo</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jdo-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.datanucleus</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>datanucleus-core</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>5.2.1</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.datanucleus</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javax.jdo</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.0-m3</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.datanucleus</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>datanucleus-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.2.0-m3</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <verbose>true</verbose>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </configuration>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <phase>process-classes</phase>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <goal>enhance</goal>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </goals>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </execution>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </executions>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build></project>主文件代码public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Properties properties = new Properties();&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("javax.jdo.PersistenceManagerFactoryClass",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "org.datanucleus.api.jdo.JDOPersistenceManagerFactory");&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("javax.jdo.option.ConnectionDriverName","org.h2.Driver");&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("javax.jdo.option.ConnectionURL","jdbc:h2:~/test");&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("javax.jdo.option.ConnectionUserName","sa");&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("javax.jdo.option.ConnectionPassword","");&nbsp; &nbsp; &nbsp; &nbsp; properties.setProperty("datanucleus.schema.autoCreateAll","true");&nbsp; &nbsp; &nbsp; &nbsp; PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(properties);&nbsp; &nbsp; &nbsp; &nbsp; PersistenceManager pm = pmf.getPersistenceManager();&nbsp;pm.currentTransaction().begin();try {//...//You can use your logic here//...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.currentTransaction().commit();&nbsp; &nbsp; &nbsp; &nbsp; }finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(pm.currentTransaction().isActive())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.currentTransaction().rollback();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pm.close();&nbsp; &nbsp; &nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答