Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which steps enable SQLite with Spring-Boot and JPA?

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

  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
    

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

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.Database class 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:

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

That looks like I have to:

  1. remove the pointless

    jpa: database: SQLITE

configuration piece above, and

  1. Provide some "strategy class" instead.

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:

       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: SQLITE line (which corresponds to that org.springframework.orm.jpa.vendor.Database class) is not mandatory at all. It's a convenience way -- for most databases but not SQLite.

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

like image 572
shful Avatar asked Oct 31 '22 06:10

shful


1 Answers

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

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

jpa:
    database-platform: that.custom.SQLiteDialect

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

like image 74
M. Deinum Avatar answered Nov 10 '22 19:11

M. Deinum