Java Prepared statement error table not found - java

I've got a problem with a data migration job build with Talend Open Studio for Data Integration. Talend open studio generates java classes. Im migration from one mysql table to another.
The statement:
String insert_tMysqlOutput_1 = "INSERT INTO `"
+ "workitem"
+ "` (`name`,`type`,`budgetHours`,`deadline`,`dateStart`,`dateEnd`,`dateCreated`,`projectId`,`parentWorkItem`,`description`,`dateUpdated`,`companyId`,`state`,`reporterId`,`assigneeId`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
java.sql.PreparedStatement pstmt_tMysqlOutput_1 = conn_tMysqlOutput_1
.prepareStatement(insert_tMysqlOutput_1);
The error:
Exception in component tMysqlOutput_1 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'dashboard.work_item' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3566)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3498)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2113)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2409)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2327)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2312)
at dashboard_new.copy_of_workitem_2_0.Copy_of_workitem.tMysqlInput_1Process(Copy_of_workitem.java:3677)
at dashboard_new.copy_of_workitem_2_0.Copy_of_workitem.tMysqlConnection_2Process(Copy_of_workitem.java:1109)
at dashboard_new.copy_of_workitem_2_0.Copy_of_workitem.runJobInTOS(Copy_of_workitem.java:7370)
at dashboard_new.copy_of_workitem_2_0.Copy_of_workitem.main(Copy_of_workitem.java:7069)
Table structure (EDIT #1)
CREATE TABLE `dashboard`.`workitem` (
`workItemId` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`type` varchar(255) NOT NULL,
`projectId` bigint(20) NOT NULL DEFAULT '0',
`companyId` bigint(20) DEFAULT NULL,
`budgetHours` decimal(10,0) DEFAULT NULL,
`deadline` datetime DEFAULT NULL,
`dateStart` datetime DEFAULT NULL,
`dateEnd` datetime DEFAULT NULL,
`parentWorkItem` bigint(20) DEFAULT NULL,
`description` text,
`state` varchar(45) DEFAULT NULL,
`reporterId` bigint(20) DEFAULT NULL,
`assigneeId` bigint(20) DEFAULT NULL,
`dateCreated` datetime DEFAULT NULL,
`dateUpdated` datetime DEFAULT NULL,
`newColumn` varchar(10) DEFAULT NULL,
PRIMARY KEY (`workItemId`),
KEY `fk_project_id_idx` (`projectId`),
KEY `fk_parent_work_item_idx` (`parentWorkItem`),
KEY `fk_workitem_company1_idx` (`companyId`),
KEY `fk_workitem_name` (`name`),
KEY `fk_workitem_assignee` (`assigneeId`),
KEY `fk_workitem_reporter` (`reporterId`),
CONSTRAINT `fk_workitem_company1` FOREIGN KEY (`companyId`) REFERENCES `company` (`companyId`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2461 DEFAULT CHARSET=latin1
The strange thing is.. work_item is not found in the code.. so i think there must by some caching of the statement but have no idea where..
EDIT #1:
And when i add a field into the query that does not exist inside the table then i get an error field not found.. so somehow check if the fields do exists.
Does anyone have any ideas?
Thanks

Something is accessing work_item, but the table's name is workitem.
Perhaps there is a trigger on the table that has a coding error in it.
Or perhaps you have not posted the latest versions.

Related

MySQL illegal mix of collations for operation '=' on a simple INSERT

There is a lot of questions about collation mixup in SELECT and INSERT * SELECT statements. Mine is a simple INSERT with nothing added to it but the data.
The error spawned in the process is:
Illegal mix of collations (utf8_unicode_ci,IMPLICIT) and (utf8_general_ci,IMPLICIT) for operation '='
All field names and table names have been simplified. The query being executed is:
INSERT INTO table1 (`a`,`b`,`c`,`d`,`e`,`f`,`g`,`h`,`i`,`j`) VALUES (?,?,?,?,?,?,?,?,?,?);```
It has been prepared to receive some data that is brought in by the Java process with JDBI.
The underlying tables have nothing odd about them. Their DDLs look as follows.
CREATE TABLE `table1` (
`a` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`b` varchar(6) COLLATE utf8_unicode_ci DEFAULT NULL,
`c` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`d` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`e` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`f` int(11) DEFAULT NULL,
`g` text COLLATE utf8_unicode_ci,
`h` bigint(20) DEFAULT NULL,
`i` bigint(20) DEFAULT NULL,
`j` datetime DEFAULT NULL,
`k` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`a`,`e`),
KEY `c` (`c`,`d`),
CONSTRAINT `fk_1` FOREIGN KEY (`a`) REFERENCES `table2` (`a2`),
CONSTRAINT `fk_2` FOREIGN KEY (`c`, `d`) REFERENCES `table3` (`a3`, `b3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `table2` (
`a2` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`b2` varchar(32) COLLATE utf8_unicode_ci DEFAULT NULL,
`c2` mediumtext COLLATE utf8_unicode_ci,
`d2` mediumtext COLLATE utf8_unicode_ci,
`e2` bigint(11) DEFAULT '0',
PRIMARY KEY (`a2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
CREATE TABLE `table3` (
`a3` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`b3` varchar(32) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`a3`,`b3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
As can be seen, there is absolutely nothing about the collation that I can understand. Is it possible that the data coming in from the java world is causing this? Could it be related to the current connection session variables?
UPDATE
I forgot to add the MySQL version: 5.6
I have solved this issue in my test runs moving all field collations to DEFAULT and the table collation to utf8_general_ci on table1. However, this is an unacceptable solution to the problem and it does not come close to answering how can there be a collation mixup in a simple INSERT statement?
I found the culprit. There is a TRIGGER on this table that references another table from another schema. The conflict exists in the trigger statements which does not help that the error message makes no allusion to such fact.
That answers
how can there be a collation mixup in a simple INSERT statement?
There is a TRIGGER accompanying the INSERT

Error Code: 1215 - Cannot add foreign key constraint

I am running this Query on MySQL workbench with the latest version of MySQL installed on MacOS.
My SQL Query is:
CREATE DATABASE IF NOT EXISTS `imdb`;
USE `imdb`;
DROP TABLE IF EXISTS `rating`;
DROP TABLE IF EXISTS `media`;
CREATE TABLE `media` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(70) DEFAULT NULL,
`year` varchar(70) DEFAULT NULL,
`rated` varchar(70) DEFAULT NULL,
`released` varchar(70) DEFAULT NULL,
`runtime` varchar(70) DEFAULT NULL,
`genre` varchar(70) DEFAULT NULL,
`director` varchar(70) DEFAULT NULL,
`writer` varchar(70) DEFAULT NULL,
`actors` varchar(70) DEFAULT NULL,
`plot` varchar(7000) DEFAULT NULL,
`language` varchar(70) DEFAULT NULL,
`country` varchar(70) DEFAULT NULL,
`awards` varchar(70) DEFAULT NULL,
`poster` varchar(270) DEFAULT NULL,
`metascore` varchar(70) DEFAULT NULL,
`imdb_rating` varchar(70) DEFAULT NULL,
`imdb_votes` varchar(70) DEFAULT NULL,
`imdb_id` varchar(70) NOT NULL,
`type` varchar(70) DEFAULT NULL,
`dvd` varchar(70) DEFAULT NULL,
`box_office` varchar(70) DEFAULT NULL,
`production` varchar(70) DEFAULT NULL,
`website` varchar(70) DEFAULT NULL,
`response` varchar(70) DEFAULT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `rating` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`source` varchar(70) DEFAULT NULL,
`value` varchar(70) DEFAULT NULL,
`imdb_id` varchar(70) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`imdb_id`)
REFERENCES `media`(`imdb_id`)
ON DELETE SET NULL
);
SET FOREIGN_KEY_CHECKS = 1;
The create Query on table rating is failing. I don't know why, any help would be appreciated.
The exact error response is:
10:12:44
CREATE TABLE `rating` ( `id` int(11) NOT NULL AUTO_INCREMENT, `source` varchar(70) DEFAULT NULL, `value` varchar(70) DEFAULT NULL, `imdb_id` varchar(70) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (`imdb_id`) REFERENCES `media`(`imdb_id`) ON DELETE SET NULL )
Error Code: 1215.
Cannot add foreign key constraint
0.123 sec
I am building a Spring REST application that uses one too many relationships for building a local copy of the IMDB database or something similar.
A foreign key in MySQL doesn't actually have to reference a primary key column, but it does have to reference a column which is unique. So, one possible fix here would be to add a unique constraint on the imdb_id column:
ALTER TABLE media ADD CONSTRAINT cstr_imdb UNIQUE (imdb_id);
A foreign key is a column or group of columns in a relational database table that provides a link between data in two tables. It acts as a cross-reference between tables because it references the primary key of another table, thereby establishing a link between them.
'id' is the primary key in your table 'media' and not 'imdb_id'. Hence, you can't make a reference to 'imdb_id'.
imdb_id is not a key on the media table - foreign keys must reference keys (or, at least, unique constraints). If imdb_id on the media table should be unique, then you could put a UNIQUE CONSTRAINT on it and will be able to reference it with a foreign key.
Also, you have on delete set null on a column that cannot be null

spring boot many to many design using jpa

I am developing a sample project between users and products. A user can sell multiple products and a product can be sold by multiple users. So I designed my db with a many to many relationship. Below are my tables:
create table `users` (
`id` bigint(20) not null auto_increment,
`first_name` varchar(100) not null,
`last_name` varchar(100) not null,
`email` varchar(100) not null,
`password` varchar(255) not null,
`phone_number` varchar(20),
`created_at` timestamp default CURRENT_TIMESTAMP,
`updated_at` timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key(`id`),
unique(`email`)
);
create table `products` (
`id` bigint(20) not null auto_increment,
`name` varchar(255) not null,
primary key(`id`),
unique(`name`)
);
create table `users_products` (
`user_id` bigint(20) not null,
`product_id` bigint(20) not null,
`quantity`int not null,
`price` DECIMAL(5, 2) not null,
`description` text not null,
`created_at` timestamp default current_timestamp,
`updated_at` timestamp default current_timestamp on update current_timestamp,
primary key (`user_id`, `product_id`),
foreign key(`user_id`) references `users`(`id`) on delete cascade,
foreign key(`product_id`) references `products`(`id`) on delete restrict
);
As you can see above, the products table name is unique because users can sell the same products.
Now I ask the question how this can be achieved using spring boot with JPA. I have seen tutorials with many to many with extra columns but the tutorials have just had an extra column with date time. But I want to perssit with the extra columns as mentioned above in the users_products table.
And also is the schema designed ok?

One to One Mapping between two tables in Hibernate annotations

I have two tables
CREATE TABLE `cb_jobs` (
`JOB_IDENTIFIER` INT(11) NOT NULL,
`CB_CREDENTIAL_TYPE_IDENTIFIER` INT(11) NOT NULL
`IS_DELETED` TINYINT(1) NULL DEFAULT '0',
PRIMARY KEY (`JOB_IDENTIFIER`),
INDEX `FK_cb_jobs_cb_credential_type` (`CB_CREDENTIAL_TYPE_IDENTIFIER`)
CONSTRAINT `FK_cb_jobs_cb_credential_type` FOREIGN KEY (`CB_CREDENTIAL_TYPE_IDENTIFIER`) REFERENCES `cb_credential_type` (`CB_CREDENTIAL_TYPE_IDENTIFIER`) ON UPDATE CASCADE ON DELETECASCADE)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB;
CREATE TABLE `cb_credential_type` (
`CB_CREDENTIAL_TYPE_IDENTIFIER` INT(11) NOT NULL AUTO_INCREMENT,
`CB_CREDENTIAL_CODE` VARCHAR(50) NULL DEFAULT NULL,
`CB_CREDENTIAL_TYPE_NAME` VARCHAR(1000) NULL DEFAULT NULL,
`CREATED_DATE` DATETIME NULL DEFAULT NULL,
`CREATED_BY_USER` INT(11) NULL DEFAULT NULL,
`UPDATED_DATE` DATETIME NULL DEFAULT NULL,
`UPDATED_BY_USER` INT(11) NULL DEFAULT NULL,
`IS_DELETED` TINYINT(1) NULL DEFAULT '0',
PRIMARY KEY (`CB_CREDENTIAL_TYPE_IDENTIFIER`)
)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=8;
I would like to get the details of 2nd table based on the credential type id.
How to write Hibernate mapping classes with one to one relationship using hibernate annotations.Anybody please help me.......
Check this tutorial this might help you
http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-one-1.html
check the hibernate one-one annotated tutorial
http://www.dzone.com/tutorials/java/hibernate/hibernate-example/hibernate-mapping-one-to-one-using-annotations-1.html

No relations in entity classes created with NetBeans from database

I'm using Netbeans to create entity class from database, I select all table in my database and the classes are created without any information about relations, like #OneToMany, #ManyToOne etc...
This is an example of two tables I have in my DB, is there anything else I need to specify in the tables creation?
CREATE TABLE `Indicator` (
`ID` int(11) NOT NULL,
`Number` int(11) NOT NULL,
`ApplicablePeriodTypeID` int(11) NOT NULL,
`IndicatorSourceID` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `IndicatorSourceID` (`IndicatorSourceID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1$$
CREATE TABLE `IndicatorSource` (
`ID` int(11) NOT NULL,
`CollectionName` varchar(255) NOT NULL,
`URL` varchar(1000) DEFAULT NULL,
`Number` int(11) NOT NULL,
`SourceName` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1$$
You might need to add a foreign key constraint, e.g. on IndicatorSourceID. Otherwise there's no hint that IndicatorSourceID refers to IndicatorSource.ID.
Changing the ENGINE to InnoDB and adding the foreign keys solved the problem. some db engine different from InnoDB dont support foreign key and when trying to add one, it doesn't generate any error.
As a side note: It seems the create entity classes from database feature in Netbeans 6.9 doesn't add relationships for foreign keys migrated from alternate keys -- only those migrated from primary keys.
Which database are you using?? Are you sure you have configure the database connection using
compatible driver to your database and jdk version...

Categories