Spring Data JDBC For SQLite
Written by Nikos Vaggalis   
Monday, 06 February 2023

spring-data-sqlite is a library that brings support to Spring Data JDBC for SQLite so that you can use Jdbctemplate to access your SQLite based datasets or use SQLite as a potential drop-in replacement for H2.

Spring doesn’t provide a straightforward way to integrate a SQLite database compared to other databases such as MySQL, Postgres or MongoDB. You have to jump some hoops like making and registering your own SQLDialect component.

Furthermore the available support is limited to Spring Data JPA. Its lighter alternative of Spring Data JDBC provides none. Since the author of this library, Mitsunori Komatsu, wanted to use Spring Data JDBC with SQlite, instead of resorting to hacks, he decided to implement a clean solution and has kindly published it as a library for others to use.

Thus spring-data-sqlite version 1.0.0 sprang up and can be imported into your project by: 

on Gradle

implementation "org. komamitsu:spring-data-sqlite:1. 0. 0"

on Maven

<dependency>
<groupId>org. komamitsu</groupId>
<artifactId>spring-data-sqlite</artifactId>
<version>1. 0. 0</version>
</dependency>

Together with the integration he also solved another issue. Spring Data JDBC depends on the underlying database tables being auto-generated ID columns and without that feature, CrudRepository#save(T) doesn’t work properly.

For cases where you need to use Spring Data JDBC for tables that don’t have auto-generated ID columns, spring-data-sqlite provides two new methods - insert(T) and update(T) - exposed by the SqliteHelperRepository helper class that work even with tables that don’t have auto-generated ID columns. These methods can be used as follows:

public interface CarRepository
extends PagingAndSortingRepository<Car, Integer>, SqliteHelperRepository<Car> {}

Car car = new Car(assignedCarId, "my new car");
carRepository. insert(car);
carRepository. update(modifiedCar);

To enable this magic in your own application you have to decorate you main application entry point with the @EnableSqliteRepositories annotation. The author also provides a simple but full source code example on how to go about using the library, found on its Github repo.

In the end he envisions that someday the features that he implemented will be merged into the official Spring Data Jdbc repository. And why not?

 

More Information

spring-data-sqlite

Related Articles

A New Era For Spring

Apache OpenJPA - Life Beyond Hibernate?

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


AI At edX With 30% Savings
13/12/2024

edX is offering a 30% discount on selected courses and program bundles until December 19th. We look at  AI-related certifications that could boost your resume in 2025.



Ruby On Rails Adds Kamal And Thruster Support
17/12/2024

Ruby on Rails 8 has been released. The new version comes preconfigured with Kamal 2 for application deployment, a new proxy called Thruster, and a trio of SQLite database-backed adapters named Solid C [ ... ]


More News

espbook

 

Comments




or email your comment to: comments@i-programmer.info

Last Updated ( Monday, 06 February 2023 )