I'm getting an exception for my Create Statement query when using hibernate-sql mapping. Here is the create statement (Postgres) and the error it complains about.
Query:
CREATE TABLE translation (
translation_id INTEGER NOT NULL DEFAULT nextval('translation_seq'::regclass) PRIMARY KEY,
category TEXT NOT NULL,
sub_category TEXT DEFAULT NULL,
prod_id INTEGER NOT NULL REFERENCES product(product_id),
date_created DATE DEFAULT now() NOT NULL,
expired BOOLEAN DEFAULT FALSE NOT NULL
);
Exception / failure in the Test:
java.lang.RuntimeException: HypersonicAccessor.dbInitSchema() failure.
at com.package1.package2.package3.database.HypersonicAccessor.dbInitSchema(HypersonicAccessor.java:210)
at com.package1.package2.package3.someDbConfig.initialize(ProdDbConfig.java:145)
at com.package1.package2.package3.backend.client.UpdaterTest.setUp(UpdaterTest.java:57)
Caused by: java.sql.SQLException: Unexpected token: REFERENCES in statement
[CREATE TABLE TRANSLATION (TRANSLATION_ID BIGINT NOT NULL PRIMARY KEY,
CATEGORY VARCHAR NOT NULL, SUB_CATEGORY VARCHAR DEFAULT NULL,
PROD_ID BIGINT NOT NULL REFERENCES]
at com.package1.package2.package3.database.HypersonicDatabase.execute(HypersonicDatabase.java:146)
at com.package1.package2.package3.database.HypersonicDatabase.runSqlScript(HypersonicDatabase.java:169)
at com.package1.package2.package3.database.HypersonicAccessor.runScriptList(HypersonicAccessor.java:123)
at com.package1.package2.package3.database.HypersonicAccessor.dbInitSchema(HypersonicAccessor.java:157)
Im not sure if my query is wrong or something else is. Any clue what could possibly be wrong ? This actually is the same issue with a few of my tests that previously were passing but now they are not.
Related
Getting an error when trying to fetch table meta data.
Using:
JOOQ 3.14.0,
PostgeSQL 12,
PostgreSQL driver 42.2.10
Example:
#RestController
#ApiVersion("v2")
public class TypeResource {
private final DSLContext ctx;
#GetMapping(value = "/test")
public void test(#RequestParam String table) {
ctx.meta().getTables(table);
}
}
Error (This is not the whole error but the rest is similar):
17:16:44.808 [http-nio-8181-exec-5] [WARN ] org.jooq.impl.MetaImpl - Default value : Could not load default value: '{}'::character varying[] for type: varchar ([Ljava.lang.String;)
org.jooq.exception.DataTypeException: Cannot convert from '{}'::character varying[] (class java.lang.String) to class [Ljava.lang.String;
at org.jooq.tools.Convert$ConvertAll.fail(Convert.java:1303)
at org.jooq.tools.Convert$ConvertAll.from(Convert.java:1192)
at org.jooq.tools.Convert.convert0(Convert.java:392)
at org.jooq.tools.Convert.convert(Convert.java:384)
at org.jooq.tools.Convert.convert(Convert.java:458)
at org.jooq.impl.AbstractDataType.convert(AbstractDataType.java:534)
at org.jooq.impl.DefaultDataType.convert(DefaultDataType.java:86)
at org.jooq.impl.DSL.val(DSL.java:24373)
at org.jooq.impl.DSL.inline(DSL.java:23891)
at org.jooq.impl.MetaImpl$MetaTable.init(MetaImpl.java:910)
at org.jooq.impl.MetaImpl$MetaTable.<init>(MetaImpl.java:560)
at org.jooq.impl.MetaImpl$MetaSchema.getTables(MetaImpl.java:421)
at org.jooq.impl.MetaImpl.getTables0(MetaImpl.java:217)
at org.jooq.impl.AbstractMeta$4.iterator(AbstractMeta.java:194)
at org.jooq.impl.AbstractMeta.get(AbstractMeta.java:340)
at org.jooq.impl.AbstractMeta.initTables(AbstractMeta.java:191)
at org.jooq.impl.AbstractMeta.getTables(AbstractMeta.java:172)
at org.jooq.impl.AbstractMeta.getTables(AbstractMeta.java:167)
17:16:44.843 [http-nio-8181-exec-5] [WARN ] org.jooq.impl.MetaImpl - Default value : Could not load default value: '{{0,0}}'::numeric[] for type: numeric ([Ljava.math.BigDecimal;)
org.jooq.exception.DataTypeException: Cannot convert from '{{0,0}}'::numeric[] (class java.lang.String) to class [Ljava.math.BigDecimal;
at org.jooq.tools.Convert$ConvertAll.fail(Convert.java:1303)
at org.jooq.tools.Convert$ConvertAll.from(Convert.java:1192)
at org.jooq.tools.Convert.convert0(Convert.java:392)
at org.jooq.tools.Convert.convert(Convert.java:384)
at org.jooq.tools.Convert.convert(Convert.java:458)
at org.jooq.impl.AbstractDataType.convert(AbstractDataType.java:534)
at org.jooq.impl.DefaultDataType.convert(DefaultDataType.java:86)
at org.jooq.impl.DSL.val(DSL.java:24373)
at org.jooq.impl.DSL.inline(DSL.java:23891)
at org.jooq.impl.MetaImpl$MetaTable.init(MetaImpl.java:910)
at org.jooq.impl.MetaImpl$MetaTable.<init>(MetaImpl.java:560)
at org.jooq.impl.MetaImpl$MetaSchema.getTables(MetaImpl.java:421)
at org.jooq.impl.MetaImpl.getTables0(MetaImpl.java:217)
at org.jooq.impl.AbstractMeta$4.iterator(AbstractMeta.java:194)
at org.jooq.impl.AbstractMeta.get(AbstractMeta.java:340)
at org.jooq.impl.AbstractMeta.initTables(AbstractMeta.java:191)
at org.jooq.impl.AbstractMeta.getTables(AbstractMeta.java:172)
at org.jooq.impl.AbstractMeta.getTables(AbstractMeta.java:167)
Example table create script :
CREATE TABLE public.user_role
(
id bigint NOT NULL DEFAULT nextval('user_role_id_seq'::regclass),
user_id bigint NOT NULL,
role_id bigint NOT NULL,
ts_created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
ts_updated timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_by_user character varying(256) COLLATE pg_catalog."default" NOT NULL DEFAULT 'n/a'::character varying,
updated_by_process character varying(64) COLLATE pg_catalog."default" NOT NULL DEFAULT 'n/a'::character varying,
CONSTRAINT user_role_pkey PRIMARY KEY (id),
CONSTRAINT x_role_id_fk FOREIGN KEY (role_id)
REFERENCES public.role (id) MATCH FULL
ON UPDATE NO ACTION
ON DELETE NO ACTION,
CONSTRAINT x_user_id_fk FOREIGN KEY (user_id)
REFERENCES public."user" (id) MATCH FULL
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
TABLESPACE pg_default;
ALTER TABLE public.user_role
OWNER to postgres;
CREATE UNIQUE INDEX x_role_user_id_role_id_uq
ON public.user_role USING btree
(user_id ASC NULLS LAST, role_id ASC NULLS LAST)
TABLESPACE pg_default;
Initially my goal was to get the table indexes and this is working fine.
I tried to update JOOQ from 3.13.1 to 3.14.0 and tested with different tables in the database but without any luck.
This is a bug which will be fixed in jOOQ 3.15 via https://github.com/jOOQ/jOOQ/issues/8469. jOOQ currently assumes that DatabaseMetaData column descriptions produce values for DEFAULT expressions, instead of expressions.
The bug is "cosmetic", as it only affects your logs. The field is still produced correctly (without any DEFAULT expression). You can mute the message in your logger configuration until the above bug is fixed.
I have been getting this error when I'm starting my application which on start up reads some data from a table:
org.h2.jdbc.JdbcSQLException: Unknown data type: "ENUM"; SQL statement:
CREATE CACHED TABLE PUBLIC.POWERPLANT(
POWERPLANTID INT NOT NULL,
ORGNAME VARCHAR(25) NOT NULL,
ISACTIVE BOOL NOT NULL,
MINPOWER INT NOT NULL,
MAXPOWER INT NOT NULL,
RAMPRATE INT,
RAMPRATESECS INT,
POWERPLANTTYPE ENUM('OnOffType','RampUpType'),
CREATEDAT TIMESTAMP NOT NULL,
UPDATEDAT TIMESTAMP NOT NULL
) [50004-186]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.parseColumnWithType(Parser.java:3990)
at org.h2.command.Parser.parseColumnForTable(Parser.java:3853)
at org.h2.command.Parser.parseCreateTable(Parser.java:5761)
at org.h2.command.Parser.parseCreate(Parser.java:4149)
at org.h2.command.Parser.parsePrepared(Parser.java:350)
at org.h2.command.Parser.parse(Parser.java:305)
at org.h2.command.Parser.parse(Parser.java:277)
Here is my connection string:
db.url = "jdbc:h2:~/path/to/db/file;MODE=MySQL;INIT=CREATE DOMAIN IF NOT EXISTS enum as VARCHAR(255);IFEXISTS=TRUE"
Any idea as to how to solve this?
I've solved the same problem by updating h2 Maven library version to 1.4.200 instead of 1.4.187
Here is how I solved it. The Enum type should be written like this!
powerPlantType VARCHAR(25) check (powerPlantType in ('OnOffType', 'RampUpType')),
I am getting error in this: prepared statement.
Prepared Statement:
PreparedStatement pStatement=connection.prepareStatement
("CREATE TABLE `details`.?(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL);
INSERT INTO `testdetails`.? (`ID`) VALUES (?);");
pStatement.setString(1, "usrname");
pStatement.setString(2, "usrname");
pStatement.setString(3, "001");
pStatement.executeUpdate();
Error details:
Severe: 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
''usrname'(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL);INSER' at line 1
How to fix this error?
JDBC parameters aren't usually usable for table and column names - they're only for values.
Unfortunately, I think this is one of those cases where you will need to be build the SQL dynamically. You will want to be very careful about which values you allow, of course.
This error appears because, you use #setString, and the string gets wrapped by ''.
To fix this, you can use one of this snippets:
"CREATE TABLE `details`.#tableName#(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL); INTO `testdetails`.#tableName# (`ID`) VALUES (?);".replaceAll("#tableName#", "usrname");
or
new StringBuilder("CREATE TABLE `details`.").append("usrname").append("(`ID` VARCHAR(255) NOT NULL,`Score` VARCHAR(255) NULL); INTO `testdetails`.").append("usrname").append("(`ID`) VALUES (?);").toString();
and pass resulted string into #prepareStatement() method.
I have the following mysql query in PHP and it works fine.
$strUpdate = "INSERT INTO `batchfolder`.`newbatch` (`BatchID` ,`Batch` ,`barcode` ,`PG`)VALUES (NULL , '', '1', '')";
and is also fine when i run it directly in database.
However when I run it in java,
try {
conn = DriverManager.getConnection (url, userName, password);
Statement st = conn.createStatement();
st.execute("INSERT INTO `batchfolder`.`newbatch` (`BatchID` ,`Batch` ,`barcode` ,`PG`)VALUES (NULL , '', '1', '')");
st.close();
}
It gives the following error.
Exception in thread "main" java.sql.SQLException: Field 'Pre' doesn't have a default value
Pre is the the next row in the database and it does not have a default value.
My question is, then how does this query run fine in mysql and php.
P.S BatchID is an int(10) autoincremented value in newbatch.
This is my table structure.
CREATE TABLE IF NOT EXISTS `newbatch` (
`BatchID` int(10) NOT NULL AUTO_INCREMENT,
`Batch` varchar(100) NOT NULL,
`barcode` varchar(5) NOT NULL,
`Ly` varchar(5) NOT NULL DEFAULT '0',
`PG` varchar(5) NOT NULL,
`Pre` varchar(5) NOT NULL,
`Flu` varchar(5) NOT NULL,
`FluID` varchar(100) DEFAULT NULL,
`DateCreated` varchar(100) DEFAULT NULL,
`Comments` varchar(500) DEFAULT NULL,
PRIMARY KEY (`BatchID`),
UNIQUE KEY `FluID` (`FluID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1000160 ;
Are you absolutely sure this actually runs in PHP? You may just not be seeing the error message.
Try this again with display_errors = 'on' and error_reporting(E_ALL | E_STRICT);
This is a SQL error, it doesn't depend on the driver. The driver just displays the message to you.
UPDATE:
It looks like Java is turning off MySQL's strict mode for some reason. The only thing that (to my knowledge) should be able to cause this behavior is MySQL strict mode being off.
If strict mode is not enabled, MySQL sets the column to the implicit
default value for the column data type.
You can check the mode that your server is running on by SELECT ##GLOBAL.sql_mode;. Try that in both Java and PHP. If the results differ then that's your answer.
UPDATE2: Jep!
Looked at your table definition, you have Pre defined as not null, but you didnt specify a value for it in the insert, so it should show an error proper.
You have several database fields that are 'NOT NULL', so you must specify these in your INSERT statement. I couldn't imagine how this would work in PHP either.
My java model is causing these warnings in my logs...
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE users (id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, last_login TIMESTAMP, name VARCHAR(255), PRIMARY KEY (id))": java.sql.SQLException: Table/View 'USERS' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE GUEST (ID BIGINT NOT NULL, NAME VARCHAR(255), PRIMARY KEY (ID))": java.sql.SQLException: Table/View 'GUEST' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE content (id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, body CLOB(2147483647), user_id INTEGER, PRIMARY KEY (id))": java.sql.SQLException: Table/View 'CONTENT' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50) NOT NULL, SEQ_COUNT DECIMAL(15), PRIMARY KEY (SEQ_NAME))": java.sql.SQLException: Table/View 'SEQUENCE' already exists in Schema 'APP'.
WARNING: PER01000: Got SQLException executing statement "INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0)": java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL111211163034730' defined on 'SEQUENCE'.
I actually have a couple of questions here...
users and content tables I created already, and used Netbeans to generate my models.
How come it's trying to create the tables when the models were created from the tables?
Secondly, I have no idea where sequence is coming from?