Relationship between grails domain

Source: Internet
Author: User
Tags grails
The ORM-style domain will eventually be reflected in the database table, and the subordination of the domain class will be reflected by the database Association ing.
One-to-many/multiple-to-one
---
_ Example __:

Class country:
[PK] + id
+ Name

Class province:
[PK] + id
+ Name
[FK]-> + country

The 'country' country can have multiple provinces. In terms of countries and provinces, it is a _ one-to-many _ relationship.
The 'province 'province can belong to only one country, and the 'country' indicates the country to which it belongs as a foreign key. In terms of province and country, it is a _ many-to-one _ relationship.

_ Grails domain __:

Class country {
String name

// One-to-many, hascript, is a dictionary set.
Static hasvince = [Province: province]
}

Class province {
String name

// Multiple-to-one, Dictionary set
Static belonsto = [COUNTRY: Country]
}

The two domian in the Code describe their relationship in one-to-many/many-to-one. In this way, grails will help us perform cascade operations during various crud operations.
Many-to-many
---
_ Example __:

Class city:
[PK] + id
+ Name

Class Road:
[PK] + id
+ Name

Class city-Road:
[PK] + id
[FK]-> + city
[FK]-> + Road

The 'city' city can have multiple roads
'Road' a highway can cross multiple cities
In many-to-many relationships, an additional table is usually used to record the ing relationship.

_ Grails domain __:

Class city {
String name

// Many-to-many
Static hasvince = [Province: province]
}

Class Road {
String name

// Many-to-many
Static hasmany = [City: City]

// The Most Important Role of belonsto is to bind cascade operations.
// Here, the road operation will not affect the city
// If you operate the main city of the road, cascade operations will inevitably affect the road
Static belonsto = [country] // This is not a dictionary, but a list, because this will not create a field, but will be bound gradually
}

In multi-to-many relationships, if cascade operations are taken into account, the most important thing is to define the master-slave relationship, that is, which table is the master
After such code is generated, crud will automatically cascade.

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.