开发者

Grails.Domain class. belongsTo.Why?

now, i try understend how works in Grails domain class and GORM. So, i try experiments:

i experiment with two domain class: Main and Sub.

Let's go!

STEP 1:

class Main {
    String name;
    String value;
}

class Sub {
    String name;
    String value;
}

Look 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$$

RESULT: Expected. All nice.

STEP 2:

class Main {

     String name;
     String value;
     Sub sub;
}

class Sub {

     String name;
     String value;
}

Look 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$$

RESULT: Expected. All nice. In terms of MySQL we have a unidirectional relationship 1:1. main -to- sub. Yes?

STEP 3:

class Main {

    String name;
    String value;
    Sub sub;
}

class Sub {

    String name;
    String value;

    static belongsTo = Main
}

Look 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$$

RESULT: In terms of MySQL we have a unidirectional relationship 1:1. main -to- sub. Yes? but this is the same that on STEP 2. but we have differents domain class on STEP 2 and on STEP 3. ie, method belongsTo does not affect the structure of the table?

STEP 4:

class Main {

    String name;
    String value;
    Sub sub;
}

class Sub {

    String name;
    String value;

    static belongsTo = [main:Main]
}

Look 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$$

RESULT: In terms of MySQL we have a unidirectional relationship 1:1. main -to- sub. Yes? but this is the same that on STEP 2 and 3. but we have differents domain class on STEP 2 and on STEP 3 and on STEP 4. Look 3 most recent example, we can conclude that the method belongsTo does not affect the structure of the table... but, but, but.. look on this STEP

STEP 5:

class Main {

    String name;
    String value;
}

class Sub {

    String name;
    String value;

    static belongsTo = [main:Main]
}

Look MySQL:

CREATE TABLE `main` (
  `id` 开发者_JS百科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$$

In this example, belongsTo affects the structure of the table, so which makes belongsTo???


As per grails documentation belongsTo indicates how operations cascade. This does not necessarily set the cascades in the database but affects the behaviour of save() and delete() operations in GORM.

So this answers why there is absolutely no difference in SQL between STEP2 and STEP3.

In STEP5 you reverse the direction of your relationship so Main no longer know about the Sub.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