HSQLDB persistence in a Spring project - java

I checked a lot of answers but I didn't understand most of them, so please bear in mind that I'm very new to programming and Java.
So, I was following this tutorial: https://medium.com/#fsonmezay/restful-issue-tracking-application-with-spring-boot-and-angularjs-61b69537b10e
But as noted in that, everything I do on the UI while the app is running disappears when I close the app.
It uses HSQLDB as the database manager, and I need the app to write on the database everything that I add while it is running.
Also, don't know if it's worth mentioning but instead of using maven, I'm using Gradle.

By default, Spring Boot autoconfigures in-memory HSQLDB connection, so all data obviously will be lost. You need to provide spring.datasource.url configuration property to override automatic configuration and use persistent data connection, for example spring.datasource.url=jdbc:hsqldb:file:~/mydb property will store your data into ~/mydb file. Take a look at Spring Boot documentation for more information on data source autoconfiguration and HSQLDB documentation for more information about HSQLDB configuration properties.

Related

what is recommended to store project configuration details in spring boot?

I'm a beginner with spring boot and java. My question is what is recommended to store Environment configuration details such as bigquery database name, aws service unqiue ids which are used in my project.
So, It is better to store in a class which is in core project or it is better to store in application.yml file
Because spring boot is so lightweight, a lot of projects use Spring boot for microservices. and in a microservice world, it's best and almost needed to store your configs in a centralized and secure place, so you can change any of them when you want.it's hard to save them in Java classes but you can easily save them with .yml or properties. in almost all cases .yml files will be better.
I think there are some options to save the information of the configurations in an application. One of them is use a *.yml to save all the information because you can have different configurations each of them per environment

In Java, Spring maintaining database without any scripts

I am working on a project in Java (using Spring Boot, Thymeleaf, Hibernate, JPA, MySql). Every time I create a new Model Class, I have to create a table in the database or if I make any change in the Model class I have to alter the table by myself. Is there any way to avoid this database related stuff. For example I will make Model classes and declare their relationships my Database tables will be generated automatically. In future if I make any changes to my classes they will be applied to the database automatically without loosing any data.
Previously I worked on PHP, Laravel. There all I needed to do is 1) run command php artisan make:migration create_posts_table, 2) declare columns like $table->string('title');, $table->foreign('user_id')->references('id')->on('users'); and then 3) run command php artisan migrate. That's it. No SQL scripts needed. I was wondering if Java, Spring has something like this.
Sure you can do it.
Use spring.jpa.hibernate.ddl-auto=update in your application.properties.
You can also use more advanced tools like https://www.liquibase.org/
Ideal way
spring.jpa.hibernate.ddl-auto=none
In my opinion, the ideal way is to create one SQL file which will create the schema at the startup for us.
To let Spring Boot to create it for you
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto= # DDL mode. This is actually a shortcut for the "hibernate.hbm2ddl.auto" property. Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
Other Possible values: create, create-drop, validate
More Detailed Explanation
You can do migration using Flyway, it's similar to Laravel migration.
Add the dependency and put your migration SQL files to classpath:db/migration. Flyway will automatically check the sql files version and apply any pending migrations.
https://flywaydb.org/documentation/plugins/springboot

How to access phpmyadmin database with play framework

I have created some mysql databases in mysql.
Now I am trying to get them into my web application by using the play framework.
I added the mysql configs in the application.conf, added the dependency for the mysql driver in the build.sbt, created my first model and added the models packages as the ebean default in the application.conf.
Now when I go into my browser I get this error:
I`m a little confused right now, because I do not want to create a new table, but use the one I created already.
Any idea what I am doing wrong??
Play's default behaviour during development is to manage your database via the evolutions plugin. You define your initial schema in conf/evolutions/default/1.sql and then apply subsequent modifications in 2.sql, 3.sql etc etc. Whenever there are changes to these evolution files the plugin will attempt to run these on the database, which is what you're seeing here (although it looks like an error, it's really just trying to be helpful.)
If you want to manage the schema yourself (and you probably should on a production DB, for example) add evolutionplugin=disabled to the application.conf file.

Simple embedded database with spring

How to setup a simple embedded database in a spring(data)+maven project?
I need to develop a simple graphical application that read some data files and display pretty stuff about it interactively. The data is very repetitive with a little hierarchical structure. However I still don't know how I will need to access it.
For these reasons, I want to store it in a database so that I can later use DB query to access the data with query filter. (it also seems a good idea to develop a persistent layer)
Because it is for a little application, I want to use an in-memory DB.
I am quite new to java (using proper dev framework) and database. But I worked on a project using spring, spring-data, JPA, etc... I did not really understand how it worked internally and would not be able to setup it up, but I found it very practical.
Now, I found lots of docs and tutorial on internet about that, but I didn't understand enough to know how to adapt them to my need. What (I think) I want is:
to use maven+spring
spring data (I guess) to use Entity, JpaRepository and Autowired stuff
an independent program, thus starting from a Application.main method
as little and simple dependencies as possible
an embedded DB (+fast+light if possible)
genericity is nice
What I fill to be lost with are:
where should I put what properties/xml-declaration
how are all the dependencies working together (spring, spring-data, h2, hsqldb, ...)
I found this project https://github.com/wrpinheiro/spring-jpa-embedded-db that looks to fit, but:
there are way too many dependencies that (I think) I don't need, thus don't want
I don't know how to start a program with it
I don't get the org.springframework.stereotype.Service thing
nor the javax.inject.Inject
I think that if you look at this project you can start building what you need
http://spring.io/guides/gs/accessing-data-rest/#initial
Its maven (or gradle), has enbeded db, spring-jpa and runs as a jar that starts its own tomcat server (you can change it into a war build if you want)
Also you can use this service(?) that spring provides to create the starting build for your project:
http://start.spring.io
You provide them with what you want to build and then the code and required files are generated :D
Pretty neat.

H2 in-memory database initialization with data

I'm using H2 with Hibernate to generate in-memory DB on the fly for unit-testing.
I managed to create the DB successfully, and everything is working ok.
But I have an issue I don't know how to approach.
I need to load reference data to the DB for testing prior to the execution of the tests.
I have this data sored as a SQL insert's file which I need to run only once in real time envirnemnt, however, because the DB is generated every time from scratch I need to figure out how to insert the data on runtime.
The data is quite simple, it's countries lists, states list, etc.
Whats the best way to do it ?
btw, everything is working underneath Spring framework.
For your tests you could execute an init script on creation of the connection.
http://www.h2database.com/html/features.html#execute_sql_on_connection
From the question tags I see you're using Hibernate. You can add a file named "import.sql" to your classpath (i.e. in src/main/resources if you're using a Maven project layout).
From Spring documentation
In addition, a file named import.sql in the root of the classpath will
be executed on startup if Hibernate creates the schema from scratch
(that is if the ddl-auto property is set to create or create-drop).
This can be useful for demos and for testing if you are careful, but
probably not something you want to be on the classpath in production.
It is a Hibernate feature (nothing to do with Spring).

Categories

Resources