I am finishing a training project with Rest api in Java,
with springboot and a postgresql database.
Im trying to initialize the database on startup, with schema.sql, and data.sql.
The creation and the data injections works fine when i look directly in the db through PgAdmin, with that configuration file.
spring.datasource.url = jdbc:postgresql://localhost:5432/db_test
spring.datasource.username = adm_library
spring.datasource.password = admin
spring.datasource.driver-class-name = org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
#hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false
spring.datasource.initialization-mode=always
#spring.jpa.hibernate.ddl-auto = none
But, if i try to use the application straight away, i'm getting
org.postgresql.util.PSQLException: ERREUR: the relation « book » doesn't exists
Here the entity causing error is "book" cause i try to retrieve books but it can be "users" if i try to retrieve users etc...
I found a way of making it work by rebooting the app without
spring.datasource.initialization-mode=always
And then it's ok.
Any explanation on this behaviour ?
Thanks !
Spring Boot automatically creates the schema of an embedded DataSource. This behavior can be customized by using the spring.datasource.initialization-mode property.
For instance, if you want to always initialize the DataSource regardless of its type:
spring.datasource.initialization-mode=always
More details can be checked in the java doc at Java-Doc and Spring Documentation.
Related
I have a spring boot app that connects to a MySQL DB and I have been able to work with the tables, but now I created a model, repository and service to access a view. I created one route in a controller to work with this view and I get the following error message:
java.sql.SQLException: The user specified as a definer ('root'#'%') does not exist
In MySQL workbench when I look at the view it looks like this:
CREATE
ALGORITHM = UNDEFINED
DEFINER = `root`#`%`
SQL SECURITY DEFINER
VIEW `db`.`my_view` AS
...
In application.properties:
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${HOSTNAME:localhost}:${PORT:3306}/${DB_NAME:db}?serverTimezone=UTC
spring.datasource.username=${USERNAME:root}
spring.datasource.password=${PASSWORD:password}
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
Do I need to change the definer in the DB? I am logged in as the same user as when I created it. Pls let me know if I need to add any information and any help is greatly appreciated!
The easiest way to fix your error is removing any DEFINER statements from SQL script.
I have a problem with the Spring Framework. It doesn't create the database automatically on startup. I read the HowTo-Guides of Spring on how to initialize the database and followed these steps, but it doesn't work. I also searched around the web for similar problems, but I didn't find anything which could help me.
Error description:
On startup of the Server, I get an Errormessage:
FATAL: Datenbank »money_man_api_db« existiert nicht (German)
FATAL: Database »money_man_api_db« does not exist (English translation)
My configuration:
application.properties:
server.port=3000
# Basic Connection Configuration
spring.datasource.hikari.connection-timeout=20000
spring.datasource.hikari.maximum-pool-size=5
# PostgreSQL Configuration
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialization-mode=always
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/money_man_api_db
spring.datasource.username=postgres
spring.datasource.password=admin
Why doesn't the database get initialized? Did I forget something?
First you should create a database, later on you can connect to this database.
The title is misleading because Hibernate will not create a database, but will create the tables in this database. Hibernate is an ORM, it will create compatible SQL queries to interact with the database.
It's a layer between your OO code and the database, that takes care of complexity of creating SQL queries and mapping them to your code.
More info can be found here: https://hibernate.org/orm/
I'm trying to run a simple H2 Spring project. I've used starter.spring.io to initialize a project with the web, JDBC, JPA, andH2` dependencies. I'm following along with in28minutes Spring Mastercourse.
All I'm trying to do is initialize one table on startup. I do the following in my data.sql:
CREATE TABLE person
(
id integer not null,
name varchar(255) not null,
location varchar(255),
birth_date timestamp,
primary key(id)
);
My application.properties looks like this:
spring.h2.console.enabled=true
I haven't touched a single other file in the entire project. Just loaded a project, added those lines, and tried running it. For some reason, my table isn't being created. I've gone through the steps in the tutorial probably 15 times by this point and can't find what I'm doing wrong, any help would be greatly appreciated.
Edit: My JDBC URL is correct, as well as my driver class. I've checked those each time I tried re-running the steps
With your exact dependency setup, and with your schema, I can see the data in the web console.
The predefined Generic H2 (embedded) settings should provide the right values, but you should input the following :
Driver Class : org.h2.Driver
JDBC URL : jdbc:h2:mem:testdb
User Name : sa
Password : <empty>
on the web console login page, as those are the defaults for an embedded h2 database.
If you don't see any issue in the startup log, this is most likely it.
Take note that H2 will not error out if you try to connect to a non-existent database.
e.g. : jdbc:h2:mem:db, jdbc:h2:mem:foobar will not produce any errors and connect to empty databases.
Hi I'm using Spring Boot version 1.5.9.
When using Spring Boot to initialize schema.sql for mysql database, it works all fine and the database schema is getting created successfully. But on restart of the application this schema.sql script is executing again and the application fails to start because the tables already exist.
I tried spring.jpa.hibernate.ddl-auto=create-drop option in application.properties but it does not have any effect (probably because it only works for Hibernate entities which I'm not using)
Is there a way to have Spring Boot to re-create schema from schema.sql every time on restart if the database is not in-memory one?
GitHub:
https://github.com/itisha/spring-batch-demo/tree/database-input
According to the documentation you can simply ignore exceptions by setting spring.datasource.continue-on-error property to true
Spring Boot enables the fail-fast feature of the Spring JDBC
initializer by default, so if the scripts cause exceptions the
application will fail to start. You can tune that using
spring.datasource.continue-on-error.
or even turn it off with spring.datasource.initialize set to false
You can also disable initialization by setting spring.datasource.initialize to false.
A workaround could be, to change the create statements in your schema.sql
from
CREATE TABLE test .....
to
CREATE TABLE IF NOT EXISTS test ...
use the IF NOT EXISTS statements
turn off automatic schema creation to avoid conflicts: add this line in your application.properties
spring.jpa.hibernate.ddl-auto=none
I am connecting to an external database (i.e. Oracle 10g) using my Play 1.2.4 application.
I have provided all the DB details in the application.conf file, then added the ojdbc.jar in the classpath and the connection seems to work fine. But the problem is everytime when the application access some data for the first time. it is trying to create the entity table (which is already present) and hence throws an exception.
I don't want to create any table since its already there with some data, hence how can I avoid this?
There might be some configuration to set in the application.conf for accessing the external database where the table is already available(I believe), kindly help me on this.
Below is the conf file:
application.mode=dev
%prod.application.mode=prod
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=****
db.pass=****
#%test.module.cobertura=${play.path}/modules/cobertura
%test.application.mode=dev
%test.db.url=jdbc:h2:mem:play;MODE=MYSQL;LOCK_MODE=0
%test.jpa.ddl=create
%test.mail.smtp=mock
Note: Presently I am connecting to the external Oracle database and not with the embedded H2 database.
In your application.conf set the following property to none:
jpa.ddl=none