Spring JPA how to skip Foreign Key Constraint violation check - spring

Is there a way to Skip Foreign Constraint check in Spring JPA.
In SQL ALTER TABLE TableName NOCHECK CONSTRAINT ForeignKeyName. How to achieve this programmatically using Spring JPA
I am working on a Sync tool using SpringBoot, Spring data JPA which copies data from one DB to Other and using Spring JPA, the tables in the schema have two many foreign key constraints and adding the mappings in all the entity classes is proving to be cumbersome. Any suggestion around skipping the constraint check and in general for the approach is appreciated.
I understand the consequence of data integrity, by removing the constraint check.

There is no special JPA way to do this, but you can simply use SQL.
Depending on if you want to do the change just for the sync process or always you'd put the statement either in the creation scripts of the database or you execute it using EntityManager.createNativeQueryenter

Related

How to apply both on delete and on update cascade simultaneously in oracle12c?

I'm beginer and I'm working on oracle 12c database so, In my database project I want to apply cascade on delete and on update simultaneously as i did in mysql but when i apply tha same technique in oracle it show me the error so how can i do that?
There is no ON UPDATE CASCADE on Oracle. While you can probably argue updating a table's primary key is valid in SQL, you probably should not, hence the decision of Oracle not to implement it.
More info here:
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:5773459616034
EDIT: As discussed in comments below, think of that constraint as a way Oracle prevents people from doing something wrong (updating primary keys).
The correct way to handle the case of a primary key that might be updated is to create a separate field that will act as the surrogate primary key. The surrogate key, of course, is immutable.
The danger of using a natural key as primary key is discussed there.

#UniqueConstraint requires alter table in MariaDB if table already existed before with no constraints?

I apologize if I repeat the question, but I did not find a similar one.
I have added a unique constraint on an already existent table. We use MariaDB.
I have used the annotation:
#Table(uniqueConstraints={#UniqueConstraint(name="autonomy_name_energyType", columnNames={"autonomy","name","energyType"})})
The unit tests pass, but in the DB I am still allowed to create duplicates.
Do I need an ALTER table too? By checking the table I can see there are no constraints added to it.
Thanks
As explained in these SO posts :
Unique constraint not created in JPA
#Column(unique=true) does not seem to work
An explicit alter table query is needed for ur constaints to take effect on the db level.
As an extra info, it would have worked if the table was being re-created via JPA. see :
Add a unique constraint over muliple reference columns

Re insert records in Oracle table with Auto generated identifiers using Hibernate

I have few tables in my database where the primary keys are auto generated using Hibernate seqhilo generator configuration. We need to archive these records and at a later point, should be able to restore them in case of a business scenario. My question is if I restore these tables with simple insert statements will that suffice or should I worry about the sequence generator? I would like to have the same ID and not a new generated one. To be clear these re-inserts will happen via direct SQL and not via Hibernate.

referential integrity hibernate Spring

I am working with Hibernate 4 and Spring 3.5 , so I want to do a delete operation in a table or entity, but a table has referential integrity with other tables. I want to know how I can know if a table has referential integrity, thus I can delete the record or not.
If there is a constraint and you are not aware and you try to delete a record that has a dependency to the other tables. You might get a ConstraintViolation Exception.
http://docs.oracle.com/javaee/6/api/javax/validation/ConstraintViolationException.html
Anyhow, I suggest you to look at table relationships (in the database) and make sure there is no PK/FK relationship before deleting any record.
Also if you are using JPA entities, you might have relationships in the JPA level (#OneToOne, #OneToMany, ...)

Configuring EclipseLink DDL Generation to ignore tables

I have several externally supplied tables which I can't modify. In my case these are things like the built in Oracle tables.
What I have is several entities which map on to these tables, but when I do my DDL generation I don't want them to be generated. Is there an annotation or an attribute I can set to ignore certain entities in the DDL generation?
You could simply switch to "create" ddl from "drop-create". The "create" calls for the existing tables would be ignored. Unfortunately there is currently no option in EclipseLink to prevent a table from being dropped when using "drop-create". Your best option is to have EclipseLink write the DDL to file and remove the lines for tables you do not want altered. It is likely that something similar will be available in a future version of EclipseLink. You can monitor and provide feedback on the currently active "extensions" feature in EclipseLink : http://wiki.eclipse.org/EclipseLink/Development/2.4.0 . Monitor this page for more information.

Resources