gpt4 book ai didi

glassfish-3 - 将 DataNucleus 与 Glassfish 3.x 结合使用

转载 作者:行者123 更新时间:2023-12-05 01:13:47 26 4
gpt4 key购买 nike

我在使用 DataNucleus 作为 Glassfish AS 上的 ORM 框架时遇到了问题。实际上我正在尝试使用连接到 MongoDb。我预计这里会出现问题,因为每个人都知道 nosql 数据库不是标准的一部分,但我遇到了使用 DataNucleus 的问题。这是我没想到的。我对 DataNucleus 和 MongoDB 都不熟悉,不幸的是我未能在 WEB 上找到任何相关信息,所以我决定逐步进行。

我创建了一个小型命令行应用程序,它使用 DataNucleus 和 MongoDB 并使用 Maven 构建。相关代码如下

坚持.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="just_test">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>

<properties>
<property name="datanucleus.ConnectionURL" value="mongodb:/justtest"/>
<property name="datanucleus.autoCreateSchema" value="true"/>
<property name="datanucleus.validateTables" value="false"/>
<property name="datanucleus.validateConstraints" value="false"/>
</properties>
</persistence-unit>
</persistence>

持久化实体的代码

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;


public class App {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("just_test");

// Create entity manager
EntityManager em = null;
String id;
try {
em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
TestEntity testEntity = new TestEntity();
tx.begin();
em.persist(testEntity);
em.flush();
tx.commit();
id = testEntity.getId();
} finally {
if (null != em) {
em.close();
}
}

try {
em = emf.createEntityManager();
TestEntity testEntity = em.find(TestEntity.class, id);
System.out.println(testEntity);
} finally {
if (null != em) {
em.close();
}
}
}

pom.xml

...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>[2.9.3, )</version>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
<version>[2.9, )</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-mongodb</artifactId>
<version>[2.9, )</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jpa</artifactId>
<version>[3.0, )</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jdo</artifactId>
<version>[3.0, )</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>[1.0, )</version>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo2-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>transaction-api</artifactId>
<version>[1.1-rev-1, )</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0b</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.datanucleus</groupId>
<artifactId>maven-datanucleus-plugin</artifactId>
<version>3.1.0-m3</version>
<configuration>
<log4jConfiguration>${basedir}/log4j.properties</log4jConfiguration>
<verbose>true</verbose>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...

这段代码工作正常。

一旦我尝试部署使用相同代码的应用程序,发生的第一件事就是 GF 无法找到某些类。不过,这些类位于 .ear 文件中。我不得不将几个类放到 GF_HOME/glassfish/domains//lib 目录中以使 GF 能够找到这些类,但最后我以异常告终

Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named just_test

值得一提的是,org.datanucleus.api.jpa.PersistenceProviderImpl 类在我也放在域的 lib 目录中的 datanucleus-api-jpa 文件中。

我也试过像下面这样用 CDI 来做

@PersistenceContext(unitName = "just_test")
protected EntityManager entityManager;

@Override
public void persist(TestEntity testEntity) {
entityManager.persist(testEntity);
}

这个方法也没有成功。我有一个异常(exception)

java.lang.ClassCastException: Cannot cast org.datanucleus.api.jpa.PersistenceProviderImpl to javax.persistence.spi.PersistenceProvider

任何想法或指向文章和/或文档的链接将不胜感激。谢谢

下面是我用于尝试在 Glassfish 上部署的应用程序的依赖项列表。我必须将所有具有“提供”范围的依赖项放入 Glassfish 的 lib 目录中

<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
<version>[2.9, )</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-mongodb</artifactId>
<version>[2.9, )</version>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jpa</artifactId>
<version>[3.0, )</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jpa_2.0_spec</artifactId>
<version>[1.0, )</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jdo</artifactId>
<version>[3.0, )</version>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo2-api</artifactId>
<version>2.2</version>
</dependency>

最佳答案

我设法解决了它。我必须修改依赖项,如下所示

<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<datanucleus.version>3.1.3</datanucleus.version>
</properties>

<dependencies>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-core</artifactId>
<version>${datanucleus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-mongodb</artifactId>
<version>${datanucleus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jpa</artifactId>
<version>${datanucleus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.datanucleus</groupId>
<artifactId>datanucleus-api-jdo</artifactId>
<version>${datanucleus.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.jdo</groupId>
<artifactId>jdo2-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>

我还必须将所有具有“提供”范围的 jar 放入 GF_HOME/glassfish/domains//lib 目录。我不知道为什么我必须这样做。很高兴得到这个答案。希望这可以为想要将 Datanucleus 与 Glassfish 一起使用的人节省时间

关于glassfish-3 - 将 DataNucleus 与 Glassfish 3.x 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13501960/

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