Hibernate oracle ORA-02291: violation of the complete constraints of the problem: do a small experiment, oracle database, using hibernate as the middleware. An error that always throws a foreign key constraint when saving many-to-many relationships, roughly as follows: (0 MS) [main] WARN: org. hibernate. util. JDBCExceptionReporter # logExceptions: SQL Error: 2291, SQLState: 23000 (10 MS) [main] ERROR: org. hibernate. util. JDBCExceptionReporter # logExceptions: ORA-02291: violation of the complete constraints (STOCK. FK_INDUSTRY_R_STO_R_INDUSTRY)-parent keyword (10 MS) [main] WARN: org. hibernate. util. JDBCExceptionReporter # logExceptions: SQL Error: 2291, SQLState: 23000 (10 MS) [main] ERROR: org. hibernate. util. JDBCExceptionReporter # logExceptions: ORA-02291: violation of the complete constraints (STOCK. FK_INDUSTRY_R_STO_R_INDUSTRY)-the parent keyword database is not found: the stock table and the industry table are multi-to-many relations, and the intermediate table is industry_r_stock. Databases designed using powerdesigner. The data script is roughly divided into three parts: www.2cto.com 1. create sequence 2. create a database 3. add the before insert trigger to the database table. In the code, the file corresponding to each database table is configured with the primary key generation mechanism of sequence: <generator class = "sequence"> <param name = "sequence"> SEQUENCE_stock </param> </generator>: this is the general situation. Now let's talk about the cause of the problem, after repeated checks, I am very sure that my database and table do not have any foreign key constraint definition problems, and there is no problem with the ing in the hibernate configuration file, that is, when saving the stock, the data in the intermediate table cannot be saved, and the above exception is repeatedly thrown. The problem is that the sequence of the primary key is defined when pdm is designed using powerdesigner. pd will CREATE 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; the latest value is retrieved FROM the sequence whenever data is inserted. However, when we use hibernate as the middleware to save an object, the first thing is to query the sequence (assumed as 1000) of the primary key of the database table, and then assign it to the currently saved object, when the inser statement is executed, the before insert trigger is triggered, and the primary key value (1001) is retrieved from the sequence again. As a result, the primary key of the currently saved object is 1001, however, in the Code, the ID of object A is still 1000. Therefore, when saving the data in the intermediate table, the foreign key constraint is broken. Indeed, the database does not have a record with a primary key value of 1000. Www.2cto.com remarks 1. If hibernate is used as the middleware, do not create the before insert trigger in the database to obtain the NEW. ID. Specify the sequence used by the primary key of the database table in *. hbm. xml. Note 2. aside from hibernate, if the before insert trigger is created, a connection between the database table and sequence is established, we do not need to consider the ID value when inserting data using SQL commands. Insert into stock (code, name) values ('code', 'name'); note 3. if the before insert trigger is not created, a sequence must be used for the primary key of the database table. The value of the primary key must be specified in the SQL command. Insert into table1 values (sequence_name.curval, 'code', 'name'); Author: dawn_sky