简体   繁体   English

类 SpringHibernateJpaPersistenceProvider 没有实现请求的接口 PersistenceProvider

[英]Class SpringHibernateJpaPersistenceProvider does not implement the requested interface PersistenceProvider

I'm stumped - I haven't used Hibernate in several years and then, never with Spring Boot.我被难住了——我已经好几年没用过 Hibernate 了,而且从来没有用过 Spring Boot。 Spring Boot but never with Hibernate or JPA. Spring Boot,但从不使用 Hibernate 或 JPA。 So i'm trying to figure out how to get this to work for my job - I'm supposed to demo something Monday and if I can get 'this' to work, I'll copy it over to my work laptop and change the details of course.所以我试图弄清楚如何让它为我的工作工作 - 我应该在星期一演示一些东西,如果我能让“这个”工作,我会把它复制到我的工作笔记本电脑上并改变当然是细节。 Btw - here's the message I get - I had to shorten it in the title:顺便说一句 - 这是我得到的信息 - 我不得不在标题中缩短它:

"Class org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider does not implement the requested interface javax.persistence.spi.PersistenceProvider" “类 org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider 未实现请求的接口 javax.persistence.spi.PersistenceProvider”

I have the "main" class - TestWebApplication:我有“主”类 - 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);
    }
}

, the entity bean: ,实体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;
    }
}

, the DAOConcrete class (the interface is just one method which is logical but if you want I'll post that too): ,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);
    }
}

, and the Controller class for the REST endpoints/MVC Controller: ,以及 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);
    }
}

The POM file is here just for completeness but I don't think it's necessarily a problem unless I'm missing a dependency: 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>

If you'll note, i'm including a dependency for the Jackson library because the list of books should return as a JSON object.如果您注意到,我将包含对 Jackson 库的依赖项,因为书籍列表应作为 JSON 对象返回。 I don't think that's a poblem but just saying - and I could probably have remoed that for this but then when it runs, the list of books would be unintelligible to me getting a response when/if it worked.我不认为这是一个问题,而只是说-我可能已经为此删除了它,但是当它运行时,当/如果它起作用时,我将无法理解书籍清单。 What am I doing wrong???我究竟做错了什么???

将休眠更改为 5.6.9.Final 版本:

<hibernate.version>5.6.9.Final</hibernate.version>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类 org.gradle.tooling.internal.gradle.DefaultBuildIdentifier 没有实现请求的接口 - Class org.gradle.tooling.internal.gradle.DefaultBuildIdentifier does not implement the requested interface 类 ch.qos.logback.classic.LoggerContext 没有实现请求的接口 org.slf4j.ILoggerFactory - Class ch.qos.logback.classic.LoggerContext does not implement the requested interface org.slf4j.ILoggerFactory 为什么ZonedDateTime类没有实现TemporalAdjuster接口 - why ZonedDateTime class does not implement TemporalAdjuster interface Map 接口是否扩展或实现了一些其他接口或 class? - Does Map interface extend or implement some other interface or class? 实现接口类的类下的代码不执行 - the code under class which implement the interface class does not execute 将最终类强制转换为该类未声明要实现的兼容接口 - Cast a final class to a compatible interface that the class does not claim to implement 接口中的Java实现类 - Java implement class in interface 为什么@Autowired 会引发 UnsatisfiedDependencyException,甚至 class 也没有实现任何接口? - Why @Autowired raises UnsatisfiedDependencyException, even the class does not implement any interface? 错误CS0535类未实现接口成员 - error CS0535 Class does not implement interface member SQLiteDatabase&#39;没有实现接口&#39; - SQLiteDatabase 'does not implement interface'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM