Hibernate-mapping (1) cascade deletion and hibernate cascade Deletion

Source: Internet
Author: User

Hibernate-mapping (1) cascade deletion and hibernate cascade Deletion

Hibernate acts as the data access layer. It uses the configuration file (hibernate. properties or hibernate. cfg. xml) and ing files (***. hbm. xml) maps a JAVA object or PO (PersistentObject, persistence object) to a database, and then adds, deletes, modifies, and queries data in the data table through the PO operation.

I have never used the first configuration file hibernate. properties. Currently, Hibernate is generally handed over to Spring for management and is used to manage applicationContex. xml configuration files.

The second (***. hbm. xml) file is the object relationship ing file between hibernate and the database. How does one analyze the relationship between entities based on specific instances in the configuration file.

Generally, a (***. hbm. xml) file represents the association between an object Model class and a database table, as shown below:

<span style="font-size:18px;">

This indicates that the class myShop. category. vo. Category corresponds to the table category;

Now there is another CategorySecond class, which has many to one relationship with the Category class:



Category. hbm. xml configuration file

<Span style = "font-size: 18px;"> 


CategorySecond. hbm. xml configuration file

<Span style = "font-size: 18px;"> 


Analyze the Hibernate-mapping configuration file in combination with the preceding two configuration files.

On the "1" side, the configuration file will configure the set node information:

Lazy: The default value is true, which indicates delayed loading. false indicates no delayed loading (instant loading );

Order-by: the sorting method of multiple object sets in one set is primary key by default, but can be changed;

Inverse: reverse. True indicates that the other party is responsible for controlling, and false indicates that it is controlled by itself. True can reduce the update statement and improve the performance.

Cascade: Indicates whether to use Cascade operations. attribute value:

All: Join operations are performed in all cases, that is, save-update and delete.

None: No association operation is performed in all cases. This is the default value.

Save-update: perform join operations when you execute save/update/saveOrUpdate.

Delete: perform join operations when executing delete.

All-delete-orphan: When a node becomes an orphan node in the object graph, it is deleted. For example, in a one-to-many relationship, Student contains multiple books. When a book is deleted from an object relationship, the book becomes an orphan node.


If you want to implement cascading deletion, you only need to configure the set node of one party: inverse = "true" cascade = "delete". After deleting one, all records that reference the primary key of one of multiple parties will also be deleted.



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.