Hibernate not creating foreign key constraint on MySQL - java

I am using Hibernate and MySQL when my mapping for table is done and when I look at my DB table its created successfully but their is no foreign key constraint but column is created.
When I try to insert record in child table and when I put id which not exist in parent table in foreign key column then also that row get inserted.
My table engine is innoDB.
If I change dialect to MS-SQL then table get created with foreign key constraint.

Sorry users,
I got issue with this actually my DBA not given permission for ALTER command.
so thats why, when an table with foreign Key is going to create first CREATE command is executed and then ALTER command is executed.
In my case CREATE command is executed successfully but ALTER command does not coz its dnt have permission.

Related

How to Get Tablename, constraints, foreign key and references from derby database with java?

So I have an open source piece of software written with java and the database is using Derby and it is written using hibernate.
The problem is when you go to build the latest build, there is no upgrade or migrate function to use your existing data from your current derby database into the new one.
So I am writing a java tool that will move the data over from one database to the other.
Here is the issue: I have no problem exporting the data, the problem comes when it comes to importing the data back in because of all the foreign keys.
With help of stack overflow I did find this bit of code which gets me part of the data I need but not all that I can use. I need to be able to create a list of Tablenames, constraints, foreign Keys and references so that way I can add my code to alter table drop foreign keys and then import my data and then add the constraints and foreign keys and references back to the database.
SELECT
fc.constraintname,
ft.tablename,
fs.schemaname,
fg.descriptor,
pc.constraintname,
ps.schemaname
FROM
sys.sysconstraints fc
JOIN sys.sysforeignkeys f ON f.constraintid =fc.constraintid
JOIN sys.sysconglomerates fg ON fg.conglomerateid = f.conglomerateid
JOIN sys.systables ft ON ft.tableid = fg.tableid JOIN sys.sysschemas fs ON ft.schemaid = fs.schemaid
JOIN sys.sysconstraints pc ON pc.constraintid = f.keyconstraintid
JOIN sys.sysschemas ps ON pc.schemaid = ps.schemaid WHERE fc.type = 'F';
The only thing that I can use out of this that I can parse is the Constraint and Tablename.
I still need to get the foreign key and the reference. I do not know enough about how a derby database is laid out and I do not have time to research it and do all the cross references to find it.
So if anyone out there can help me out on what I need to do to modify the sql statement above to obtain the following in one table preferably:
Tablename - Constraint - Foreign Key - References
Example Final Result:
Cost is the tablename
constraint is 1232133
foreign key is Currency_ID
references is Currency
So I can rebuild all of the following lines similiar below:
alter table cost add constraint 1232133 foreign key (Currency_ID) references Currency
Thank You,
Shawn Mulligan

WSO2-APIM 2.1.0 to 2.2.0 upgrading issue: For the table IDN_OIDC_REQ_OBJECT_REFERENCE

I'm using the link for upgrading WSO2-APIM from 2.1.0 to 2.2.0 version.
While performing step 6-g I came across java.sql.SQLSyntaxErrorException for creating a table IDN_OIDC_REQ_OBJECT_REFERENC.
My all other scripts ran successfully but at the time of executing sql scripts from migration-resources\5.5.0\dbscripts\step1\identity\oracle.sql the issue occurred. This file exist inside zip file given in step 6-a.
When I tried running following sql script manually
CREATE TABLE IDN_OIDC_REQ_OBJECT_REFERENCE (
ID INTEGER,
CONSUMER_KEY_ID INTEGER ,
CODE_ID VARCHAR(255) ,
TOKEN_ID VARCHAR(255) ,
SESSION_DATA_KEY VARCHAR(255),
PRIMARY KEY (ID),
FOREIGN KEY (CONSUMER_KEY_ID) REFERENCES IDN_OAUTH_CONSUMER_APPS(ID) ON DELETE CASCADE,
FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE,
FOREIGN KEY (CODE_ID) REFERENCES IDN_OAUTH2_AUTHORIZATION_CODE(CODE_ID) ON DELETE CASCADE);
into oracle db I got following error:
Error at Command Line : 66 Column : 61
Error report -
SQL Error: ORA-02270: no matching unique or primary key for this column-list
02270. 00000 - "no matching unique or primary key for this column-list"
*Cause: A REFERENCES clause in a CREATE/ALTER TABLE statement
gives a column-list for which there is no matching unique or primary
key constraint in the referenced table.
*Action: Find the correct column names using the ALL_CONS_COLUMNS
catalog view
Line 66 is nothing but: FOREIGN KEY (TOKEN_ID) REFERENCES IDN_OAUTH2_ACCESS_TOKEN(TOKEN_ID) ON DELETE CASCADE,
Let me know what thing I've missed while doing the migration ? Or is it because of existing table are not proper ? Any help will be grateful. Thanks in advance.

