I have an application built with Sprigboot (2.4.5) Spring JPA and Eclipselink. It connects to MariaDB. The CREATE table statements the application is generating are structured as VARCHAR without a size. However, I would have expected expect VARCHAR(255). MariaDB rejects the VARCHAR statements and fails to create tables. I am sure I am missing something really really obvious here - so I apologize in advance! But hoping for some guidance. This is an example of the generated code:
CREATE TABLE api_user (ID NUMBER(19) NOT NULL, apiKey VARCHAR, assignDate TIMESTAMP, code VARCHAR UNIQUE, disabled VARCHAR, expiryDate TIMESTAMP, name VARCHAR, version NUMBER(19), PRIMARY KEY (ID))
eclipselinkDdlGeneration=drop-and-create-tables
eclipselinkDdlGenerationOutputMode=database
eclipselinkJdbcUppercaseColumns=false
eclipselinkWeaving=false
dbDriver=org.mariadb.jdbc.Driver
Related
After upgrading to Boot 2.7 the integration tests that were using an embedded H2 database started failing.
I see this WARN message in the logs, but it's not very clear the cause or the solution for this:
WARN 8053 ---[ main] o.h.t.s.i.ExceptionHandlerLoggedImpl :GenerationTarget encountered exception accepting command : Error executing DDL "create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))" via JDBC Statement
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))" via JDBC Statement
...
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "create table [*]user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id))"; expected "identifier"; SQL statement:
create table user (id bigint generated by default as identity, email varchar(255) not null, name varchar(255), primary key (id)) [42001-212]
...
It seems my User table is not created after the upgrade, thus making my tests fail.
It seems Boot 2.7 upgraded to its H2 dependency to 2.x, which is not backward compatible and introduces several changes:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.7-Release-Notes#h2-21
The issue was that User is now a Keyword / reserved word (The H2 "migration-to-v2" guide was not very helpful in my case; it mentioned new keywords were added, but it didn't provide a link to ):
https://www.h2database.com/html/advanced.html#keywords
So, what I had to do is use "quoted names" to define the Table name of my Entity (it seems I can use backticks in the table annotation too instead of escaping double quotes):
#Table(name="\"user\"")
#Entity
public class User {
...
I also had to use double quotes on my data.sql files for this table:
INSERT INTO "user"(id, email, name) VALUES(1, 'test#user.com', 'Test User');
Note: the migration guide also mentions the possibility of using the SET NON_KEYWORDS command as a workaround, but it also discourages it.
Add the following to your src/test/resources/application-test.properties file (assuming your tests run with the test profile):
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
If any of your JPA entities have UUID fields, ensure those fields are annotated with #Column and the annotation's columnDefinition defines the column as type UDID. (In its simplest form: #Column(columnDefinition="UDID").) This works around a Hibernate bug.
It could be because of Identity columns in schema.sql.Identity columns should be normally declared with GENERATED BY DEFAULT AS IDENTITY.
Ex: JOB_EXECUTION_ID BIGINT IDENTITY -- should be changed as JOB_EXECUTION_ID BIGINT GENERATED BY DEFAULT AS IDENTITY.
Refer http://www.h2database.com/html/migration-to-v2.html for more such changes occurred due to upgrade in H2 version.
I am trying to create table as below,
create table "customer`£`s"(customer_id number(10) not null,
UNIT VARCHAR2(50) not null,
city varchar2(50), constraint customers_pk PRIMARY KEY(customer_id));
But getting Invalid character error.
Can anyone help?
We are working on similar projects. I was able to create the table.
I have very simple spring boot app which is using h2 database on local. I have schema-h2.sql which has below script -
CREATE TABLE USERS(
userid NUMBER(10,0) AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(256) UNIQUE NOT NULL,
fname VARCHAR(256),
lname VARCHAR(256)
);
And in application-default.properties -
spring.h2.console.enabled=true
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:testdb;MODE=MSSQLServer;DB_CLOSE_DELAY=-1;DB_CLO SE_ON_EXIT=false
When I look at /h2-console, I can see table being created but it is missing unique constraint on email column.
I even tried adding alter statement in schema-h2.sql script to create constraint but that doesn't work either -
ALTER TABLE USERS ADD CONSTRAINT UQ_EMAIL UNIQUE(email);
Also, when run those script in h2-console it creates unique constraint correctly so it does seem like something to do with how spring executes them.
When debugging I can see that Alter statement gets executed without any exception in ScriptUtils class.
Not sure how to take this forward so any help will be greatly appreciated.
For anyone else coming across this, I was able to fix it by altering my JPA annotation. Adding the unique parameter in addition to creating the constraint in h2-schema.sql allowed the constraint to actually be created.
#Column(name = "email", unique = true, nullable = false)
private String email;
If schema is beign created by spring boot with schema.sql file, make sure to disable hibernate schema auto create:
spring.jpa.hibernate.ddl-auto=none
Otherwise, Hibernate will overwritte it everytime the app starts.
I'm new in using in-memmory database.
I tried to use H2 database for developing project with spring boot, spring data JPA, but i got a problem when initialize application.
Caused by: org.h2.jdbc.JdbcSQLException: Unknown data type: "FK_PERSON__PERSONTYPE_IDX"; SQL statement:
Because this script was exported from MySQL. So i thinked there are some wrong syntax which H2 does not understand
For example, this is a part of script:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(255) NOT NULL,
`lastname` varchar(255) DEFAULT NULL,
`type` int(11) NOT NULL,
`address` text,
PRIMARY KEY (`id`),
KEY `fk_person__persontype_idx` (`type`),
CONSTRAINT `fk_person__persontype` FOREIGN KEY (`type`) REFERENCES `persontype` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' ';
And i tried some solutions from these:
Convert MySQL script to H2
http://matthewcasperson.blogspot.de/2013/07/exporting-from-mysql-to-h2.html
Replace symbols with double quotes, single quotes,... even not use quote at all but not working.
Please show me why?
Thank you.
I had a similar issue.
Removing the "KEY" line, which in your example corrosponds to:
KEY `fk_person__persontype_idx` (`type`),
worked for me.
does DB2 support enums? I didn't really find anything online.
The Query doesn't work:
create table prototype.test(id int not null primary key, level ENUM('upper', 'lower') not null);
Thanks in advance!
You can create a check constraint for that.
alter table prototype.test add constraint checklevel check (level in ('upper', 'lower'));
Or you can include this in the create table:
create table prototype.test(
id int not null primary key,
level varchar(5) check (level in ('upper', 'lower')
);
No DB2 does not support ENUMS. The are some database which I am aware of which suports Enums is MySql and Postgresql but DB2 for sure does not support it.