Spring Mvc Hibernate Encoding/Multi-line import sql - java

I am working on Spring MVC,
On project startup i have set database to import default sql
with hibernate configuration hibernate.hbm2ddl.import_files.
Data inside import.sql is encoded with UTF-8.
Console Output
ERROR: org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388:
Unsuccessful: INSERT INTO menu (id, DATE_CREATED, DATE_DELETED,
DATE_UPDATED, TITLE_ENG, TITLE_GEO, TITLE_RUS, ENABLED, PARENT_ID,
URL, SITE_ID, USER_ID) VALUES
ERROR:
org.hibernate.tool.hbm2ddl.SchemaExport - You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near '' at line 1
ERROR:
org.hibernate.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: (1,
'2015-09-10 12:00:00', NULL, NULL, 'About Us', N'ჩვენს
შეს�ხებ', 'About Us', b'1', NULL, '/article/view/1', 3,
1).

Make sure you are setting the JDBC driver to use UTF-8:
jdbc:mysql://dbname?useUnicode=true&characterEncoding=utf-8
In persistence.xml, set the hibernate connection charset:
<property name="hibernate.connection.charSet" value="UTF-8"/>
If your resources/import.sql file is multi-line, add:
<property
name="hibernate.hbm2ddl.import_files_sql_extractor"
value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor"
/>
or for Spring Boot, in application.properties:
spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

Related

In Hibernate using update but still going for a create table

Using Hibernate in my project. I am getting create table queries at my logs,
But I am using a HBM2DDL_AUTO="update"
What else could had been missed ?
PS: I get these logs only when the tables are already present at runtime. If tables not present, it obviously runs smooth.
Here are the properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "org.postgresql.Driver");
settings.put(Environment.URL, "jdbc:postgresql://" + jdbc_host + ":5432/" + jdbc_database + "?prepareThreshold=0");
settings.put(Environment.USER, jdbc_user);
settings.put(Environment.PASS, jdbc_password);
settings.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "update");
Error Logs:
[INFO ]: org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
[INFO ]: org.hibernate.type.BasicTypeRegistry - HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType#3add81c4
[WARN ]: org.hibernate.boot.internal.InFlightMetadataCollectorImpl - HHH000069: Duplicate generator name wca_seq_generator
[WARN ]: org.hibernate.boot.internal.InFlightMetadataCollectorImpl - HHH000069: Duplicate generator name wca_seq_generator
[WARN ]: org.hibernate.boot.internal.InFlightMetadataCollectorImpl - HHH000069: Duplicate generator name wca_seq_generator
[INFO ]: org.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess#59696551] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
[WARN ]: org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl - GenerationTarget encountered exception accepting command : Error executing DDL "create table WCA_MESSAGE_AUDIT (id int8 not null, createts times
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table WCA_MESSAGE_AUDIT (id int8 not null, create
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559)
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlStrings(AbstractSchemaMigrator.java:504)
I have not worked postgresql in java but I've worked with MySQL. And Whenever' I need to create the table using hibernate then I mention the follow feature in application.properties and it allow to create the table. please once go for it and if it still doesn't work please inform me. Put it in proprerties.
spring.jpa.hibernate.ddl-auto=update

SQL Error unable to create H2 Database via Cucumber tests in Spring Boot App

I am trying to create a H2 database in my Spring Boot project, when I run a cucumber test.
I have this in my application.yml:
booking:
datasource:
username: sa
password:
url: jdbc:h2:mem:bookingdb;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1
driver-class-name: org.h2.Driver
My cucumber test is stored in my Cucumber package in src/acceptTest/java folder.
And the below data.sql file is stored in src/acceptTest/resources folder:
CREATE TABLE tlink (
link_id int,
ext_id varchar(255),
address_id varchar(255),
client_id varchar(255),
instance varchar(255),
source varchar(255),
timestamp datetime2
);
INSERT INTO TLINK(link_id, ext_id, address_id, client_id, instance, source, timestamp) VALUES(13582,'0000059811','3037260','0000059811','1','1', '2018-08-22 15:13:34');
When I run the runner class, the tests are being executed, but the database isn't being created.
Below are some of the logs:
Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
Caused by: org.h2.jdbc.JdbcSQLException: Table "tclientlink" not found; SQL statement:
The CREATE statement in data.sql does not seem to be getting picked up in my code. Why could this be happening?
What your message in log said is that you don't have connection to your database. At first you need establish in application.proprties connection to your server e.g. localhost/server and then when you will have connection than will be table mapped to your class which has Annotation #Entity. Annotation #Entity is just that all data will be mapped to this object and everything you will reach from this Entity class. But you need first create connection to server.
Check this link to create connection to H2 thought application.properties
https://dzone.com/articles/integrate-h2-database-in-your-spring-boot-applicat

How to turn on DDL transactional support?

