我正在尝试在MongoDB中使用Hibernate OGM,但是在访问数据库时遇到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>
错误日志在此处的链接中。 https://pastebin.com/i1zvqQqq
我认为这可能与休眠有关的某些依赖项或依赖项版本有关,因此我尝试添加一些依赖项,但仍然无法正常工作。
答案 0 :(得分:1)
这里的问题是由您使用的Hibernate OGM与pom.xml中提供的Hibernate OGM所需的Hibernate ORM版本之间不匹配引起的。
如果要使用Hibernate OGM 5.1.x,则需要使用Hibernate ORM 5.1.x,请参见此处的兼容性表: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。