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

:创建在类路径资源中定义名为“Entity ManagerFactory”的bean时出错

潘自强
2023-03-14

我从spring.io创建了一个Spring Boot示例,并添加了以下依赖项:web、jpa、driver db2,但我出现了以下错误

我的实体类

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 {
}

应用程序.属性

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>

共有1个答案

宫子晋
2023-03-14

您需要向spring data jpa存储库提供hibernate实体(employee.class)和主键的数据类型(Long)。正确的实现方式是

@Repository
public interface EmpRep extends JpaRepository<Employee, Long> {}
 类似资料: