I would like to put data only when database does not exist on booting Spring boot.
How can I see that?
My application yaml has
...hbm2ddl.auto: update
so I want to see if hibernate did create tables or not in Java code.
I have a Spring boot application that uses JPA with Hibernate. It already connect to a primary data source for all transactions.
Now the task in hand is that I need to dynamically create Database Schema in a different database. For instance,
For project 1, create schema 1 in Database X
For project 2, create schema 2 in Database X
Later, these databases will be used by others externally. I am looking for the best way to get this done.
In case of defining multiple datasources in spring boot project, you have to declare associated beans in your configuration. With spring.jpa.hibernate.ddl-auto=create it will automatically create the schema in right database as answered here
What is the difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl?
I have found in this question: What are the possible values of spring.datasource.initialization-mode? that OP is using both in properties, however it seems like the origin of hbm2ddl is hibernate directly not Spring Data Jpa.
Nevertheless, reading the answer from another OP, it looks like pass-through only.
However in our commercial project with mariadb, when we do not close our spring boot application gracefully with spring.jpa.hibernate.hbm2ddl.auto=create, when the application is run again, it deletes old data and creates everything from scratch. On the other hand with spring.jpa.hibernate.ddl.auto=create every second run (after no graceful application shutdown) causes key constraint exceptions (DB is not being dropper before creation)
From this Link
By default, JPA databases are automatically created only if you use an embedded database (H2, HSQL, or Derby).
You can explicitly configure JPA settings by using spring.jpa.* properties. For example, to create and drop tables you can add the following line to your application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate’s own internal property name for this (if you happen to remember it better) is hibernate.hbm2ddl.auto.
From this Link
spring.jpa.hibernate.ddl-auto 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".
From this Link
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts).
It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.
In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform.
This allows you to switch to database-specific scripts if necessary. For example, you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql, and so on).
I'm building a fairly simple Spring Boot application which needs some configuration to be set (and regularly updated as part of it's use). I'd like to create a simple admin interface and first-run wizard to set/update this configuration.
I'd like a way to easily read and write these configuration values within the application and have them persist. I would like to avoid the overhead of a database for 5-10 configuration strings. There is some good documentation on externalising configuration in Spring Boot but it doesn't talk about how this config could be updated and persisted by the app.
Options I have come up with:
There is a write option with Spring configuration that I'm not aware of (this would be awesome)
Don't use the Spring Boot configuration functionality, instead use apache commons configuration (or similar??) to read and write to a file which lives in a location specified by an environment variable
as per option 2 but use HSQL, H2 or Derby as a file-based database
Thanks for any suggestions as to how best to achieve this.
I use embedded MongoDB database in a development environment. It is setup empty at the application startup. I would like to load initial data which is needed for the application when the Spring context is setup.
Is there a way in Spring Data MongoDB to point a JSON file(s) which would be loaded into a database (something like import.sql (hibernate.hbm2ddl.import_files) in Hibernate or #UsingDataSet in NoSql Unit)?
mongeez see wiki link for spring integration wiki