Spring boot unable to run schema.sql - spring-boot

I am on spring boot 2.3, and have following in my schema.sql
CREATE TABLE customer (id SERIAL PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255));
Pom file has h2 dependency.
When I run application, I get error:
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "CUSTOMER" not found; SQL statement:
INSERT INTO customer (first_name, last_name) VALUES ($1, $2) [42102-200]
Is some change needed in application.properties to make spring boot execute schema.sql?

You can drop table if exist before creation :
DROP TABLE IF EXISTS customer;
CREATE TABLE customer (id SERIAL PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255));

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.

error creating table and also MysqlInnoBDailect is not coming in propeties

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table dept (dept_id bigint not null, dep_name varchar(255), hod varchar(255), primary key (dept_id)) type=InnoDB" via JDBC Statement

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?

How can I mapping Tables embeded in spring boot jpa?

i have create two table in sql like this:
create table dbo.person(
id int not null identity(1,1),
code nvarchar(50),
name nvarchar(250),
PRIMARY KEY (Id)
)
create table dbo.employ(
personId int not null,
age smallint,
sex bit,
PRIMARY KEY (personId),
FOREIGN KEY (PersonId) REFERENCES Persons(Id)
)
how can i pmapping this tables in spring boot jpa?

Spring H2 created table not found after successful creation

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

Resources