"Hibernate learning"-Cascade operations

Source: Internet
Author: User

Cascade Policy: Responsible for controlling the association between the object to the object cascade, including update, delete, and so on, the other objects are also affected, such as I delete an object, then it is a many-to-one relationship of the object is also deleted.

After using the crawl strategy, after interviewing a sub-operation of the note: Cascadetype.merge Way

@OneToOne (Cascade={cascadetype.merge}) @JoinColumn (name= "company_id") Publiccompany Getcompany () {return company;} Publicvoid Setcompany (company company) {this.company= Company;}

Cascadetype.merge Cascade UPDATE, cascade operation is usually to the entity's additions and deletions to the operation, and the crawl strategy reflects the association object, mainly reflected in the above.

In addition, there are several types of cascading:

Cascadetype.refresh: Cascade Refresh, when multiple users operate an entity at the same time, in order to get the data from the user is real-time, before using the data in the entity can call the REFRESH () Method!

Cascadetype.remove: Cascade Delete, when the Remove () method is called, the primary key table is deleted before the foreign key table is deleted!

Cascadetype.merge: Cascading updates

Cascadetype.all: Contains all of the cascading properties above.

The above cascade operations can only be effective if the database constraints are met.

These strategies, annotations are flexible configuration, there are many parameters for us to choose, which type needs to be directly used on it. Very convenient, set which parameters at a glance.

First I declare that this cascade operation is the same as the Cascade operation we have previously understood, except that we are using annotations in this form. Now the difference between this and our previous use is that we were directly against the database, if it is directly targeted at the database is equivalent to writing dead; now with hibernate annotations, it is the relationship between object and object, object-oriented, more flexible. If it is an object operation then our entity must be flexible, do not write dead, write dead feeling and the same way as the previous database.

Although the cascade operation is convenient for us to manipulate the data, we cannot rely on it, and if the amount of data is particularly large or many-to-many relationships, the cascade will be slow.

"Hibernate learning"-Cascade operations

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.