我从spring.io创建一个spring boot示例,并添加了这个依赖: Web,JPA,驱动程序db2,但是我有此错误
org.springframework.beans.factory.BeanCreationException:错误 在类路径中创建名称为“ entityManagerFactory”的bean 资源 [org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaConfiguration.class]: 通过工厂方法实例化Bean失败;嵌套异常为 org.springframework.beans.BeanInstantiationException:失败 实例化 [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: 工厂方法'entityManagerFactory'抛出异常;嵌套的 异常是java.lang.NoClassDefFoundError: sun / io / ByteToCharConverter 在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:645) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:625)上 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:323) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory $$ Lambda $ 137 / 770947228.getObject(未知 来源)〜[na:na] 在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) 〜[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) 〜[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 〜[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) 〜[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在com.example.springboot.SpringbootApplication.main(SpringbootApplication.java:10) [classes /:na] 引起原因:org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: 工厂方法'entityManagerFactory'抛出异常;嵌套的 异常是java.lang.NoClassDefFoundError: sun / io / ByteToCharConverter 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] ...省略了20个通用框架 引起原因:java.lang.NoClassDefFoundError:sun / io / ByteToCharConverter 在hit.db2sql.k。(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.j.b(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.j.c(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.ab.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.ac.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.ac.o(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.ac.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.ac.n(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.y.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.c.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.c.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.c.a(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.c。(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2.Db2Driver.newConnection(DashoA8514)〜[hitjdbcdb2.jar:na] 在hit.db2sql.b.connect(DashoA8514)〜[hitjdbcdb2.jar:na] 在com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) 〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) 〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) 〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) 〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) 〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.pool.HikariPool。(HikariPool.java:115)〜[HikariCP-3.4.1.jar:na] 在com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) 〜[HikariCP-3.4.1.jar:na] 在org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:158) 〜[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:116) 〜[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79) 〜[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:324) 〜[spring-jdbc-5.2.1.RELEASE.jar:5.2.1.RELEASE] 在org.springframework.boot.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:120) 〜[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateDefaultDdlAutoProvider.getDefaultDdlAuto(HibernateDefaultDdlAutoProvider.java:42) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.lambda $ getVendorProperties $ 1(HibernateJpaConfiguration.java:130)处 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration $$ Lambda $ 461 / 2090563811.get(未知 来源)〜[na:na] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings.getDdlAuto(HibernateSettings.java:41) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties $$ Lambda $ 462 / 704869156.get(未知 来源)〜[na:na] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineDdlAuto(HibernateProperties.java:136) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.getAdditionalProperties(HibernateProperties.java:102) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties.determineHibernateProperties(HibernateProperties.java:94)上 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration.getVendorProperties(HibernateJpaConfiguration.java:132) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:133) 〜[spring-boot-autoconfigure-2.2.1.RELEASE.jar:2.2.1.RELEASE] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_40-ea] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 〜[na:1.8.0_40-ea] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 〜[na:1.8.0_40-ea] 在java.lang.reflect.Method.invoke(Method.java:483)〜[na:1.8.0_40-ea] 在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) 〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE] ...省略了21个通用框架 引起原因:java.lang.ClassNotFoundException:sun.io.ByteToCharConverter 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:372)〜[na:1.8.0_40-ea] 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:361)〜[na:1.8.0_40-ea] 在java.security.AccessController.doPrivileged(本机方法)〜[na:1.8.0_40-ea] 在java.net.URLClassLoader.findClass(URLClassLoader.java:360)〜[na:1.8.0_40-ea] 在java.lang.ClassLoader.loadClass(ClassLoader.java:424)〜[na:1.8.0_40-ea] 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:308)〜[na:1.8.0_40-ea] 在java.lang.ClassLoader.loadClass(ClassLoader.java:357)〜[na:1.8.0_40-ea] ...省略了63个共同的框架
我的实体类
package com.example.springboot.model;
//import javax.persistence.*;
import java.io.Serializable;
import javax.persistence.*;
@Table(name = "employee")
@Entity(name = "employee")
public class Employee implements Serializable {
@Id
@SequenceGenerator(name = "bs",sequenceName = "bike_seq",allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO,generator = "bs")
private long empid;
@Column(columnDefinition = "VARCHAR2(20)")
private String empname;
public long getEmpid() {
return empid;
}
public void setEmpid(long empid) {
this.empid = empid;
}
public String getEmpname() {
return empname;
}
public void setEmpname(String empname) {
this.empname = empname;
}
}
组件
package com.example.springboot.control;
import com.example.springboot.model.Employee;
import com.example.springboot.repos.EmpRep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/s")
public class EmpCont {
@Autowired
private EmpRep empRep;
@GetMapping("/a")
public List<Employee> bikes()
{
return empRep.findAll();
}
}
repository
package com.example.springboot.repos;
import org.springframework.data.jpa.repository.JpaRepository;
public interface EmpRep extends JpaRepository {
}
application.property
spring.jackson.serialization.INDENT_OUTPUT=true
spring.datasource.url=jdbc:db2://localhost:50000/SAMPLE
spring.jpa.properties.hibernate.default_schema=pervasive
spring.datasource.username=11
spring.datasource.password=11
spring.datasource.driver-class-name=hit.db2.Db2Driver
spring.messages.basename=messages
server.servlet.context-path=/pervasive
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.DB2Dialect
pom.xml
<?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.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.ibm.db2</groupId>
<artifactId>jcc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--<dependency>-->
<!--<groupId>hit.db2.db2Driver</groupId>-->
<!--<artifactId>hitjdbcdb2</artifactId>-->
<!--<version>1.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>com.ibm.db2</groupId>-->
<!--<artifactId>db2jcc</artifactId>-->
<!--<version>8.1</version>-->
<!--</dependency>-->
<dependency>
<groupId>hit.db2.db2Driver</groupId>
<artifactId>hitlicense</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>hit.db2.db2Driver</groupId>
<artifactId>hitjdbcdb2</artifactId>
<version>1.0</version>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.xml.bind</groupId>-->
<!--<artifactId>jaxb-api</artifactId>-->
<!--<version>2.3.0</version>-->
<!--</dependency>-->
<!--<dependency>-->
<!--<groupId>org.hibernate.validator</groupId>-->
<!--<artifactId>hibernate-validator</artifactId>-->
<!--<version>6.0.16.Final</version>-->
<!--</dependency>-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
答案 0 :(得分:-1)
您需要向Spring Data jpa存储库提供休眠实体(Employee.class)和主键(Long)的数据类型。正确的实现是
@Repository
public interface EmpRep extends JpaRepository<Employee, Long> {}