ALTER TABLE CREATE CONSTRAINT IF NOT EXIST possible?

I have an application which starts (AppStarter) a web server with a web application. The Web Application has migration scripts (flyway).
I want to write some data from AppStarter through JDBC in a table. But I want to create the table if it does not exist. The table also has some constraints.
Within the AppStarter I execute following command:
CREATE CACHED TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER (
ID VARCHAR(32) PRIMARY KEY NOT NULL,
VERSION INTEGER,
USER_ID VARCHAR(32)NOT NULL,
ROLE_ID VARCHAR(32) NOT NULL,
PARAMETER VARCHAR(255) NOT NULL
);
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER ADD CONSTRAINT PUBLIC.CURTBP_USER_ID FOREIGN KEY(USER_ID) REFERENCES PUBLIC.CORE_USER(ID) NOCHECK;
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER ADD CONSTRAINT PUBLIC.CURTBP_ROLE_ID FOREIGN KEY(ROLE_ID) REFERENCES PUBLIC.CORE_USER_ROLE(ID) NOCHECK;
The web app also reads some information from this table and creates the tables.
Now I have a sql migration script
CREATE CACHED TABLE IF NOT EXISTS PUBLIC.CORE_USERROLE_TO_PARAMETER (
ID VARCHAR(32) PRIMARY KEY NOT NULL,
VERSION INTEGER,
USER_ID VARCHAR(32)NOT NULL,
ROLE_ID VARCHAR(32) NOT NULL,
PARAMETER VARCHAR(255) NOT NULL
);
But how do I create the constraint only if they does not already exist?
Thanks in advance
Currently I can get if the constraints exists with
select * from INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_NAME='CURTRP_USER_ID'
but how do I build this into a if query with H2
Edit:
I could move the constraint part in total to the migration script, but this seems somehow wrong.
I am working with H2 Database.
Following my comment, this should be possible:
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER
ADD CONSTRAINT IF NOT EXISTS PUBLIC.CURTBP_USER_ID
FOREIGN KEY(USER_ID) REFERENCES PUBLIC.CORE_USER(ID) NOCHECK;
ALTER TABLE PUBLIC.CORE_USERROLE_TO_PARAMETER
ADD CONSTRAINT IF NOT EXISTS PUBLIC.CURTBP_ROLE_ID
FOREIGN KEY(ROLE_ID) REFERENCES PUBLIC.CORE_USER_ROLE(ID) NOCHECK;
Use this query to get the foreign key constraints
SELECT * FROM INFORMATION_SCHEMA.CONSTRAINTS WHERE CONSTRAINT_TYPE = 'REFERENTIAL'
You can try ALTER TABLE IF EXISTS like CREATE IF EXISTS. If its a responsibility of your application only, and not handled by another app or script.

How to find the primary key of deleted records with JDBC.?

I want to know how to find the primary key of the deleted records through the JDBC connection.
If my query like following then what will be the primary key of deleted records?
String sqlDelete = "DELETE FROM devicesequences WHERE deviceId = 20;
What is the primary key of the deleted record?
You need a second query. And of course you need to execute it before running the DELETE query.
SELECT id FROM devicesequences WHERE deviceId = 20;
assuming that id is the name of your primary key column.
Since you can delete any arbitrary row (or set of rows) at any time: no, there's no way to tell WHICH row (or rows) you most recently deleted.
You can, however, create a "trigger" to save this information for you.
http://dev.mysql.com/doc/refman/5.0/en/triggers.html

Can't create foreign key

I'm trying to add a foreign key to an existing table, and was having issues. I figured that I had an error in my syntax, so I updated my hibernate.cfg.xml file to auto-update.
As it turns out, hibernate had the same error. Here's my SQL to add the foreign key:
alter table pbi add index FKEA3F7BDE9BAB051 (FK_idP), add constraint FKEA3F7BDE9BAB051 foreign key (FK_idP) references p (idP)
and the error is:
Cannot add or update a child row: a foreign key constraint fails (`db`.`#sql-6f8_3`, CONSTRAINT `FKEA3F7BDE9BAB051` FOREIGN KEY (`fk_idP`) REFERENCES `p` (`idP`))
Can anyone think of a reason why this would fail?
This error means that constraint can not be applied because there are existing records that would violate it.
In your case, pbi table has rows whose FK_idP column has a value for which there are no matching records with that value in idP column of p table.

Categories