使用 MongoDB 的 Hibernate OGM - 创建 EntityManagerFactory 时出现 ClassNotFoundException
Hibernate OGM with MongoDB - ClassNotFoundException when creating EntityManagerFactory
我正在尝试将 Hibernate OGM 与 MongoDB 一起使用,但在访问数据库时遇到 ClassNotFoundException 和 NoClassDefFoundError 问题,我不知道为什么。
这是我的代码
package com.example.model;
import org.hibernate.annotations.Type;
import org.springframework.stereotype.Indexed;
import javax.persistence.*;
@Indexed
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
private String id;
private String firstName;
private String lastName;
public Customer() {};
public Customer(String firstName, String lastName) {
this.setFirstName(firstName);
this.setLastName(lastName);
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
getId(), getFirstName(), getLastName());
}
public String getId(){
return this.id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
这是我访问数据库的地方
package com.example.repository;
import com.example.model.Customer;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;
import java.util.Optional;
@Repository("customerRepository")
public class CustomerNoSQLRepository {
public List<Customer> findAll(){
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );
EntityManager entitymanager = emfactory.createEntityManager();
Query query = entitymanager.createQuery("SELECT * from customer");
List<Customer> list = query.getResultList();
for(Customer e:list) {
System.out.println("Customer NAME :"+e.getFirstName()+" "+e.getLastName());
}
return list;
}
}
这里是persistence.xml
中的依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
这是persistence.xml
<?xml version="1.0"?>
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="ogm-jpa-tutorial" transaction-type="RESOURCE_LOCAL">
<!-- Use Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>test.y.model.Post</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<!--<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />-->
<property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
<property name="hibernate.ogm.datastore.database" value="mongotestdb"/>
<!--<property name="hibernate.ogm.datastore.username" value="admin"/>-->
<!--<property name="hibernate.ogm.datastore.password" value="password"/>-->
</properties>
</persistence-unit>
</persistence>
错误日志在 link 此处。
https://pastebin.com/i1zvqQqq
我认为这可能是关于 hibernate 缺少一些依赖项或依赖项版本,所以我尝试添加一些依赖项但仍然不起作用。
这里的问题是由于您使用的 Hibernate OGM 所需的 Hibernate ORM 版本与您在 pom.xml 中提供的版本不匹配造成的。
如果您想使用 Hibernate OGM 5.1.x,您需要使用 Hibernate ORM 5.1.x,请在此处查看兼容性 table:http://hibernate.org/ogm/releases/5.1。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.15.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.15.Final</version>
</dependency>
或者,更好的是,您可以使用更新版本的 Hibernate OGM。
我正在尝试将 Hibernate OGM 与 MongoDB 一起使用,但在访问数据库时遇到 ClassNotFoundException 和 NoClassDefFoundError 问题,我不知道为什么。
这是我的代码
package com.example.model;
import org.hibernate.annotations.Type;
import org.springframework.stereotype.Indexed;
import javax.persistence.*;
@Indexed
@Entity
@Table(name = "customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Type(type = "objectid")
private String id;
private String firstName;
private String lastName;
public Customer() {};
public Customer(String firstName, String lastName) {
this.setFirstName(firstName);
this.setLastName(lastName);
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
getId(), getFirstName(), getLastName());
}
public String getId(){
return this.id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
这是我访问数据库的地方
package com.example.repository;
import com.example.model.Customer;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import java.util.List;
import java.util.Optional;
@Repository("customerRepository")
public class CustomerNoSQLRepository {
public List<Customer> findAll(){
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "ogm-jpa-tutorial" );
EntityManager entitymanager = emfactory.createEntityManager();
Query query = entitymanager.createQuery("SELECT * from customer");
List<Customer> list = query.getResultList();
for(Customer e:list) {
System.out.println("Customer NAME :"+e.getFirstName()+" "+e.getLastName());
}
return list;
}
}
这里是persistence.xml
中的依赖<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.3.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.ogm</groupId>
<artifactId>hibernate-ogm-mongodb</artifactId>
<version>5.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
这是persistence.xml
<?xml version="1.0"?>
<persistence 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_2_0.xsd"
version="2.0">
<persistence-unit name="ogm-jpa-tutorial" transaction-type="RESOURCE_LOCAL">
<!-- Use Hibernate OGM provider: configuration will be transparent -->
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<class>test.y.model.Post</class>
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<!--<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossStandAloneJtaPlatform" />-->
<property name="hibernate.ogm.datastore.host" value="127.0.0.1"/>
<property name="hibernate.ogm.datastore.port" value="27017"/>
<property name="hibernate.ogm.datastore.create_database" value="true"/>
<property name="hibernate.ogm.datastore.database" value="mongotestdb"/>
<!--<property name="hibernate.ogm.datastore.username" value="admin"/>-->
<!--<property name="hibernate.ogm.datastore.password" value="password"/>-->
</properties>
</persistence-unit>
</persistence>
错误日志在 link 此处。 https://pastebin.com/i1zvqQqq
我认为这可能是关于 hibernate 缺少一些依赖项或依赖项版本,所以我尝试添加一些依赖项但仍然不起作用。
这里的问题是由于您使用的 Hibernate OGM 所需的 Hibernate ORM 版本与您在 pom.xml 中提供的版本不匹配造成的。
如果您想使用 Hibernate OGM 5.1.x,您需要使用 Hibernate ORM 5.1.x,请在此处查看兼容性 table:http://hibernate.org/ogm/releases/5.1。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.1.15.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.1.15.Final</version>
</dependency>
或者,更好的是,您可以使用更新版本的 Hibernate OGM。