Spring H2 created table not found after successful creation - spring

I am using Spring Boot's schema.sql magic to create an in memory H2 database. The script contains the following statements:
create table PERSON
(
ID BIGINT not null primary key,
NAME VARCHAR(255) not null
);
create index IDX_PERSON_NAME on PERSON (NAME);
Upon launch Spring Boo fails with the following exception:
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #2 of URL [file:/D:/git/.../build/resources/main/schema.sql]: create index IDX_PERSON_NAME on PERSON (NAME); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "PERSON" not found; SQL statement:
create index IDX_PERSON_NAME on PERSON (NAME) [42102-200]
How can the statement fail to find the table that was created in the preceding statement?

Simply because NAME is not mentioned as Unique constraint, you have only specified it as NOT NULL constraint
create table PERSON
(
ID BIGINT not null primary key,
NAME VARCHAR(255) not null,
CONSTRAINT Person_Name_Unique UNIQUE (NAME)
);
create index IDX_PERSON_NAME on PERSON (NAME);

Here are the possible reasons,
The schema needs to be mentioned on while referring to table like test_schema.person
Your syntax for creating the index might be wrong. Refer to this link for H2 Syntax,https://www.baeldung.com/spring-yaml

Related

How to use keyword VALUE as a field name in H2 database

I inherited a project where in UnitTests an H2 database is being used. On the startup of the project a lot of tables are created and populated with data.
CREATE TABLE SRC_FIELD_VALUE
( ID NUMBER IDENTITY,
INPUT_SRC_FIELD_REF_ID NUMBER,
VALUE VARCHAR2(255),
DESCRIPTION VARCHAR2(255)
);
We recently migrated to the latest version of H2 : 2.1.214
In my POM.XML I added "NON_KEYWORDS=VALUE,ID" to the end of definition but still got an exception :
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "CREATE TABLE SRC_FIELD_VALUE ( ID NUMBER IDENTITY, INPUT_SRC_FIELD_REF_ID NUMBER, [*]VALUE VARCHAR2(255), DESCRIPTION VARCHAR2(255) )"; expected "identifier"; SQL statement:
CREATE TABLE TC_RULE_COMP_DATA ( ID NUMBER IDENTITY, RULE_REF_ID NUMBER, SORT_ORDER NUMBER, VALUE CLOB ) [42001-214]
I also tried to put double quote around "VALUE" but got the same exception. It's interesting that "ID" is not getting considered as KEYWORD though.
What are my options here? I can't rename the fields.

Springboot test fail because of table not found from H2 database, but the table has been created

The problem that I can see is that the table is created with the name 'article' and the query is done with ARTICLE in capital letter. I tried to change the application.properties file to this:
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MySQL;NON_KEYWORDS=USER;IGNORECASE=TRUE;CASE_INSENSITIVE_IDENTIFIERS=TRUE;DATABASE_TO_LOWER=TRUE
but it doesn't help the problem.
Here is some output from the console that demonstrate the table has been created:
Hibernate: create table "article" ("id" bigint generated by default as identity, "content" clob, "language" integer, "last_edited" timestamp, "published" timestamp, "title" varchar(255), "image_id" bigint, "user_id" bigint not null, primary key ("id"))
and later this pops up:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ARTICLE" not found; SQL statement: select * from Article where language=? ORDER BY published desc
I tried to name the table in uppercase letter with
DATABASE_TO_LOWER=FALSE, DATABASE_TO_UPPER=TRUE;
and it didn't help either.
What could I try next?

Oracle NoSQL - what does mean : Table exists but definitions do not match

I run into this issue:
Caused by: java.lang.IllegalArgumentException:
Error: User error in query:
CREATE TABLE failed for table TEST:
Table exists but definitions do not match
I added 2 new columns in my existing database using console, and added them in table creation statement. It hits the error when I run creating table again.
I checked everything, they all match, (name, type…)
the order of columns matter when you create a table. Did you validate this ? The IF NOT EXISTS in the CREATE TABLE clause checks for the same structure of the table. Please refer the documentation below for more details.https://docs.oracle.com/en/database/other-databases/nosql-database/21.1/sqlreferencefornosql/create-table.html
This inform you that the table that you are trying to create exits but the structure is not the same. It could be useful.
A sample to show the error when order of columns is changed:
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement completed successfully
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement did not require execution
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, lastName STRING,
firstName STRING,
PRIMARY KEY (id));
Error handling command CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER,
lastName STRING,
firstName STRING,
PRIMARY KEY (id)): Error: User error in query: CREATE TABLE failed for table testing_tbl: Table exists but definitions do not match

Can't use HSQL sequences in JUnit

I'm trying to run a JUnit test class for my Spring-Batch application. I had to create the following tables: http://static.springsource.org/spring-batch/reference/html/metaDataSchema.html. I create them in my initialization database script, including the following:
CREATE SEQUENCE BATCH_STEP_EXECUTION_SEQ;
CREATE SEQUENCE BATCH_JOB_EXECUTION;
CREATE SEQUENCE BATCH_JOB_SEQ;
The creation of all sequences and tables didn't generate any errors. But while executing my JUnit test, I get the following error:
org.springframework.dao.DataAccessResourceFailureException: Could not obtain identity(); nested exception is java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: BATCH_JOB_SEQ
at org.springframework.jdbc.support.incrementer.HsqlMaxValueIncrementer.getNextKey(HsqlMaxValueIncrementer.java:119)
That is caused when the class HsqlMaxValueIncrementer runs:
stmt.executeUpdate("insert into " + getIncrementerName() + " values(null)");
What am I doing wrong?
Thanks!!
For HSQLDB the 3 tables need the ID field.
CREATE TABLE BATCH_STEP_EXECUTION_SEQ (
ID BIGINT IDENTITY
);
CREATE TABLE BATCH_JOB_EXECUTION_SEQ (
ID BIGINT IDENTITY
);
CREATE TABLE BATCH_JOB_SEQ (
ID BIGINT IDENTITY
);
Reference: https://github.com/SpringSource/spring-batch/blob/master/spring-batch-core/src/main/resources/org/springframework/batch/core/schema-hsqldb.sql

Table creation with h2 database

I am new to h2.I just using h2 in spring embedded mode with hibernate.I am trying to execute the following scripts using h2.
CREATE TABLE acct_authority (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
value varchar(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
The table acct_authority is created without any error.But if i create another table with the following script.
CREATE TABLE acct_role (
id bigint(20) NOT NULL auto_increment,
name varchar(255) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY name (name)
);
It shows error as constraint name allready exists.What mistake i did.
You tried to create two constraints with same name. As you see, both CREATE TABLE statements contain following:
UNIQUE KEY name (name)
Result is that first one creates constraint named name, and second one fails because constraint name already exists. Problem can be solved by using unique names. Also in general it makes sense have little bit more descriptive names for database objects. Maybe you can use for example something like following:
UNIQUE KEY acct_authority_name_UNIQUE (name)
...
UNIQUE KEY acct_role_name_UNIQUE (name)

Resources