类SpringHibernateJpaperSistenceProvider不会实现请求的接口PersistenceProvider

发布于 2025-02-11 02:08:13 字数 10273 浏览 3 评论 0原文

我很难过 - 几年来我没有使用过冬眠,然后再也没有使用Spring Boot。春季靴,但从未与Hibernate或JPA一起使用。因此,我试图弄清楚如何使这项工作为我的工作工作 - 我应该在星期一演示一些东西,如果我能得到“此”来工作,我会将其复制到我的工作笔记本电脑上并更改细节当然。顺便说一句 - 这是我收到的消息 - 我不得不在标题中缩短它:

“ org.springframework.orm.jpa.jpa.jpa.dendor.springhibernatejpapersisterceistercerovider并未实现请求的接口Javax.persistence.persistence.persistence.persistence.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;
    }
}

,daoconcrote类(接口只是一种合乎逻辑的方法,但如果您愿意,我也会发布它):

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);
    }
}

和用于其余端点/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>

如果您要注意,我包括杰克逊库的依赖性,因为书籍列表应作为JSON返回目的。我不认为这是一个知的,而只是说 - 我可能会为此重新求解,但是当它运行时,书籍清单对我来说是难以理解的。我在做什么错???

I'm stumped - I haven't used Hibernate in several years and then, never with Spring Boot. Spring Boot but never with Hibernate or 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"

I have the "main" class - 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:

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):

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:

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:

<?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. 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???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纸短情长 2025-02-18 02:08:13

将Hibernate更改为5.6.9的版本(或任何更高的5.6.x):

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

Change hibernate to version 5.6.9.Final (or any higher 5.6.x):

<hibernate.version>5.6.9.Final</hibernate.version>
羁绊已千年 2025-02-18 02:08:13

首先检查gradle依赖项树检查用于Hibernate的哪个版本。
就我而言,我更新了依赖性并解决了他的问题。
Hibernate-core:6.2.13.final

First Check the gradle dependencies tree check which version used for hibernate.entity manager and hibernate-core and update the dependencies
In my case I update the dependency and solved he problem.
hibernate-core:6.2.13.Final

德意的啸 2025-02-18 02:08:13

这在build.gradle中为我带来了困难:

// With Jakarta
implementation('org.ehcache:ehcache') {
    capabilities {
        requireCapability('org.ehcache:ehcache-jakarta')
    }
}
implementation 'org.hibernate:hibernate-jcache:6.4.4.Final'

This did the trick for me in build.gradle:

// With Jakarta
implementation('org.ehcache:ehcache') {
    capabilities {
        requireCapability('org.ehcache:ehcache-jakarta')
    }
}
implementation 'org.hibernate:hibernate-jcache:6.4.4.Final'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文