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.