Lightweight ORM Development Series: Object Relationships and cascade operations

Source: Internet
Author: User

The full name of ORM is object/Relation Mapping, that is, object relationship ing. Since ORM is developed, it is inevitable to involve the relationship between objects.

Through the abstraction of object-oriented ideas, we can define the relationships between objects as follows:

  • One-to-one
  • Multiple-to-one (one-to-multiple)
  • Many-to-many

We can easily create an enumeration for object relationships.

  Public   Enum Relation
{
None = 1 ,
Onetoworkflow,
Manytoone,
Onetoone,
Manytoyun
}

This relation enumeration will be used in relationattribute. For details, see lightweight ORM Development Series: attribute preparation.

Next, let's take a look at Cascade operations.

All mainstream databases support cascade operations. The following is a small example of cascade operations-cascade deletion.

Create Table
(
Id varchar ( 20 ) Primary key,
Password varchar ( 20 ) Not Null
)

Create Table B
(
ID Int Identity ( 1 , 1 ) Primary key,
Name varchar ( 50 ) Not Null ,
Userid varchar ( 20 ),
Foreign key (userid) references a (ID) on Delete Cascade
)
Table B creates the primary code ID corresponding to a for the external code userid, and declares cascading deletion.
Test data:
Insert a values ( ' 11 ' , ' Aaa ' )
Insert a values ( ' 23 ' , ' Aaa ' )
Insert B values ( ' Da ' , ' 11 ' )
Insert B values ( ' Das ' , ' 11 ' )
Insert B values ( ' WW ' , ' 23 ' )
The ID in Table A is deleted' 11 The USERID in Table B is 11 It is also automatically deleted by the database. This is Cascade deletion.
Delete Where ID = ' 11 '  

Databases generally support cascade operations such as on update cascade and on Delete cascade.

If we can use these relationships well, we can achieve cascading deletion like Nhibernate.

Below is a cascade Enumeration

  Public   Enum Cascade
{
All,
Saveorupdate,
Delete,
None
}

ThisCascadeWill be used in relationattribute. For more information, see lightweight ORM Development Series: attribute preparation.

Well, today's discussion is here, half past ten. Good night, everybody!

 

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.