Data integrity constraint Error

Source: Internet
Author: User

The issue of foreign key correlation was encountered in recent projects. Although the previous project dealt with similar errors, but did not know why the error, this is probably to figure out what the situation is.

Scenario: Class A primary key aid is a Class B foreign key bid, which sets a foreign key constraint in the database, deletes the constraint, and updates the cascade. Delete The Times wrong org.hibernate.exception.ConstraintViolationException in the program ... This error is reported when it violates the database constraints, and is now only a case of reporting this error.

After the online query solution found that the set of constraints either to hibernate management, or to the database management, or will be error. When given to hibernate management, the database does not need to set the foreign key, the Cascade attribute in the set mapping file is one of the following:

 All: associated operations in all cases, i.e. save-update and delete.  
None: No association operation is performed in all cases. This is the default value.
save-update: The associated operation is performed when Save/update/saveorupdate is executed.
delete: The associated operation is performed when the delete is executed.
All-delete-orphan: When a node becomes an orphan node in the object graph, delete the node
But now I'm going to delete and update the separate processing, I will not use hibernate settings, had to give the database management, this time the program does not process will be error.
First, use native SQL statements to write the Delete method, otherwise hibernate will certainly print the wrong, and in the deletion method catch to constraintviolationexception, you can do what you want to deal with. such as:
Try  {    +"...");     Catch (Throwable e) {    = e.getcause ();        if instanceof constraintviolationexception) {               thrownew sysexception ("111");}        }    

Also attach the native deletion method:

 Public void throws sysexception {        customercontextholder.setcustomertype (customercontextholder.data_source_mysql;     = clazz.getsimplename ();     = Constant.Table_Entity_MAP.get (ClassName). Split ("_");     = "DELETE from" + Constant.Table_Entity_MAP.get (className) + "WHERE" +classid[1]+ "Id =?" ;     = getsession (). createsqlquery (SQL);    Query.setparameter (0, id);    Query.executeupdate ();}

Data integrity constraint Error

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.