why did not generate enableSelectByPrimaryKey when using mybatis generator - java

When I am using this command to generate mybatis xml file:
java -jar mybatis-generator-core-1.3.4.jar -configfile generatorConfig.xml -overwrite
everything works fine but finnaly i found the user mapper result file did not genreate SelectByPrimaryKey function. this is part of my generate file config:
<table tableName="users"
enableCountByExample="true"
enableUpdateByExample="true"
enableDeleteByExample="true"
enableSelectByExample="true"
enableSelectByPrimaryKey="true"
enableUpdateByPrimaryKey="true"
selectByPrimaryKeyQueryId="true"
selectByExampleQueryId="true">
<generatedKey column="ID" sqlStatement="JDBC" identity="true" />
</table>
my database is PostgreSQL 13. what should I do to fix it? This is my user table DML:
CREATE TABLE public.users (
id int8 NOT NULL GENERATED ALWAYS AS IDENTITY,
nickname varchar NULL,
avatar_url varchar NULL,
phone varchar NOT NULL,
updated_time int8 NOT NULL,
created_time int8 NOT NULL,
salt varchar NULL,
pwd varchar NULL,
sex int4 NULL,
level_type varchar NULL,
phone_region varchar NULL,
country_code int4 NULL,
user_status int4 NULL DEFAULT 1,
last_login_time int8 NULL,
first_login_time int8 NULL,
app_id int4 NOT NULL,
register_time int8 NULL,
apple_iap_product_id varchar NULL,
CONSTRAINT unique_phone UNIQUE (phone)
);

The DML you posted shows that the table doesn't have a primary key. You have two options.
You could define a primary key in the table by adding this to the create table statement:
create table public.users (
...
primary key (id)
);
Or, if you don't want to define a primary key in the database, you can use the "VirtualPrimaryKeyPlugin" in MyBatis Generator. See here for details: https://mybatis.org/generator/reference/plugins.html

Related

[HY000][1215] Cannot add foreign key constraint

application_user
-- auto-generated definition
create table application_user
(
id bigint auto_increment
primary key,
email varchar(255) not null,
is_active bit null,
name varchar(255) not null,
password varchar(255) not null,
surname varchar(255) not null,
username varchar(255) not null
)
engine = MyISAM;
I have a table that generated by hibernate.
I want to create a table and add a foreign key manually.
So far I tried this
application_user_log
CREATE TABLE application_user_log (
log_id BIGINT NOT NULL AUTO_INCREMENT,
fk_user_id BIGINT NOT NULL,
old_user_name BIGINT NOT NULL,
new_user_name BIGINT NOT NULL,
PRIMARY KEY (log_id),
FOREIGN KEY (fk_user_id) REFERENCES application_user(id)
) ;
And I got this error message.: [HY000][1215] Cannot add foreign key constraint
Why I got this error?
Well I don't know why my answer got converted into a comment, but I knonw that MyISAM doesn't support foreign keys. You can read details here.

How to execute a long MySQL script in Java without adding external libs?

