referential integrity hibernate Spring - 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, ...)

Related

Spring JPA how to skip Foreign Key Constraint violation check

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

Unable to delete a record when created foreign-key constraints manually in mysql without using migration.

I have created database tables manually and i have also created foreign key constraints in tables manually without using migration. now when i am going to delete a record it giving me following error-
Integrity constraint violation: Cannot delete or update a parent row:
a foreign key constraint fails
Note: i can't use migration because database was already created.
Use ON DELETE CASCADE and ON UPDATE CASCADE on foreign keys, like:
...(create/alter Children table query)...
CONSTRAINT FK_ParentChild
FOREIGN KEY (parent_id) REFERENCES Parents(id)
ON UPDATE CASCADE ON DELETE CASCADE;
So you don't have to manually delete children before deleting the parent element. All children records will automatically delete along with the deletion of parent record.
I would suggest investigating this further by doing the follow:
On your development environment, write migrations to create the
tables from scratch 2)
Look at the generated schema and compare it
to the schema of your existing production tables
Note the
differences between the two schema and write migrations to correct
the production tables.
Note:
If you need to correct the data in the meantime and you are confident that the data integrity of your database is not at risk. You can use the following statements to drop the foreign key checks whilst you correct the data.
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
I can't stress enough, remember to turn on your foreign key checks once you are finished and do not do this on a production database. Take a copy of the database and try things locally until you are confident that your corrections are 100% safe ;)

#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

Relationships between tables in Sentry shema

Why in Sentry 2 database schema, relationships between tables don't exist? For example between users and users_groups table. Do I have to manually set that?
Sentry will create the needed tables for you so you do not nee to make any other changes.
There are no foreign keys if this is what you asking. Foreign keys are just way to help guarantee the data integrity and are not required.

Inserting in a child table

The design of my database has a table named person and tables employee and student are specializations of the table person the relationship between tables is total and has an overlapping restriction.
The problem is that I want to insert a student or employee and that the parent table (person) is updated automatically but the DBMS says violated a referential integrity constraint
I am using oracle can someone help me?
If I understood you correctly you have one table per type (TPT) and an employee can never be a student and also the other way around.
I assume that your problem is that the constraint is checked immediately instead of using deferred checking. That means the constraints are checked when your transaction is finished - which gives you the possibility to insert an employee/student and let your trigger do its work and after that do the commit.
Information about deferred constraints:
Oracle documentation
More information

Resources