violation of full constraint (*)-no parent keyword found

Source: Internet
Author: User

question:

Do a small experiment of your own, Oracle database, using hibernate as a middleware. An error that always throws a foreign key constraint when a many-to-many relationship is saved, generally as follows:

(0 ms) [Main] Warn:org.hibernate.util.jdbcexceptionreporter#logexceptions:sql error:2291, sqlstate:23000

(MS) [Main] error:org.hibernate.util.jdbcexceptionreporter#logexceptions:ora-02291: Violating the full constraints (stock). fk_industry_r_sto_r_industry)-parent keyword not found

(MS) [Main] Warn:org.hibernate.util.jdbcexceptionreporter#logexceptions:sql error:2291, sqlstate:23000

(MS) [Main] error:org.hibernate.util.jdbcexceptionreporter#logexceptions:ora-02291: Violating the full constraints (stock). fk_industry_r_sto_r_industry)-parent keyword not found

Database:

The stock table and the industry table are many-to-many relationships, and the middle table is industry_r_stock. A database designed using PowerDesigner. The script for the data is roughly divided into three parts:

1. Create sequence

2. Create a database

3. Add a before insert trigger to the library table

The corresponding files for each library table in your code are configured with the sequence primary key generation mechanism:

<generator class= "Sequence" >

<param name= "sequence" >SEQUENCE_stock</param>

</generator>

Reason:

That's the way it is, now talk about the cause of the problem, after repeated examination, very sure that my library table does not exist the problem of the definition of foreign key constraints, the Hibernate configuration file in the corresponding relationship is also no problem, that is, when the stock, can not save the data in the middle table, repeatedly thrown above the exception.

The problem is that when you design PDM using PowerDesigner, the SEQUENCE,PD that defines the primary key creates the before insert trigger:

CREATE TRIGGER Tib_basic_info before INSERT on Basic_info for each ROW

BEGIN

SELECT Sequence_stock. Nextval INTO:NEW.ID from DUAL;

End;

Every time a data is inserted, the most recent value is fetched from the sequence. However, when we use hibernate as a middleware to save an object, the first thing is to query the sequence of the primary key of the library table (assuming 1000) and assign it to the object A that is currently being saved, triggering the before insert trigger when the Inser statement is executed, The primary key value (1001) was retrieved from the sequence, causing the current saved object's primary key to be 1001, but in code the ID of object A is still 1000, so it is said that the foreign key constraint is broken when saving the intermediate table data. It is true that there are no records with a primary key value of 1000 in the database.

Note 1.

If you use Hibernate as a middleware, do not create before insert triggers in the database to get new.id. Specifies the sequence used by the primary key of the library table in *.hbm.xml.

Note 2.

Aside from hibernate, if you create a before insert trigger, you have a connection between the library table and the sequence, and we don't need to consider the value of the ID when we insert the data using the SQL command.

INSERT into the stock (Code,name) VALUES (' Code ', ' name ');

Note 3.

If you do not create a before insert trigger, the primary key for the library table uses a sequence, and the value of the primary key must be specified in the SQL command. INSERT INTO table1 values (sequence_name.curval, ' Code ', ' name ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.