I just started using flywaydb and just for a test tried with create and insert queries and fired the migrate but it ignored all my create table scripts :(
I used -X option for debug and found a strange line
DEBUG: DDL Transactions Supported: false
Seems if i made it true, it will work.
Does anyone faced this issue, if yes how to get rid of this?
i have did this on new schema, with clean-init-migrate
Full stacktrace below:
[ec2-user#ec2 flyway]$ ./flyway -X clean
/usr/bin/tput
Flyway (Command-line Tool) v.3.0
DEBUG: Adding location to classpath: /home/ec2-user/installables/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26.jar
Database: jdbc:mysql://0.0.0.0:3306/test (MySQL 5.5)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: test
DEBUG: Cleaning schema `test` ...
Cleaned schema `test` (execution time 00:00.025s)
[ec2-user#ec2 flyway]$
[ec2-user#ec2 flyway]$ ./flyway -X init
/usr/bin/tput
Flyway (Command-line Tool) v.3.0
DEBUG: Adding location to classpath: /home/ec2-user/installables/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26.jar
Database: jdbc:mysql://0.0.0.0:3306/test (MySQL 5.5)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: test
DEBUG: Schema `test` already exists. Skipping schema creation.
Creating Metadata table: `test`.`schema_version`
DEBUG: Found statement at line 17: CREATE TABLE `test`.`schema_version` (
`version_rank` INT NOT NULL,
`installed_rank` INT NOT NULL,
`version` VARCHAR(50) NOT NULL,
`description` VARCHAR(200) NOT NULL,
`type` VARCHAR(20) NOT NULL,
`script` VARCHAR(1000) NOT NULL,
`checksum` INT,
`installed_by` VARCHAR(100) NOT NULL,
`installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`execution_time` INT NOT NULL,
`success` BOOL NOT NULL
) ENGINE=InnoDB
DEBUG: Found statement at line 30: ALTER TABLE `test`.`schema_version` ADD CONSTRAINT `schema_version_pk` PRIMARY KEY (`version`)
DEBUG: Found statement at line 32: CREATE INDEX `schema_version_vr_idx` ON `test`.`schema_version` (`version_rank`)
DEBUG: Found statement at line 33: CREATE INDEX `schema_version_ir_idx` ON `test`.`schema_version` (`installed_rank`)
DEBUG: Found statement at line 34: CREATE INDEX `schema_version_s_idx` ON `test`.`schema_version` (`success`)
DEBUG: Executing SQL: CREATE TABLE `test`.`schema_version` (
`version_rank` INT NOT NULL,
`installed_rank` INT NOT NULL,
`version` VARCHAR(50) NOT NULL,
`description` VARCHAR(200) NOT NULL,
`type` VARCHAR(20) NOT NULL,
`script` VARCHAR(1000) NOT NULL,
`checksum` INT,
`installed_by` VARCHAR(100) NOT NULL,
`installed_on` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`execution_time` INT NOT NULL,
`success` BOOL NOT NULL
) ENGINE=InnoDB
DEBUG: Executing SQL: ALTER TABLE `test`.`schema_version` ADD CONSTRAINT `schema_version_pk` PRIMARY KEY (`version`)
DEBUG: Executing SQL: CREATE INDEX `schema_version_vr_idx` ON `test`.`schema_version` (`version_rank`)
DEBUG: Executing SQL: CREATE INDEX `schema_version_ir_idx` ON `test`.`schema_version` (`installed_rank`)
DEBUG: Executing SQL: CREATE INDEX `schema_version_s_idx` ON `test`.`schema_version` (`success`)
DEBUG: Metadata table `test`.`schema_version` created.
DEBUG: MetaData table `test`.`schema_version` successfully updated to reflect changes
Schema initialized with version: 1
[ec2-user#ec2 flyway]$ ./flyway -X migrate
/usr/bin/tput
Flyway (Command-line Tool) v.3.0
DEBUG: Adding location to classpath: /home/ec2-user/installables/flyway-3.0/bin/../jars/mysql-connector-java-5.1.26.jar
Database: jdbc:mysql://0.0.0.0:3306/test (MySQL 5.5)
DEBUG: DDL Transactions Supported: false
DEBUG: Schema: test
DEBUG: Spring Jdbc available: false
DEBUG: Validating migrations ...
DEBUG: Scanning for filesystem resources at '/home/ec2-user/installables/flyway-3.0/bin/../sql' (Prefix: 'V', Suffix: '.sql')
DEBUG: Scanning for resources in path: /home/ec2-user/installables/flyway-3.0/bin/../sql (/home/ec2-user/installables/flyway-3.0/bin/../sql)
DEBUG: Found filesystem resource: /home/ec2-user/installables/flyway-3.0/bin/../sql/V1__cr_tbl.sql
DEBUG: Found filesystem resource: /home/ec2-user/installables/flyway-3.0/bin/../sql/V2__Add_people.sql
Validated 2 migrations (execution time 00:00.032s)
DEBUG: Schema `test` already exists. Skipping schema creation.
DEBUG: Locking table `test`.`schema_version`...
DEBUG: Lock acquired for table `test`.`schema_version`
Current version of schema `test`: 1
Migrating schema `test` to version 2
DEBUG: Found statement at line 1: insert into PERSON (ID, NAME) values (1, 'Axel')
DEBUG: Found statement at line 2: insert into PERSON (ID, NAME) values (2, 'Mr. Foo')
DEBUG: Found statement at line 3: insert into PERSON (ID, NAME) values (3, 'Ms. Bar')
DEBUG: Executing SQL: insert into PERSON (ID, NAME) values (1, 'Axel')
DEBUG: Rolling back transaction...
DEBUG: Transaction rolled back
ERROR: Migration of schema `test` to version 2 failed! Please restore backups and roll back database and code!
DEBUG: MetaData table `test`.`schema_version` successfully updated to reflect changes
ERROR: Unexpected error
org.flywaydb.core.internal.dbsupport.FlywaySqlScriptException: Error executing statement at line 1: insert into PERSON (ID, NAME) values (1, 'Axel')
at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java:91)
at org.flywaydb.core.internal.resolver.sql.SqlMigrationExecutor.execute(SqlMigrationExecutor.java:73)
at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:287)
at org.flywaydb.core.internal.command.DbMigrate$5.doInTransaction(DbMigrate.java:285)
at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
at org.flywaydb.core.internal.command.DbMigrate.applyMigration(DbMigrate.java:285)
at org.flywaydb.core.internal.command.DbMigrate.access$800(DbMigrate.java:46)
at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:207)
at org.flywaydb.core.internal.command.DbMigrate$2.doInTransaction(DbMigrate.java:156)
at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:72)
at org.flywaydb.core.internal.command.DbMigrate.migrate(DbMigrate.java:156)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:864)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:811)
at org.flywaydb.core.Flyway.execute(Flyway.java:1171)
at org.flywaydb.core.Flyway.migrate(Flyway.java:811)
at org.flywaydb.commandline.Main.executeOperation(Main.java:120)
at org.flywaydb.commandline.Main.main(Main.java:88)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.PERSON' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2812)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2761)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:894)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:732)
at org.flywaydb.core.internal.dbsupport.JdbcTemplate.executeStatement(JdbcTemplate.java:235)
at org.flywaydb.core.internal.dbsupport.SqlScript.execute(SqlScript.java:89)
... 16 more
This simply means your DB does not support DDL transactions. Use a different like PostgreSQL, DB2 or SqlServer if you need that.
Now as to why your scripts are not being picked up, this is impossible to tell from your question.
Start with a getting started tutorial and once successful, adapt it to your project.

Multiple statements in single mapper method in MyBatis

I have a mapper method to delete all records in a table. But first I need to delete all records from associated junction table. This is what I tried:
#Delete("delete from COC_BLOCK_FIELD; delete from COC_BLOCK;")
void deleteBlocks();
That seemed reasonable, yet MyBatis throws an exception when processing my mapper:
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delete from COC_BLOCK' at line 1
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: delete from COC_BLOCK_FIELD; delete from COC_BLOCK;
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delete from COC_BLOCK' at line 1
I'm using MyBatis 3.2.6, MySQL 5.5.35-1ubuntu1 and MySQL Connector 5.1.26.
The problem was that I hadn't set the flag allowMultiQueries=true in the JDBC URL.

SequenceGenerator problem with unit testing in Hsqldb/H2

I have tried using both Hsqldb and H2 for unit testing but facing problem with sequence generators. Field declaration looks like this.
#Id #GeneratedValue(strategy=GenerationType.SEQUENCE, generator="keyword_seq")
#SequenceGenerator(name="keyword_seq",sequenceName="KEYWORD_ID_SEQ", allocationSize=1)
#Column(name="IM_KEYWORD_ID")
private Long keywordId;
When I try to test simple insert in this table in Hsqldb with following configuration, it gives error
<prop key="hibernate.connection.driver_class">org.hsqldb.jdbc.JDBCDriver</prop>
<prop key="hibernate.connection.url">jdbc:hsqldb:mem:testdb;sql.syntax_ora=true</prop>
Error :
Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: KEYWORD_ID_SEQ.NEXTVAL
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
With H2:
<prop key="hibernate.connection.driver_class">org.h2.Driver</prop>
<prop key="hibernate.connection.url">jdbc:h2:~/test</prop>
Error is :
Hibernate: select KEYWORD_ID_SEQ.nextval from dual
3085 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 42001, SQLState: 42001
3088 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Syntax error in SQL statement "SELECT KEYWORD_ID_SEQ.NEXTVAL FROM[*] DUAL "; expected "identifier"; SQL statement:
Any idea, how to solve this?
I think the dialect is set wrongly. Can you check your hibernate configuration.

Categories

Resources