Hibernate reverse engineering with sybase - java

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?

Related

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.

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

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?

Are batch inserts not working only because of the MySQL driver? What about others?

Earlier I was trying to get batch inserts working in Hibernate. I tried everything: For the config I set batch_size(50), order_inserts(true), order_updates(true), use_second_level_cache(false), use_query_cache(false). For the session I used setCacheMode(CacheMode.IGNORE) and setFlushMode(FlushMode.MANUAL). Still the MySQL query log showed that each insert was coming in separately.
The ONLY thing that worked was setting rewriteBatchedStatements=true in the JBDC connection string. This worries me, as my application is supposed to support any JBDC database and I'm trying to avoid DB specific optimizations.
Is the only reason hibernate can't actually use batch statements because the MySQL driver doesn't support them by default? What about other drivers, do I have to add options to the connection string so they can support batched inserts? If you need specific db's, think SQL server, sqlite, Postgres, etc
One reason it could not be working is that hibernate disables batching if you use the Identity id generation strategy.
Also MySQL doesn't support JDBC batch prepared statements the same way as other databases without turning on the rewrite option.
I don't see that it is a problem to turn this flag on though, if your are setting up your application for a different database you will have to change the settings such as dialect, driver name, etc. anyway and as this is part of the JDBC connect String then you are isolated from the configuration.
Basically I think you are doing the right thing.
As batch insert (or bulk insert) is part of the SQL standard, ORM frameworks like Hibernate support and implement it. Please see Chapter 13. Batch Processing and Hibernate / MySQL Bulk insert problem .
Basically, you need to set the JDBC batch size via the variable named hibernate.jdbc.batch_size to a reasonable size. Also don't forget to end the batch transaction with flush() and clear().

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.

Error SQL7008 while updating a DB2 for iSeries table

I have a Java Web application using Hibernate and DB2 for iSeries and during update of a table I get he following error:-
Error SQL7008 while updating a DB2 for
iSeries table
From doing some googling on this error message I noticed that it happens when you are running an insert/update in a non-transactional mode. The explanation is given here.
This occurs because the table you are
trying to update is not being
journalled, and your update is being
run within a transaction.
Generally, you should always commit (and rollback if an exception occurs) your transactions. Usually I never set auto commit to true but in this case I would like to understand if it's truly needed as mentioned in the link above. Can you set the auto commit to true in your connection to see if this goes away?
<property name="hibernate.connection.autocommit" value="true"/>
Also this link has some tutorials on transaction management with hibernate.
I found the answer to my question,
This occurs As CoolBeans mentioned because the table I was trying to update is not being journalled.
Add this table to Journal, here are the steps
this took care of my problem.

Categories