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/
Related
In a Spring Boot app, I am using Hibernate and 2 tables is created properly. However, I also need to insert data one of these tables and for this purpose I thought I should use Flyway.
Then I just added insert clauses to the Flyway and use the following parameters for Hibernate and Flyway in application.properties:v
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update # also tried none
spring.flyway.url=jdbc:mysql://localhost:3306
spring.flyway.schemas=demo-db
spring.flyway.user=root
spring.flyway.password=******
I have not used Flyway for initializing database and I am not sure if I can use Flyway with Hibernate as I mentioned above. Or, should I disable Hibernate table creation and create another migration script for table creation?
If you use flyway only for insert data don't do that. Try to use this:
With Hibernate:
In addition, a file named import.sql in the root of the classpath is executed on startup if Hibernate creates the schema from scratch (that is, if the ddl-auto property is set to create or create-drop).
With Basic SQL Scripts:
Spring Boot can automatically create the schema (DDL scripts) of your JDBC DataSource or R2DBC ConnectionFactory and initialize it (DML scripts). It loads SQL from the standard root classpath locations: schema.sql and data.sql
The issue here is that Hibernate does not automatically create tables. Additionally, if using Spring Boot, Flyway will run before the service using hibernate has started. As a result, your Flyway script are interacting with a table that does not exist.
The recommended way to do this is to use Flyway to manage both your database structure, your create tables etc, and static data. This will mean your database is versioned and provisioned ready for your service and hibernate can connect.
I am using Axon Framework 4.1 (without Axon Server) with Oracle 12.1 and Spring Boot Jpa 2.3.0
When the application starts I am using hibernate via this property spring.jpa.hibernate.ddl-auto=create to generate all my tables including Axon's tables. As a result I got the names in this format DOMAIN_EVENT_ENTRY, TOKEN_ENTRY etc..
After that, when I send a command, I got the following error message: An event for aggregate [2] at sequence [0] could not be persisted.
I think this is because Axon searching for the table with the name: DOMAINEVENTENTRY and not finding it and I got the error message. When I renamed DOMAIN_EVENT_ENTRY table to DOMAINEVENTENTRY everything started working well.
If I guess well this is the problem. But on the other hand I used Axon with H2 and it worked with the name DOMAIN_EVENT_ENTRY, so I guess its an Oracle specific issue?
So my question is how can I configure the Axon tables name to generate the tables in the right name without hacking.
And think it would be good in this case a more detailed error message. Why my event can't be persisted.
My properties related with the question:
hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
spring.jpa.hibernate.ddl-auto=create
spring.jpa.generate-ddl=true
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver.class=oracle.jdbc.driver.OracleDriver
Thanks,
Mate
UPDATE
Here is the answer:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
This create the tables in this style: DOMAINEVENTENTRY etc..
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.
I am developing JavaFX Application using Spring boot, JPA, Hibernate & SQLite Database with the Gradle Build system in IntelliJ. Everything works well for the first time when there is no SQLite DB file. It will create that file and create all tables with proper definitions and I am able to do all DB operations using my app.
But when I run it for the second time It's not running and giving me an error which I am unable to solve it. Because this is the first time I am doing this.
the error is below.
Caused by: org.hibernate.exception.GenericJDBCException: Error
accessing tables metadata at
org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
at
org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
... 35 more Caused by: org.sqlite.SQLiteException: [SQLITE_ERROR] SQL
error or missing database (too many terms in compound SELECT) at
org.sqlite.core.DB.newSQLException(DB.java:941) at
org.sqlite.core.DB.newSQLException(DB.java:953) at
org.sqlite.core.DB.throwex(DB.java:918) at
org.sqlite.core.NativeDB.prepare_utf8(Native Method) at
org.sqlite.core.NativeDB.prepare(NativeDB.java:134) at
org.sqlite.core.DB.prepare(DB.java:257) ... 25 more
Now I have no Idea about
org.hibernate.exception.GenericJDBCException: Error accessing tables
metadata
and
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing
database (too many terms in compound SELECT)
My application.properties file is like below
##Database Properties
spring.datasource.driver-class-name=org.sqlite.JDBC
spring.datasource.url=jdbc:sqlite:MYDATABASE.db
spring.datasource.username=root
spring.datasource.password=
#Hibernate Properties
#The SQL dialect makes hibernate generate better SQL for chosen database
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.SQLiteDialect
#Hibernate DDL auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.main.web-environment=false
spring.main.web-application-type=none
##Uncomment below 2 lines to enable hibernate JDBC queries/logs
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Is there anyone gone through this? please help.
I suspect the problem might be related to the setting at this line:
spring.jpa.hibernate.ddl-auto=update
As far as I understand, hibernate will try to update the existing schema at startup. For this to happen, Hibernate needs to resolve the structure of your existing tables, so it needs to fetch the metadata. Looking at the exception you get, I believe it fails to fetch metadata using SQLLite JDBC driver. This would also explain why you did not get any errors when you run your application for the first time. Hibernate creates the schema from scratch in this case, so there is no error.
I suggest you to use create-drop during your development phase and none when you go to production.
You can refer to the answer for another question which explains the settings in more detail:
How does spring.jpa.hibernate.ddl-auto property exactly work in Spring?
I created a small POC app with spring boot, using hibernate (5.2.9) and maria db (10.1.19).
I had some sql dialect issues where my create/drop table SQL was using type=MyIasam but resolved that locally by setting the spring.jpa.properties.hibernate.dialect, however, when I deploy to the cloud (PCF) all of the cloud profile stuff kicks in, and I end up with hibernate deciding its dialect is going to be org.hibernate.dialect.MySQLDialect
this results in invalid SQL getting generated for creating new tables.
Note that I'm not really sure what else could be happening. This is a spring boot app (1.5.3) and the cloud profile is kicking in to do auto configuration. There's a bunch of properties injected. And I can't seem to get my dialect property to be respected.
This is a solid crushingly easy problem that is the escaping me.
Any ideas what I need to set, or provide as dependencies?
I tried removing all of the mysql dependencies, but then the connection string inject is jdbc:mysql... which i think may be part of the problem...