I'm using JDBC MySqlDataSource class to handle a database for my app. I'm trying to make a method that formats the database for app's use - creates tables and constraints. Basically it's a script generated from phpMyAdmin export option. I've made a class called DatabaseTemplate with a static String containing the script and then I use it in my formatDatabase method.
public void formatDatabase() {
try {
sql.executeUpdate(DatabaseTemplate.GetScript());
} catch (SQLException e) {
System.out.println("Connection not found.");
e.printStackTrace();
}
}
It makes an SQL exception "Syntax not correct". My script string looks like this:
CREATE TABLE `arenas` (
`arena_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`location` varchar(50) COLLATE utf16_polish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `contestants` (
`contestant_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`surname` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`nickname` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`score` int(11) NOT NULL,
`language` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`contact_info` text COLLATE utf16_polish_ci NOT NULL,
`additional_info` text COLLATE utf16_polish_ci,
`team_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `matches` (
`match_id` int(11) NOT NULL,
`sideA` int(11) NOT NULL,
`sideB` int(11) NOT NULL,
`sideA_score` int(11) DEFAULT NULL,
`sideB_score` int(11) DEFAULT NULL,
`time` datetime NOT NULL,
`tournament` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `system_users` (
`sys_usr_id` int(11) NOT NULL,
`login` varchar(20) COLLATE utf16_polish_ci NOT NULL,
`pw_hash` int(32) NOT NULL,
`permissions` varchar(5) COLLATE utf16_polish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `teams` (
`team_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`where_from` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`leader_id` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
CREATE TABLE `tournaments` (
`tournament_id` int(11) NOT NULL,
`name` varchar(50) COLLATE utf16_polish_ci NOT NULL,
`type` enum('solo','team') COLLATE utf16_polish_ci NOT NULL,
`arena_id` int(11) NOT NULL,
`operator` int(11) NOT NULL,
`additional_info` text COLLATE utf16_polish_ci
) ENGINE=InnoDB DEFAULT CHARSET=utf16 COLLATE=utf16_polish_ci;
ALTER TABLE `arenas`
ADD PRIMARY KEY (`arena_id`);
ALTER TABLE `contestants`
ADD PRIMARY KEY (`contestant_id`),
ADD KEY `team_id` (`team_id`);
ALTER TABLE `matches`
ADD PRIMARY KEY (`match_id`),
ADD KEY `sideA` (`sideA`),
ADD KEY `sideB` (`sideB`),
ADD KEY `tournament` (`tournament`);
ALTER TABLE `system_users`
ADD PRIMARY KEY (`sys_usr_id`);
ALTER TABLE `teams`
ADD PRIMARY KEY (`team_id`),
ADD KEY `leader_id` (`leader_id`);
ALTER TABLE `tournaments`
ADD PRIMARY KEY (`tournament_id`),
ADD KEY `arena_id` (`arena_id`),
ADD KEY `operator` (`operator`);
ALTER TABLE `contestants`
MODIFY `contestant_id` int(11) NOT NULL AUTO_INCREMENT;
ALTER TABLE `contestants`
ADD CONSTRAINT `contestants_ibfk_1` FOREIGN KEY (`team_id`) REFERENCES `teams` (`team_id`);
ALTER TABLE `matches`
ADD CONSTRAINT `matches_ibfk_1` FOREIGN KEY (`sideA`) REFERENCES `teams` (`team_id`),
ADD CONSTRAINT `matches_ibfk_2` FOREIGN KEY (`sideB`) REFERENCES `teams` (`team_id`),
ADD CONSTRAINT `matches_ibfk_3` FOREIGN KEY (`tournament`) REFERENCES `tournaments` (`tournament_id`);
ALTER TABLE `teams`
ADD CONSTRAINT `teams_ibfk_1` FOREIGN KEY (`leader_id`) REFERENCES `contestants` (`contestant_id`);
ALTER TABLE `tournaments`
ADD CONSTRAINT `tournaments_ibfk_1` FOREIGN KEY (`arena_id`) REFERENCES `arenas` (`arena_id`),
ADD CONSTRAINT `tournaments_ibfk_2` FOREIGN KEY (`operator`) REFERENCES `system_users` (`sys_usr_id`);
I'm guessing it's because of how String in java uses \n. I tried removing it, changing it to \r\n, it's still the same exception. How do I format this script so that it executes properly?
It might be good to read the Java documentation on issuing SQL queries as it seems you are using the wrong method in your code.
If you are curious about the difference between DDL (Data Definition Language) and DML (Data Manipulation Language), there are valuable articles on them on Wikipedia.
You might find it easier to place the sql in a dedicated file setup.sql and then load the file when needed and execute the sql from the file. If your code contains more than one SQL statement, you could store each in a separate file.
That way your SQL query will:
actually be readable to a human and
there will be no issues with multi-line strings in Java.

Trouble inserting foreign key with H2

I'm using H2 to test my database code. To do that, I start by reading in a set of SQL files and using RunScript.execute to execute the SQL.
My real database is MySQL, and I have tested the SQL scripts to make sure they work and they do. The problem is decidedly inside of H2. Here's the set of scripts:
CREATE TABLE stores (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);
CREATE TABLE regions (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
);
Running these 3 scripts to create the tables using MySQL Workbench works fine. Running them from JUnit via the follow code does not:
Reader storeTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V1__create_stores_table.sql"));
Reader regionTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V2__create_regions_table.sql"));
Reader productTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V3__create_products_table.sql"));
this.dc = DatabaseConnector.getConnection();
RunScript.execute(dc, storeTableSql);
RunScript.execute(dc, regionTableSql);
RunScript.execute(dc, productTableSql);
I get the following error:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE PRODUCTS (
ID INTEGER AUTO_INCREMENT PRIMARY KEY,
STORE_ID INTEGER NOT NULL,
NAME VARCHAR(255) NOT NULL,
REGION_ID INTEGER NOT NULL,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
DELETED_AT TIMESTAMP,
UNIQUE(STORE_ID, NAME),
FOREIGN KEY STORE_FK[*](STORE_ID) REFERENCES STORES(ID),
FOREIGN KEY REGION_FK(REGION_ID) REFERENCES REGIONS(ID)
) "; expected "("; SQL statement:
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
) [42001-196]
This code works in MySQL, and I am running H2 in MySQL compatibility mode via the following connection string: jdbc:h2:file:~/database;MODE=MySQL. I'm not sure what else I can do here. This error seems very strange to me, as I have no idea why it would think I need ANOTHER (.
Does anyone know how to fix this? Or have a better suggestion on how to seed my test database? I cannot use hibernate for this task due to a restriction on the system I am running on so I'm forced to do a lot manually.
Thank you!
From this post, but with a little more explanation.
Foreign key constraints in H2 do not allowed named constraints for whatever reason. The solution to this problem is to simply remove the name:
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY (store_id) REFERENCES stores(id),
FOREIGN KEY (region_id) REFERENCES regions(id)
);
The error message is so unclear and misleading. Hopefully this helps someone.

