当前位置: 首页 > 知识库问答 >
问题:

类SpringHibernateJpaPersistenceProvider未实现请求的接口PersistenceProvider

逄嘉禧
2023-03-14

我被难住了——我已经好几年没用过Hibernate了,然后,从来没有用过Spring Boot。Spring Boot,但从来没有用过Hibernate或JPA。所以我想弄清楚如何让它为我的工作工作工作——我应该在周一演示一些东西,如果我能让“this”工作,我会把它复制到我的工作笔记本电脑上,当然会更改细节。顺便说一句——这是我得到的信息——我不得不在标题中缩短它:

“类org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider未实现请求的接口javax.persistence.spi.PersistenceProvider”

我有一个“main”类-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);
    }
}

,以及RESTendpoint/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对象返回。我不认为这是一个问题,但我只是说,我可能已经删除了这一点,但当它运行时,对我来说,这本书的列表将无法理解,当/如果它起作用时,我会得到回应。我做错了什么???

共有1个答案

江宏深
2023-03-14

将hibernate更改为5.6.9版。最终版本:

<hibernate.version>5.6.9.Final</hibernate.version>
 类似资料:
  • 本文向大家介绍微信小程序HTTP接口请求封装的实现,包括了微信小程序HTTP接口请求封装的实现的使用技巧和注意事项,需要的朋友参考一下 最近在学习小程序的编写,需要调用后端接口,经常要用到wx.request方法,所以就自己封装了一下,简化一下代码,如果能给大家提供帮助更好,我的项目后端是使用的Java SSM框架,wx.request的URL就是后端提供的接口。在封装的时候我在网上看了很多篇博客

  • 问题内容: 如下代码: 产生以下错误: 错误CS0738:“ InheritanceTest.MyData”未实现接口成员“ InheritanceTest.ISomeData.Data”。’InheritanceTest.MyData.Data’无法实现’InheritanceTest.ISomeData.Data’,因为它没有匹配的返回类型’System.Collections.Generic

  • 我对类上的接口实现的理解是,接口类型引用实现接口的类的对象。 但是当我研究下面的代码片段在上面的代码中使用时… GetConnection(host,username,password)返回对对象的引用(类型为Connection),但DriverManager类中没有实现接口。谁能澄清我的这个疑问..?还是我漏掉了什么? 下面的代码段不能得到相同的东西 createStatement()应该返回

  • 问题内容: 前一段时间,我遇到了一段代码,该代码使用了一些标准Java功能来定位实现给定接口的类。我知道这些函数隐藏在某些非逻辑的位置,但是由于包名称暗含,它们可以用于其他类。那时我不需要它,所以我忘记了它,但是现在我需要了,而且似乎无法再次找到这些函数。在哪里可以找到这些功能? 编辑:我不是在寻找任何IDE函数或任何东西,而是可以在Java应用程序中执行的东西。 问题答案: 不久前,我整理了一个

  • 本文向大家介绍PHP请求Socket接口测试实例,包括了PHP请求Socket接口测试实例的使用技巧和注意事项,需要的朋友参考一下 使用php读取socket接口的数据,通过php传递请求方法和请求参数,得到返回结果 PHP文件: Java的Socket服务端: 效果: 以上这篇PHP请求Socket接口测试实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。

  • 我有个Z接口。它是由Y类实现的。我有另一个继承类Y的类x。因为y实现了类z指定的契约,所以x做同样的事情,因为它扩展了类y。因此,它是否也应该实现接口Z?