spring-data-jpa 找不到名为'entityManagerFactory'的Bean

m2xkgtsf  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(182)

我的应用程序无法启动。它是由Sping Boot + JPA + SQLite组成的。但是Spring Boot和MySQL运行良好。我不明白为什么会发生这种情况。
错误为
找不到名为'entityManagerFactory'的Bean

<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>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.15.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.1.4.Final</version>
        </dependency>

编码:

@Entity
public class Contact implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int ContactID;
    String Number;
    String textMessage;
    String DownloadID;
    long ContactFlags;

    public int getContactID() {
        return ContactID;
    }

    public void setContactID(int contactID) {
        ContactID = contactID;
    }

    public String getNumber() {
        return Number;
    }

    public void setNumber(String number) {
        Number = number;
    }

    public String getTextMessage() {
        return textMessage;
    }

    public void setTextMessage(String textMessage) {
        this.textMessage = textMessage;
    }

    public String getDownloadID() {
        return DownloadID;
    }

    public void setDownloadID(String downloadID) {
        DownloadID = downloadID;
    }

    public long getContactFlags() {
        return ContactFlags;
    }

    public void setContactFlags(long contactFlags) {
        ContactFlags = contactFlags;
    }
}

public interface ContactRepository extends CrudRepository<Contact,Integer> {
}

@Configuration
public class DBConfig {
    @Bean
    public DataSource dataSource(){
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.sqlite.JDBC");
        dataSourceBuilder.url("jdbc:sqlite::/home/vivien/viber.db");
        return dataSourceBuilder.build();
    }
}

public class SQLiteDialect extends Dialect {    
    SQLiteDialect(){
        registerColumnType(Types.BIT, "integer");
        registerColumnType(Types.TINYINT, "tinyint");
        registerColumnType(Types.SMALLINT, "smallint");
        registerColumnType(Types.INTEGER, "integer");
        registerColumnType(Types.BIGINT, "bigint");
        registerColumnType(Types.FLOAT, "float");
        registerColumnType(Types.REAL, "real");
        registerColumnType(Types.DOUBLE, "double");
        registerColumnType(Types.NUMERIC, "numeric");
        registerColumnType(Types.DECIMAL, "decimal");
        registerColumnType(Types.CHAR, "char");
        registerColumnType(Types.VARCHAR, "varchar");
        registerColumnType(Types.LONGVARCHAR, "longvarchar");
        registerColumnType(Types.DATE, "date");
        registerColumnType(Types.TIME, "time");
        registerColumnType(Types.TIMESTAMP, "timestamp");
        registerColumnType(Types.BINARY, "blob");
        registerColumnType(Types.VARBINARY, "blob");
        registerColumnType(Types.LONGVARBINARY, "blob");
        // registerColumnType(Types.NULL, "null");
        registerColumnType(Types.BLOB, "blob");
        registerColumnType(Types.CLOB, "clob");
        registerColumnType(Types.BOOLEAN, "integer");
        registerFunction( "concat", new VarArgsSQLFunction(StringType.INSTANCE, "", "||", "") );
        registerFunction( "mod", new SQLFunctionTemplate( StringType.INSTANCE, "?1 % ?2" ) );
        registerFunction( "substr", new StandardSQLFunction("substr", StringType.INSTANCE) );
        registerFunction( "substring", new StandardSQLFunction( "substr", StringType.INSTANCE) );
    }
    public boolean supportsIdentityColumns() {
        return true;
    }

    spring.datasource.url=jdbc:sqlite::/home/vivien/viber.db
    spring.datasource.driver-class-name = org.sqlite.JDBC
    spring.datasource.username=
    spring.datasource.password=
    spring.jpa.database-platform=com.example.demo.SQLiteDialect
    spring.jpa.show-sql=true

    @RestController
public class MyController {
     @Autowired
    ContactRepository contactRepository;

    @GetMapping("/hello")
    public String Hello(){
        return "Hello Sqlite";
    }

    @GetMapping("/all")
    public Iterable<Contact> getAll(){
        return contactRepository.findAll();
    }
}

错误消息:
启动ApplicationContext时出错。若要显示条件报告,请在启用“调试”的情况下重新运行应用程序。
2018年12月25日17:35:44.340错误25591 --- [主要]
o.s.b.d.记录故障分析报告程序:


应用程序启动失败


说明:
com.example.demo.controller.MyController中的字段contactRepository需要一个名为“entityManagerFactory”的Bean,但找不到该Bean。
注射点具有以下注解:

  • @org.springframework.beans.工厂.注解.自动连线(必需=true)
ndasle7k

ndasle7k1#

这里的问题是您在MyController类中自动连接了一个EntityManagerFactory。EntityManagerFactory不能通过@Autowired或@Resource直接注入
如果您需要EntityMananger的依赖项注入,请使用

@PersistenceContext
private EntityManager entityManager;
eyh26e7m

eyh26e7m2#

这是一个老问题,但是最近我在pom.xml文件中添加了<hibernate.version>6.1.1.Final</hibernate.version>这样的属性时遇到了同样的问题。
你说越新越好?
然后,我发现spring-boot-dependences-2.7.2.pom.xml有一个属性<hibernate.version>5.6.10.Final</hibernate.version>
我以这种方式重写了它,给自己制造了一个问题。看起来Sping Boot JPA仍然不知道如何与最新版本的Hibernate一起工作。
对我来说,解决方案是从pom.xml中删除<hibernate.version>属性,通常是从Hibernate依赖项的描述中删除<version>标记。

相关问题