autogenerated primary keys in postgres with jooq - java

I am using jooq generated dao's to do a create operation on a table.
The table has a primary key 'id' with type bigserial and the default constraints of not null
CREATE TABLE public.book (
id bigserial NOT NULL,
author varchar(64)
CONSTRAINT book_primary PRIMARY KEY (id)
)
to create a record with jooq i say
Book b = new Book();
b.setAuthor("Eric");
BookDao bd = new BookDao(jooqConfiguration);
bd.insert(b);
This throws a constrain violation exception that id is null.If i set an id such as
Book b = new Book();
b.setId(25);
b.setAuthor("Eric");
BookDao bd = new BookDao(jooqConfiguration);
bd.insert(b);
I do not get the exception but postgres does not generate a value automatically.
The postgres documentation at http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL says the "Omit the SERIAL column in INSERT, or specify DEFAULT keyword"
How do i configure the jooq generated dao to either omit this column or use a value of DEFAULT
Edit - I am using jooq version 3.3.2 and postgres 8.4. ( The final target is aws redshift. So prototyping on postgres 8.4).

This issue of generated dao's not properly handling handling default values is already present in jooq's list of github issues as https://github.com/jOOQ/jOOQ/issues/2700.
It is fixed in 3.4.0. Migrating to 3.4.0 fixed the issue for me.
answering the question so that other can find it if required.

Related

How can I create a schema with JOOQ?

I am trying to create a Vertica table with JOOQ 3.5.x:
Connection connection = create();
DSLContext dslContext = DSL.using(connection);
Field<String> myColumn = DSL.field("my_column", SQLDataType.VARCHAR);
Table table = DSL.tableByName("my_schema", "my_table");
dslContext.createTable(table)
.column(myColumn, myColumn.getDataType())
.execute();
This fails on Schema "my_schema" does not exist.
I can solve it with:
dslContext.execute("create schema if not exists my_schema");
But is there a more elegant way to create a schema with JOOQ?
Currently JOOQ covers just a subset of the possible DDL statements that can be executed against a server and schema management is not yet included so you have to drop back to plan old SQL.
If you need to do a lot of DDL work you should start to look at the latest version 3.8 as this has extend the capabilities to include
DEFAULT column values in CREATE TABLE or ALTER TABLE statements
IF EXISTS in DROP statements
IF NOT EXISTS in CREATE statements
ALTER TABLE .. { RENAME | RENAME COLUMN | RENAME CONSTRAINT } statements
Version 3.6 added
ALTER TABLE ADD CONSTRAINT (with UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK)
ALTER TABLE DROP CONSTRAINT
CREATE TEMPORARY TABLE

Create table with primary key using jOOQ