Hibernate not setting reference column

I'm using hibernate 4.3.7.Final on OSX with Mysql 5.5.8.
I have a joinTable relationship setup however my foreign keys fail to add:
[ERROR] org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 foreign key (email) references T_USER
[ERROR] org.hibernate.tool.hbm2ddl.SchemaExport - Can't create table 'carcloud.#sql-149c1_195' (errno: 150)
if I run SHOW ENGINE INNODB STATUS; I get the following:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
141106 5:48:00 Error in foreign key constraint of table carcloud/#sql-149c1_123:
foreign key (email) references T_USER:
Syntax error close to:
if i run:
alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 foreign key (email) references T_USER (email);
The constraint is successfully added, note (email) at the end which is the referencing column.
It is set as my primary key:
CREATE TABLE `T_USER` (
`email` varchar(100) NOT NULL,
`created_by` varchar(50) NOT NULL,
`created_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`last_modified_by` varchar(50) DEFAULT NULL,
`last_modified_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`OPT_LOCK` int(11) DEFAULT NULL,
`first_name` varchar(50) DEFAULT NULL,
`last_name` varchar(50) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`phone` varchar(100) NOT NULL,
PRIMARY KEY (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Any ideas?
The only difference that i see is in Hibernate "references T_USER" and in sql "references T_USER (email)" so you should insert java code that call "alter table T_USER_AUTHORITY add constraint FK_fr51fcyulxn31ijiotp4fx7i5 etc .. "

jdbc derby Syntax Error when create table

Please have a look:
CREATE TABLE Uzytkownik(
user_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
login VARCHAR(25) NOT NULL,
password VARCHAR(16) NOT NULL,
CONSTRAINT user_pk PRIMARY KEY (user_id)
);
CREATE TABLE Wizytowka(
wizytowka_id INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
tytul VARCHAR(60) NOT NULL,
tresc VARCHAR(400) NOT NULL,
premium_w INTEGER(1) NOT NULL,
id_user INTEGER NOT NULL,
CONSTRAINT wizytowka_id PRIMARY KEY (wizytowka_id),
FOREIGN KEY (id_user) REFERENCES Uzytkownik(user_id)
);
I got Syntax Error: Encountered "(" at line 5, column 26.
Line 8, column 1
The problem is in your second create statement. Derby does not support a length attribute for the integer type. Therefore
premium_w INTEGER(1) NOT NULL,
results in an error. Modify it to
premium_w INTEGER NOT NULL,
and it will work.

Categories

Resources