Hibernate with SQLite Unable to run App second time after DB creation - java

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?

Related

Spring Boot Framework doesn't initialize Database on Startup

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/

Envers audit table does not exist when hibernate (via create-drop) tries to delete it

i am using java, spring, hibernate and envers among other things. For testing purposes i use a h2 database and for the test configuration i set spring.jpa.hibernate.ddl-auto=create-drop. Some of my entities are audited (using envers).
Now, when i run my tests i get something like:
2020-04-24 12:05:30.109 DEBUG [org.hibernate.SQL] [Test worker]: alter table nmc.testtable_aud drop constraint FKcmwq41oxs0yus7mgufns1njbd
Hibernate: alter table nmc.testtable_aud drop constraint FKcmwq41oxs0yus7mgufns1njbd
2020-04-24 12:05:30.112 WARN [org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl] [Test worker]: GenerationTarget encountered exception accepting command : Error executing DDL "alter table nmc.testtable_aud drop constraint FKcmwq41oxs0yus7mgufns1njbd" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table nmc.testtable_aud drop constraint FKcmwq41oxs0yus7mgufns1njbd" via JDBC Statement
...
Caused by: org.h2.jdbc.JdbcSQLException: Table "testtable_aud" not found; SQL statement:
When i use spring.jpa.hibernate.ddl-auto=update the described error does not occur, but that causes issues further down the line so it is not an option for me.
While the tests run green on my local developer instance it fails on the central testing machine. Also, this is really annoying since i do not want it to throw an error when there is nothing wrong.
It seems to me that this is a really basic issue. I have searched for some time now and tried different things, but i cannot seems to resolve this conflict. From what i read envers/hibernate integration works well so i cannot believe that this is a common thing which then makes me think that i configured something incorrectly. I just expect that hiberate should only drop tables which already exist or at least produce the appropriate sql.
Any help and/or pointer in the right direction would be greatly appreciated.
Thanks.

Can you set hibernate to drop tables before (re)creating it

I have an oracle database and a spring API written in Java. I'm trying to get it to drop my tables, before hibernate recreates it on startup. If the tables do not exist, they will be created as expected and I can see the status of their creating in the console output. However, I then can't drop and recreate them if I stop and rerun it.
I've got the hibernate settings set in an application.properties file (included below) but nothing i've tried has worked.
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.generate_ddl=true
hibernate.generate_ddl-auto=update
hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
I've tried changing the update for create-drop with no luck.
Any ideas what I'm doing wrong?
You can change hibernate.generate_ddl-auto=update to hibernate.generate_ddl-auto=create-drop or try hibernate.hbm2ddl.auto=create-drop
If Spring JPA is being used the property required is spring.jpa.hibernate.ddl-auto=create

Hibernate reverse engineering with sybase

I am trying to implement Hibernate reverse engineering with sybase database, but I keep getting the below error :
Caused by: java.sql.SQLException: The 'CREATE TABLE' command is not allowed within a multi-statement transaction in the 'tempdb' database.
I see that it's executing the below procedure on my database
devConfig..sp_jdbc_primarykey 'devConfig','dbo','Customer'
This command runs fine if I execute this from any DB tool like (squirrel or DBArtisan) but fails via hibernate.
I was thinking if I can disable the Hibernate transactions somehow, but I don't find any such way.
Can someone please help me on this?

Hibernate database name change gives MySQLSyntaxErrorException: Table doesn't exist

I used to have a database called database and everything was working well using hibernate and its models.
I removed <property name="hibernate.hbm2ddl.auto"> to avoid update or create as it's a production server, we want to do it manually.
We recently switched to database2 and so we switched the hibernate configuration file and all the hibernate XML models.
`<class name="com.api.models.database.MmApplications" table="mm_applications" catalog="database2">`
but it keeps looking for database event if we migrated the database, the models and the connexion.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'database.mm_applications' doesn't exist
Does someone can help me ?
UPDATE ----
Hibernate is connecting to the right database (database2), but there is a prefix as a prefix database. making the queries hitting the database instead of database2, and when I try to force the default_schema my queries become :
`... from database.database2.mm_applications ....`
Any idea?
My database is specified in the hibernate.connection.url property. Have you changed that also ? An example would be: jdbc:mysql://localhost/mydatabase
Also, instead of removing hibernate.hbm2ddl.auto then perhaps you should set its value to validate. That way hibernate will ensure that the datamodel matches the database schema.
I found the problem, It was an other application deployed on the same tomcat server using hibernate as well with another database (database) making a conflict with the new application ...
There is still something weird, by connecting to any database, hibernate will use the specified catalog in the hibernate models and so constructing the request using the catalog.table_name
Hope this help someone someday.

Categories