jOOQ has CREATE TABLE syntax as stated in the documentation:
create.createTable(AUTHOR)
.column(AUTHOR.ID, SQLDataType.INTEGER)
.column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
.column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
.execute();
I'm wondering how to define which column belongs to the primary key? So is there a way in jOOQ to create a CREATE TABLE statement with PRIMARY KEY information?
I'm specifically interested in a solution for SQLite, which doesn't have syntax to add the primary key afterwards, so I think in worst case I have to go to a DB specific solution?
This feature has been implemented for jOOQ 3.8: #4050.
create.createTable(AUTHOR)
.column(AUTHOR.ID, SQLDataType.INTEGER)
.column(AUTHOR.FIRST_NAME, SQLDataType.VARCHAR.length(50))
.column(AUTHOR_LAST_NAME, SQLDataType.VARCHAR.length(50))
.constraints(
constraint("PK_AUTHOR").primaryKey(AUTHOR.ID)
)
.execute();
Since jOOQ 3.6 (#3338), you can also use the ALTER TABLE statement to add a constraint after creating the table:
create.alterTable(AUTHOR)
.add(constraint("PK_AUTHOR").primaryKey(AUTHOR.ID))
.execute();

Is there a Spring Batch 3 Upgrade Script for MySQL?

I haven't seen a script to do the DDL modification necessary to go from Spring Batch 2 -> 3 in MySQL. Curious if one exists?
After running a quick comparison of the schemas, these appear to be the changes for upgrading from Spring Batch 2.2.7.RELEASE -> 3.0.1.RELEASE on MySQL.
ALTER TABLE `BATCH_JOB_EXECUTION` MODIFY COLUMN `EXIT_CODE` varchar(2500) DEFAULT NULL;
ALTER TABLE `BATCH_JOB_EXECUTION` ADD COLUMN `JOB_CONFIGURATION_LOCATION` varchar(2500) DEFAULT NULL;
ALTER TABLE `BATCH_JOB_EXECUTION_SEQ` ADD COLUMN `UNIQUE_KEY` char(1) NOT NULL;
ALTER TABLE `BATCH_JOB_EXECUTION_SEQ` ADD UNIQUE KEY `UNIQUE_KEY_UN` (`UNIQUE_KEY`);
ALTER TABLE `BATCH_JOB_SEQ` ADD COLUMN `UNIQUE_KEY` char(1) NOT NULL;
ALTER TABLE `BATCH_JOB_SEQ` ADD UNIQUE KEY `UNIQUE_KEY_UN` (`UNIQUE_KEY`);
ALTER TABLE `BATCH_STEP_EXECUTION` MODIFY COLUMN `EXIT_CODE` varchar(2500) DEFAULT NULL;
ALTER TABLE `BATCH_STEP_EXECUTION_SEQ` ADD COLUMN `UNIQUE_KEY` char(1) NOT NULL;
ALTER TABLE `BATCH_STEP_EXECUTION_SEQ` ADD UNIQUE KEY `UNIQUE_KEY_UN` (`UNIQUE_KEY`);
For anyone who wants to know the DDL changes for postgresql:
ALTER TABLE BATCH_JOB_EXECUTION ALTER COLUMN EXIT_CODE TYPE varchar(2500);
ALTER TABLE BATCH_JOB_EXECUTION ADD COLUMN JOB_CONFIGURATION_LOCATION varchar(2500) DEFAULT NULL;
ALTER TABLE BATCH_STEP_EXECUTION ALTER COLUMN EXIT_CODE TYPE varchar(2500);
This worked for me when I upgraded from 2.2.7.RELEASE -> 3.0.7.RELEASE.
Really surprised there's no migration guide/or scripts, at least that I could find.

How can i refresh my primary key column?

Till recent time i was using hibernate #Entity annotation to map to database tables.All the primary keys are annotated with #GeneratedValue(strategy = GenerationType.IDENTITY)
I got a scenario where i need to create new schema + migrate data from old schema into new schema.(with few column changes like drop, length and type)
After successful migration of data to new schema tables when i try to insert data using Application its throwing an exception
[ERROR] util.JDBCExceptionReporter DB2 SQL Error: SQLCODE=-803, SQLSTATE=23505, SQLERRMC=1; _NewSchema_._TableName_ , DRIVER=3.51.90
I believe that application is trying to insert rows again with Primary key value starting from 1 because same application is working fine with empty tables.
I want data rows to be inserted with its primary key value as highest value of existing rows primary key .
Any help will be thank full :)
Yes you can do that by altering the table. Alter the table and set starting index for identity column in DB2.
Suppose maximum rows for TBALE_A is 50 and name of identity column is TABLE_ID
ALTER TABLE TBALE_A ALTER COLUMN TABLE_ID
RESTART WITH 51
Your guess is correct, here is my solution, execute the following SQL to give the ID column a specified start position, then your application will work fine.
alter table TABLE_NAME alter column ID set GENERATED BY DEFAULT RESTART WITH 10000;
Hope to help you :)
In case of generation type , IDENTITY, you should look for identity column to be auto incemental.
#GeneratedValue(strategy = GenerationType.IDENTITY) required primary key column to be auto incremental.

Entity class with DB showing Table with No primary key

I want to create an Entity class with database in Netbeans.
When I select a Data source jdbc/Ionbank (custom Jdbc connection Using JDBC-ODBC bridge with Ms SQL 2005 as database).
I see all the tables from that database.
All tables show no primary key, but they have primary keys in them.
Things I have tried :-
Created new 4-5 data source.
Created tables using query, and not the New table option.
Tried changing Odbc connection.
Tried using different drivers for the Jdbc-Odbc bridge like Sql4jdbc.jar, Jdts.jar.
I had same issue, but i solved it using the following: "New Entity Classes from Database" cannot process some tables, saying "no primary key"
A quote from that link helped me:
The problem will happen if you have foreign keys where upper case and lower case table names don't match the referenced table's definition.
For instance:
create table OkTable (
id int not null auto_increment
, primary key (id)
);
create table MisunderstoodTable(
id int not null auto_increment
oktable int not null
, primary key (id)
, foreign key ok (oktable) references oktable (id)
);
The MisunderstoodTable has a foreign key where the target table name doesn't match the lower/uppercase name of the referenced table.
To avoid this problem, just make sure you type your foreign key definitions while matching upper/lower casing for the targeted table.

Categories

Resources