How can i refresh my primary key column? - java

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.

Related

Insert data into oracle DB

I have a table in oracle with primary key and auto increment attribute. But sometimes I have preset ID value for the record.
So when I try to insert record I get exception your trying to insert into table with auto increment field.
So in SQLDeveloper I tried by disabling triggering and inserting values and then enabling trigger which worked perfectly
ALTER TABLE TABLE_NAME DISABLE ALL TRIGGERS;
INSERT INTO TABLE_NAME SELECT * FROM ARCHIVE_TABLE_NAME WHERE TABLE_NAME_COLUMN >= '27-JUN-16 10.35.12.945000000';
ALTER TABLE TABLE_NAME ENABLE ALL TRIGGERS;
But I would like to do this grammatically through hibernate.
So I have following questions
1) If is there any other way of inserting records in table with auto incremtn field?
2) If no then how to execute above 3 statements in hibernate
you can insert records with auto increment key by using the following jpa annotation on your auto incremented field
#GeneratedValue(generator="InvSeq")
#SequenceGenerator(name="InvSeq",sequenceName="INV_SEQ", allocationSize=5)
private long autoIncId;
see this link http://www.oracle.com/technetwork/middleware/ias/id-generation-083058.html

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.

INSERT with DEFAULT id doesn't work in PostgreSQL

I tried running this statement in Postgres:
insert into field (id, name) values (DEFAULT, 'Me')
and I got this error:
ERROR: null value in column "id" violates not-null constraint
I ended up having to manually set the id. The problem with that is when my app inserts a record I get a duplicate key error. I am building a java app using Play framework and ebean ORM. So the entire schema is generated automatically by ebean. In this case, what is the best practice for inserting a record manually into my db?
Edit:
Here is how I'm creating my Field class
#Entity
public class Field {
#id
public Long id;
public String name;
}
Edit:
I checked the field_seq sequence and it looks like this:
CREATE SEQUENCE public.field_seq INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START 1 CACHE 1;
Edit:
Here is the generated SQL in pgAdmin III:
CREATE TABLE field
(
id bigint NOT NULL,
created timestamp without time zone,
modified timestamp without time zone,
name character varying(255),
enabled boolean,
auto_set boolean,
section character varying(17),
input_type character varying(8),
user_id bigint,
CONSTRAINT pk_field PRIMARY KEY (id),
CONSTRAINT fk_field_user_3 FOREIGN KEY (user_id)
REFERENCES account (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT ck_field_input_type CHECK (input_type::text = ANY (ARRAY['TEXT'::character varying, 'TEXTAREA'::character varying]::text[])),
CONSTRAINT ck_field_section CHECK (section::text = ANY (ARRAY['MAIN_CONTACT_INFO'::character varying, 'PARTICIPANT_INFO'::character varying]::text[]))
);
CREATE INDEX ix_field_user_3
ON field
USING btree
(user_id);
There is no column default defined for field.id. Since the sequence public.field_seq seems to exist already (but is not attached to field.id) you can fix it with:
ALTER SEQUENCE field_seq OWNED BY field.id;
ALTER TABLE field
ALTER COLUMN id SET DEFAULT (nextval('field_seq'::regclass));
Make sure the sequence isn't in use for something else, though.
It would be much simpler to create your table like this to begin with:
CREATE TABLE field
(
id bigserial PRIMARY KEY,
...
);
Details on serial or bigserial in the manual.
Not sure how the the Play framework implements this.
This works.
insert into field (id, name) values (nextval('field_seq'), "Me");

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.

JPA ids auto_increment generation

Am using JPA into my application and then the when insert different objects the database use sequential ids.
For Example:
If i have table member and table user , when inserting object of type user will take id = 1 then add object into table member will take id = 2 .
The problem is : i imported a .sql file into the database.
Then when i insert a new record from my application it cause an exception because it use ids already added.
How can i solve this problem ?
You can set the start value for AUTO_INCREMENT as
ALTER TABLE tbl AUTO_INCREMENT = 100;
See also:
3.6.9. Using AUTO_INCREMENT

Categories

Resources