java 哪些步骤使用 Spring-Boot 和 JPA 启用 SQLite?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29671709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:42:30  来源:igfitidea点击:

Which steps enable SQLite with Spring-Boot and JPA?

javasqlitejpaspring-bootspring-data

提问by shful

To run SQLite with a Spring-boot (Version 1.2.3.RELEASE) application, I did these three steps:

要使用 Spring-boot(版本 1.2.3.RELEASE)应用程序运行 SQLite,我执行了以下三个步骤:

  1. use the SQLiteDialect class from Spring boot and SQLite

  2. provide a JDBC driver; we used org.xerial:sqlite-jdbc (http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7)

  3. configure the datasource; here with a spring-boot .yml file:

       spring:
      datasource:
        url: jdbc:sqlite:<full-path-to-file>.db
        username: ...
        password: ...
        driverClassName: org.sqlite.JDBC
        ...
      jpa:
        database: SQLITE
        dialect: that.custom.SQLiteDialect
        hibernate:
          ddl-auto: update
    
  1. 使用Spring boot 和 SQLite 中的 SQLiteDialect 类

  2. 提供JDBC驱动程序;我们使用了 org.xerial:sqlite-jdbc ( http://mvnrepository.com/artifact/org.xerial/sqlite-jdbc/3.8.7)

  3. 配置数据源;这里有一个 spring-boot .yml 文件:

       spring:
      datasource:
        url: jdbc:sqlite:<full-path-to-file>.db
        username: ...
        password: ...
        driverClassName: org.sqlite.JDBC
        ...
      jpa:
        database: SQLITE
        dialect: that.custom.SQLiteDialect
        hibernate:
          ddl-auto: update
    

Now, that's not enough. It ends with a "No enum constant org.springframework.orm.jpa.vendor.Database.SQLITE" error:

现在,这还不够。它以“无枚举常量 org.springframework.orm.jpa.vendor.Database.SQLITE”错误结束:

Field error in object 'spring.jpa' on field 'database': rejected value [SQLITE]; codes [typeMismatch.spring.jpa.database,typeMismatch.database,typeMismatch.org.springframework.orm.jpa.vendor.Database,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [spring.jpa.database,database]; arguments []; default message [database]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.orm.jpa.vendor.Database' for property 'database'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type org.springframework.orm.jpa.vendor.Database for value 'SQLITE'; nested exception is java.lang.IllegalArgumentException: No enum constant org.springframework.orm.jpa.vendor.Database.SQLITE]

Indeed the org.springframework.orm.jpa.vendor.Databaseclass has no SQLITE value, sadly. However, the spring doc (http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/orm/jpa/vendor/Database.html) states:

遗憾的是,确实org.springframework.orm.jpa.vendor.Database类没有 SQLITE 值。但是,spring 文档(http://docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/orm/jpa/vendor/Database.html)指出:

If a given PersistenceProvider supports a database not listed here, the strategy class can still be specified using the fully-qualified class name.

如果给定的 PersistenceProvider 支持此处未列出的数据库,则仍然可以使用完全限定的类名指定策略类。

That looks like I have to:

看起来我必须:

  1. remove the pointless

    jpa: database: SQLITE

  1. 去除无意义的

    jpa:数据库:SQLITE

configuration piece above, and

上面的配置块,以及

  1. Provide some "strategy class" instead.
  1. 提供一些“策略类”代替。

Am I on the right path here ?

我在正确的道路上吗?

What do they mean with "strategy class" and how to get or implement it ?

“战略课程”是什么意思以及如何获得或实施它?

UPDATE

更新

Solution: According to the comment of M. Deinum, the right configuration reads:

解决方案:根据M. Deinum的评论,正确的配置如下:

       spring:
      datasource:
        url: jdbc:sqlite:<full-path-to-file>.db
        username: ...
        password: ...
        driverClassName: org.sqlite.JDBC
        ...
      jpa:
        database-platform: that.custom.SQLiteDialect
        hibernate:
          ddl-auto: update

Seems like no "strategy class" needs to be implemented at all.
I've got it now: The removed jpa: database: SQLITEline (which corresponds to that org.springframework.orm.jpa.vendor.Databaseclass) is not mandatory at all. It's a convenience way -- for most databases but not SQLite.

似乎根本不需要实施“战略课程”。
我现在明白了:删除的jpa: database: SQLITE行(对应于那个org.springframework.orm.jpa.vendor.Database类)根本不是强制性的。这是一种方便的方式——适用于大多数数据库,但不适用于 SQLite。

Remark: When using Hibernate, that database-platform=...entry is like hibernate.dialect=that.custom.SQLiteDialectin a Hibernate specific configuration, which would take the same effect.

备注:当使用 Hibernate 时,该database-platform=...条目就像hibernate.dialect=that.custom.SQLiteDialect在 Hibernate 特定配置中一样,会产生相同的效果。

采纳答案by M. Deinum

For Hibernate the so called strategy classis a functional implementation of the org.hibernate.dialect.Dialectclass. For the most common database there is a short cut and then you can use the databaseproperty and simply specify the database (like ORACLE, DB2etc.). However that doesn't exists for SQLite, you would need to configure the specific dialect.

对于 Hibernate,所谓的策略类该类的功能实现org.hibernate.dialect.Dialect。对于最常见的数据库有一个捷径,然后你可以使用database属性,只需指定数据库(如ORACLEDB2等)。但是,SQLite 不存在这种情况,您需要配置特定的方言。

To specify the dialect you can use the database-platformproperty and just pass the dialect to use to it.

要指定方言,您可以使用该database-platform属性并将方言传递给它。

jpa:
    database-platform: that.custom.SQLiteDialect

This also works for any other dialect like the org.hibernate.dialect.MySQL5InnoDBDialectif you want to specify a specific one for MySQL (for instance).

这也适用于任何其他方言,例如org.hibernate.dialect.MySQL5InnoDBDialect如果您想为 MySQL 指定特定方言(例如)。