maintenant, j'essaie de comprendre comment fonctionne la classe de domaine Grails et GORM. Donc, j'essaie des expériences:Grails.Domain classe. belongsTo.Why?
j'expérimente avec deux classe de domaine: Main et Sub.
Allons-y!
ÉTAPE 1:
class Main {
String name;
String value;
}
class Sub {
String name;
String value;
}
Rechercher MySQL:
CREATE TABLE `main` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `sub` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
RÉSULTAT: attendu. Tout est sympa.
ÉTAPE 2:
class Main {
String name;
String value;
Sub sub;
}
class Sub {
String name;
String value;
}
Rechercher MySQL:
CREATE TABLE `main` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`sub_id` bigint(20) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK3305B98FB5DA4E` (`sub_id`),
CONSTRAINT `FK3305B98FB5DA4E` FOREIGN KEY (`sub_id`) REFERENCES `sub` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `sub` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
RÉSULTAT: attendu. Tout est sympa. En termes de MySQL, nous avons une relation unidirectionnelle 1: 1. principal-à-sous. Oui?
ÉTAPE 3:
class Main {
String name;
String value;
Sub sub;
}
class Sub {
String name;
String value;
static belongsTo = Main
}
Rechercher MySQL:
CREATE TABLE `main` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`sub_id` bigint(20) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK3305B98FB5DA4E` (`sub_id`),
CONSTRAINT `FK3305B98FB5DA4E` FOREIGN KEY (`sub_id`) REFERENCES `sub` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `sub` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
RÉSULTAT: En termes de MySQL, nous avons une relation unidirectionnelle 1: 1. principal-à-sous. Oui? mais c'est la même chose que sur STEP 2. mais nous avons différents classe de domaine sur STEP 2 et sur STEP 3. ie, la méthode belongsTo n'affecte pas la structure de la table?
ÉTAPE 4:
class Main {
String name;
String value;
Sub sub;
}
class Sub {
String name;
String value;
static belongsTo = [main:Main]
}
Rechercher MySQL:
CREATE TABLE `main` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`sub_id` bigint(20) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK3305B98FB5DA4E` (`sub_id`),
CONSTRAINT `FK3305B98FB5DA4E` FOREIGN KEY (`sub_id`) REFERENCES `sub` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `sub` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
RÉSULTAT: En termes de MySQL, nous avons une relation unidirectionnelle 1: 1. principal-à-sous. Oui? mais c'est le même que sur STEP 2 et 3. mais nous avons la classe de domaine differents sur STEP 2 et l'étape 3 et le 4 STEP Regardez 3 exemple le plus récent, nous pouvons conclure que la méthode belongsTo ne modifie pas la structure de la table ... mais, mais, mais .. regardez cette étape
ÉTAPE 5:
class Main {
String name;
String value;
}
class Sub {
String name;
String value;
static belongsTo = [main:Main]
}
Rechercher MySQL:
CREATE TABLE `main` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
CREATE TABLE `sub` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`version` bigint(20) NOT NULL,
`main_id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`value` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `FK1BE407E56D06` (`main_id`),
CONSTRAINT `FK1BE407E56D06` FOREIGN KEY (`main_id`) REFERENCES `main` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8$$
Dans cet exemple, belongsTo affecte la structure la table, alors qui fait partie de ???
OK. Alors, quelle est la question que vous aimeriez poser spécifiquement? Peut-être que cela pourrait aider: http://www.grails.org/doc/latest/guide/5.%20Object%20Relational%20Mapping%20(GORM).html#5.2.1%20Association%20in%20GORM – mfloryan
Ma question spécifique : quel rôle appartientTo pour une table? J'ai fait beaucoup d'exemples - et le seul que j'ai vu un changement dans la table – user471011
BTW si vous voulez l'association bidirectionnelle 1: 1, géré par Grails, mettre 'belongsTo' aux deux classes. Et Grails ne supporte pas complètement 1: 1 pour l'instant, IIRC. –