Luca Federzoni
Luca Federzoni

Reputation: 65

Access sqlite db file with jpa

I have a huge sqlite file containing my db. I need to know if it is possible and how to connect to this db as an embedded one with jpa. I'm developing an app that packs this database inside it's own jar so that when I use it on another system I don't have to import a copy of my db back and forth. The technologies I'd like to use are Angular and Spring since those are the ones I know best. If there are some techonlogies that better suit this purpose I'd like some suggestions.

Thanks :)

Upvotes: 6

Views: 12805

Answers (1)

Semo
Semo

Reputation: 821

I hope I undestood your question correctly, so I made a small project for you, hence you can have a look into it: spring-jpa-sqlite-sample. It may guide you a bit, though I and don't claim correctness or completeness.

The path to the sqlite file can easily be changed by inserting the correct url in the persistence.properties file:

driverClassName=org.sqlite.JDBC
url=jdbc:sqlite:src/main/resources/chinook.db  --> you may use relative paths.
hibernate.dialect=dev.mutiny.semo.config.SQLiteDataTypesConfig
hibernate.hbm2ddl.auto=none
hibernate.show_sql=true

You can also use Environment variables from your system, which Spring tries to read from, so that you can reference the correct directory to a file. This can be found here: Read system environment var (SO)

Last but not least. Beware of using huge SQLite files. Find another way and transfer it first into a 'real' Database like any other Client/Server RDBMS you know (Oracle, MariaDB, MSSQL, depends on your scenario/taste).

Have closer look onto the documentation: When to use SQLite (and when not to!)

Upvotes: 8

Related Questions