Hibernate-mapping (i) Cascade Delete

Source: Internet
Author: User

Hibernate is a data access layer that uses a configuration file (Hibernate.properties or Hibernate.cfg.xml) and a mapping file (***.hbm.xml) to put Java objects or PO (persistentobject, Persistent objects) are mapped to databases in the database, and then by manipulating the PO, the data in the data table is increased, deleted, changed, checked and other operations.

First configuration file hibernate.properties I didn't use it, now. Hibernate is usually given to Spring management, multi-use Applicationcontex.xml configuration file Management.

a second ( * **.hbm.xml) file, which is the Object relational mapping file for hibernate with the database. Combining specific examples, the analysis of the relationship between entities, in the configuration file is how to reflect it.

generally speaking, one ( * **.hbm.xml) file that represents an entity Model class and a database Table 's Association, as follows:

<span style= "FONT-SIZE:18PX;" >

This indicates class class myShop.category.vo.Category corresponding table category;

now there is another class Categorysecond , it and Category A class is a many-to-one relationship:



Category.hbm.xml configuration file

<span style= "FONT-SIZE:18PX;" >


CategorySecond.hbm.xml configuration file

<span style= "FONT-SIZE:18PX;" >


combined with the above 2 configuration files, analyzing Hibernate -mapping configuration file.

On the "one" side, the set node information will be configured in the configuration file:

Lazy: The default is true using lazy loading, false means no delay loading (instant load);

order-by: A party to a set set of multiple entities within a set of the sort method by default is the primary key sort but can be changed;

Inverse: Invert. True indicates that the other party is responsible for control, and False indicates that it is controlled by itself. True to reduce the UPDATE statement to improve performance.

Cascade: Whether to use cascading operations; property values:

       All: Associated operations are performed in all cases, i.e. save-update and delete.

       None: Associated operations are not 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, the node is deleted.     For example, in a one-to-many relationship, student contains multiple book, which becomes an orphan node when a book is deleted in the object relationship.           


if you want to implement cascade deletion, simply configure the "one" side of the Set node: inverse= "true" cascade= "delete", so When you delete the "one" party, all records of the primary key of the party referenced to one of the many Parties are also deleted.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hibernate-mapping (i) Cascade Delete

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.