#  GORM for Mongo

#関連ページ
$lsx(.)

# GORM 6.1系のバグ（ただし6.0.5でも発生している模様）

## ドメインクラスに作成したメソッドでフィールドを変更するとDirtyチェックが機能しない
関連するIssue
* https://github.com/grails/grails-data-mapping/issues/961
* https://github.com/kaleidos/grails-postgresql-extensions/issues/106

### issueを噛み砕くと
* 上記issueに書いてある通り、ドメインクラスAにフィールドhoge, fuga...があるとする
* Aにフィールドを変更するようなメソッドを作る

```groovy
class A {
   String hoge
   boolean fuga
   .
   .
   .         
   void updateFoo() {
       this.hoge = "aaa"        
       this.fuga = true
   }
}

```
* 上記のようなメソッドを作り、ServiceやControllerから(A)instance.updateFoo()を呼んだ時にgormのdirtyチェックが走らずに、instance.save()した時に正常にアップデートされない


# 継承マッピング

## MappedSuperClassの実現

**1.3.0.GA時点**

```groovy
abstract class AbstractUser {

        static mapWith = "mongo"

}

class User extends AbstractUser {
}
```

- どちらも grails-app/domain 下に置く<br>
src/groovy 下では動作しない


### 継承される要素

- <font color="red">run-appしたまま書き換えても当該クラスにしか反映されないので注意すること</font><br>
(例えばAbstractUserのmappingを書き換えても、Userに反映されるのは再起動後)

```groovy
abstract class AbstractUser {

        static mapWith = "mongo"
        static mapping = {
                name index: true
        }
        static constraints = {
                desc nullable: true
        }

        String name
        String desc

        def beforeInsert() {
                doSomething()
        }

        def afterLoad() {
                doSomething()
        }

}

class User extends AbstractUser {

        static mapWith = "mongo"
        static mapping = {
                order index: true
        }
        static constraints = {
                lastLoginDate nullable: true
        }

        int order
        Date lastLoginDate

}
```

- indexの継承: OK~
    - ただし、ensureIndexが実行されるのでabstractUserコレクションは作成されてしまう
    - AbstractUserに
```
 mapWith = "none"
```
を記述するとコレクション作成を抑制できるが、「mapWith = "mongo"」なUserも強制的に除外されるので利用できない
- constraintsの継承: OK
- beforeInsertの継承: OK
- afterLoadの継承: OK



## Polymorphic associations

**1.3.0.GA時点**

```groovy
class User {
        static mapWith = "mongo"
}

class ActiveUser extends User {
}

class InactiveUser extends User {
}
```

- ドキュメントプロパティに以下のような型情報が追加される
 "_class": "ActiveUser",
- 全て grails-app/domain 下に置く<br>
src/groovy 下では動作しない
- 親クラスが abstract だと動作しない<br>
(save時に _class プロパティが入らず、ロードもabstractなので正常に行えない)


### collection per class hierarchy

Hibernateの「table per class hierarchy」的な。

```groovy
class User {
        static mapWith = "mongo"
        static mapping = {
                collection "user"
        }
}

class ActiveUser extends User {
        static mapping = {
                collection "activeUser"         // 無視される
        }
}

class InactiveUser extends User {
        static mapping = {
                collection "inactiveUser"       // 無視される
        }
}

def user1 = new ActiveUser()
user1.save()
def user2 = new InactiveUser()
user2.save()

ActiveUser.list()
```

- 子クラスのcollection指定は無視され、全て user コレクションに保存される


---

## Page Navigation

- Canonical URL: https://tips.weseek.co.jp/Tips/Grails/GORM%20for%20Mongo
- Permalink: https://tips.weseek.co.jp/5fb74c91a1ba4200489a4b84
- Parent: [Grails](/65069da24603e7ff39a6b3f0.md)
- Children: 0 total
- Total descendants: 0
- Siblings: 2 total
  - [RestResponder について](/5fb74d07a1ba4200489a4b93.md)
  - [Spock](/5fb74c7aa1ba4200489a4b79.md)
- Last updated: 2020-11-20T04:56:49.863Z by takuya
- Full page listing (all children regardless of count): https://tips.weseek.co.jp/_api/v3/page-listing/children?id=5fb74c91a1ba4200489a4b84
