A study on the preservation of hibernate-to-multi-correlation relationship

Source: Internet
Author: User

when Hibernate was previously used, it was often not determined if the object with the associated relationship was saved successfully.
Therefore, it is deliberate to practice the 2 objects of a one-to-many relationship.

I. Preparation of POJO classes and configuration FilesHere's a little bit ahead of time.foreign key ColumnsIn the databaseis not set to null 。 Therefore, a foreign key can appear empty.
(1) Pojo class           (2) mapping file            (3) Preparation of methods in the test class        
Two, both sides maintain the correlation relations and two-way correlation note: Here the two-way association refers to the two-way relationship is established
(1) 1 Parties and multi-party are preserved, and establish a two-way association relationship. This is sure to Save the success .

(2) 1 Party or parties, one party is not saved. The save fails because the classification is transient, so it fails.
(3) on the basis of (2) code, configure a cascade save in the mapping file for product. This saves the product when the classification is cascaded, so that the classification is not instantaneous object , save success !
            

Three, two-way maintenance of foreign keys, but only one-way association relationship (1) There is no classification in the product, because the foreign key can be null, so it can be saved successfully .
(2) There is no classification in the product, there are products in the classification. Products and classifications are saved and saved successfully .                          (3) There is no classification in the product, there are products in the classification, only the classification is saved.          This is definitely a failed save because the classification also maintains the foreign key. When the foreign key is maintained in the classification, it is found that the product in the collection is a transient object. So certainly save failed.。        Four, the foreign key only by the multi-party maintenance first to maintain the function of the foreign key to cancel, only in the classification of the mapping file, configure inverse to True
      (1) The product has the classification, only saves the product. As a result of saving the product, the foreign key will be maintained, and the classification object used to maintain the foreign key is a transient object, so it must Save failed。 (2) for (1) We added a save category after saving the product. When you save a product, the object used to maintain the foreign key is the persisted object. This will saved successfully .。 (3) The product has the classification, the classification has the product, only saves the classification. saved successfully。 Because the classification does not maintain foreign keys, the objects in the collection do not affect whatever state they contain.
(4) The product has the classification, only saves the classification. Must saved successfullyBecause the classification does not maintain foreign keys, and the objects that are used to maintain foreign keys do not. The same (3).
Five, summed up to see so many instances, found that the reasons for the failure of preservation are the same.       is to take it. The object that updates the foreign key (this description may be a bit around) cannot be transient. For example, if the product side maintains a foreign key, then the object that he uses to update the foreign key is the category within him. If this classification is transient, the save is definitely unsuccessful.



  



 

A study on the preservation of hibernate-to-multi-correlation relationship

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.