SQLite with Spring Boot

Added custom dialect for SQLite database.

Simplify spring-data-rest Configuration

Now uses profiles so that learners don't need to uncomment code in
order for demonstrations to work.

Issue: BAEL-2213
This commit is contained in:
Josh Cummings 2018-12-01 00:40:53 -07:00 committed by GitHub
parent eb1a92698b
commit 51352c470f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 142 additions and 3 deletions

View File

@ -33,6 +33,15 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>${sqlite.version}</version>
</dependency>
<!-- <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>${hsqldb.version}</version> </dependency> <dependency> <groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId> <version>${derby.version}</version> </dependency> <dependency> <groupId>org.xerial</groupId> <artifactId>sqlite-jdbc</artifactId> <version>${sqlite.version}</version>
</dependency> -->
@ -43,6 +52,8 @@
</build>
<properties>
<start-class>com.baeldung.SpringDataRestApplication</start-class>
<sqlite.version>3.25.2</sqlite.version>
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
</properties>

View File

@ -7,6 +7,7 @@ import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@ -14,11 +15,12 @@ import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
//@Configuration
@Configuration
@EnableJpaRepositories(basePackages = "com.baeldung.repositories")
// @PropertySource("persistence-h2.properties")
// @PropertySource("persistence-hsqldb.properties")
// @PropertySource("persistence-derby.properties")
//@PropertySource("persistence-sqlite.properties")
public class DbConfig {
@Autowired
@ -59,3 +61,25 @@ public class DbConfig {
}
}
@Configuration
@Profile("h2")
@PropertySource("persistence-h2.properties")
class H2Config {}
@Configuration
@Profile("hsqldb")
@PropertySource("persistence-hsqldb.properties")
class HsqldbConfig {}
@Configuration
@Profile("derby")
@PropertySource("persistence-derby.properties")
class DerbyConfig {}
@Configuration
@Profile("sqlite")
@PropertySource("persistence-sqlite.properties")
class SqliteConfig {}

View File

@ -0,0 +1,78 @@
package com.baeldung.dialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.identity.IdentityColumnSupport;
import java.sql.Types;
public class SQLiteDialect extends Dialect {
public 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.BLOB, "blob");
registerColumnType(Types.CLOB, "clob");
registerColumnType(Types.BOOLEAN, "integer");
}
public IdentityColumnSupport getIdentityColumnSupport() {
return new SQLiteIdentityColumnSupport();
}
public boolean hasAlterTable() {
return false;
}
public boolean dropConstraints() {
return false;
}
public String getDropForeignKeyString() {
return "";
}
public String getAddForeignKeyConstraintString(String constraintName, String[] foreignKey, String referencedTable, String[] primaryKey, boolean referencesPrimaryKey) {
return "";
}
public String getAddPrimaryKeyConstraintString(String constraintName) {
return "";
}
public String getForUpdateString() {
return "";
}
public String getAddColumnString() {
return "add column";
}
public boolean supportsOuterJoinForUpdate() {
return false;
}
public boolean supportsIfExistsBeforeTableName() {
return true;
}
public boolean supportsCascadeDelete() {
return false;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.dialect;
import org.hibernate.MappingException;
import org.hibernate.dialect.identity.IdentityColumnSupportImpl;
public class SQLiteIdentityColumnSupport extends IdentityColumnSupportImpl {
@Override
public boolean supportsIdentityColumns() {
return true;
}
@Override
public String getIdentitySelectString(String table, String column, int type) throws MappingException {
return "select last_insert_rowid()";
}
@Override
public String getIdentityColumnString(int type) throws MappingException {
return "integer";
}
}

View File

@ -0,0 +1 @@
spring.profiles.default=h2

View File

@ -1,4 +1,7 @@
driverClassName=org.sqlite.JDBC
url=jdbc:sqlite:memory:myDb
url=jdbc:sqlite:memory:myDb?cache=shared
username=sa
password=sa
password=sa
hibernate.dialect=com.baeldung.dialect.SQLiteDialect
hibernate.hbm2ddl.auto=create-drop
hibernate.show_sql=true