我在使用 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);
}
这种方法也没有成功。我有一个例外
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>