我很困惑——我已经好几年没有使用过 Hibernate 了,然后就再也没有使用过 Spring Boot。 Spring Boot,但从未与 Hibernate 或 JPA 一起使用。所以我想弄清楚如何让它为我的工作工作 - 我应该在周一演示一些东西,如果我能让“这个”工作,我会将其复制到我的工作笔记本电脑上并更改当然是细节。顺便说一句 - 这是我收到的消息 - 我必须在标题中缩短它:
“类 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider 未实现请求的接口 javax.persistence.spi.PersistenceProvider”
我有“主”类 - TestWebApplication:
package net.draconia.testWeb;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
@SpringBootApplication(scanBasePackages = {"com.draconia.testWeb.controller"})
public class TestWebApplication
{
@Bean
public DataSource getDatasource()
{
BasicDataSource objDatasource = new BasicDataSource();
objDatasource.setDriverClassName("com.mysql.jdbc.Driver");
objDatasource.setUrl("jdbc:mysql://localhost:3306/Test");
objDatasource.setUsername("root");
objDatasource.setPassword("R3g1n@ M1lL$ 1$ My Qu3eN!");
return(objDatasource);
}
@Bean
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory()
{
LocalContainerEntityManagerFactoryBean objEntityManager = new LocalContainerEntityManagerFactoryBean();
objEntityManager.setDataSource(getDatasource());
objEntityManager.setPackagesToScan(new String[] { "net.draconia.testWeb.beans" });
JpaVendorAdapter objVendorAdapter = new HibernateJpaVendorAdapter();
objEntityManager.setJpaVendorAdapter(objVendorAdapter);
objEntityManager.setJpaProperties(getHibernateProperties());
return(objEntityManager);
}
protected Properties getHibernateProperties()
{
Properties objHibernateProperties = new Properties();
objHibernateProperties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
objHibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
return(objHibernateProperties);
}
@Bean
public JpaTransactionManager getHibernateTransactionManager()
{
JpaTransactionManager objTransactionManager = new JpaTransactionManager();
objTransactionManager.setEntityManagerFactory(getEntityManagerFactory().getObject());
return(objTransactionManager);
}
public static void main(String[] args)
{
SpringApplication.run(TestWebApplication.class, args);
}
}
,实体bean:
package net.draconia.testWeb.beans;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity(name = "Books")
public class Book
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer miId;
@Column(columnDefinition = "varchar(200) not null", insertable = true, length = 200, name = "BookName", nullable = false, table = "Books", unique = false, updatable = true)
private String msBookName;
@Column(columnDefinition = "varchar(100) not null", insertable = true, length = 100, name="Author", nullable = false, table = "Books", unique = false, updatable = true)
private String msAuthor;
public String getAuthor()
{
if(msAuthor == null)
msAuthor = "";
return(msAuthor);
}
public String getBookName()
{
if(msBookName == null)
msBookName = "";
return(msBookName);
}
public int getId()
{
if(miId == null)
miId = 0;
return(miId);
}
public void setAuthor(final String sAuthor)
{
if(sAuthor == null)
msAuthor = "";
else
msAuthor = sAuthor;
}
public void setBookName(final String sBookName)
{
if(sBookName == null)
msBookName = "";
else
msBookName = sBookName;
}
public void setId(final Integer iId)
{
if(iId == null)
miId = 0;
else
miId = iId;
}
}
, DAOConcrete 类(该接口只是一种合乎逻辑的方法,但如果您愿意,我也会发布它):
package net.draconia.testWeb.dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import net.draconia.testWeb.beans.Book;
@Repository("bookDAO")
public class BookDAOImpl implements BookDAO
{
@Autowired
private EntityManagerFactory mObjEntityManagerFactory;
public List<Book> getAllBooks()
{
EntityManager objEntityManager = getEntityManagerFactory().createEntityManager();
List<Book> lstBooks = objEntityManager.createQuery("from Books", Book.class).getResultList();
return(lstBooks);
}
protected EntityManagerFactory getEntityManagerFactory()
{
return(mObjEntityManagerFactory);
}
}
,以及 REST 端点/MVC 控制器的控制器类:
package net.draconia.testWeb.controllers;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import net.draconia.testWeb.beans.Book;
import net.draconia.testWeb.dao.BookDAO;
@Controller
public class TestController
{
@Autowired
private BookDAO mObjDAO;
@GetMapping("/Books")
public List<Book> getBooks()
{
return(getDAO().getAllBooks());
}
protected BookDAO getDAO()
{
return(mObjDAO);
}
}
POM 文件放在这里只是为了完整性,但我认为这不一定是问题,除非我缺少依赖项:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>net.draconia</groupId>
<artifactId>testWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testWeb</name>
<description>Demo project for Spring Boot</description>
<properties>
<hibernate.version>6.1.0.Final</hibernate.version>
<java.version>11</java.version>
<mysql.version>8.0.29</mysql.version>
<spring.version>5.3.2</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果您注意到,我包含了 Jackson 库的依赖项,因为书籍列表应作为 JSON 对象返回。我不认为这是一个问题,只是说 - 我可能已经为此删除了它,但是当它运行时,当/如果它有效时,书籍列表对我来说将难以理解。我究竟做错了什么???