1

Grails、Groovy、GSPは初めてです。

ドメインクラス「ProductCategory」があります。

class ProductCategory {

    static constraints = {
    }

    static mapping = {
        table 'product_category';
        version false;
        cache usage: 'read-only';
        columns {
            parent column: 'parentid';
            procedure column: 'procid';
        }

    }

    static hasMany = [children:ProductCategory];

    ProductProcedure procedure;
    Integer lineorder;
    String name;
    ProductCategory parent;
    String templatelink;
    char offline;

    String toString() {
        return id + " (" + name + ")";
    }
}

各カテゴリは親を持つことができます。私は既存のデータベースを使用していますが、テーブルにはそれを行うための列'parentid'があります。カテゴリに親(ルートレベル)がない場合、その親IDは0です。

親に関するデータがある場合はそれを表示しようとしているGSPがあります。

<g:if test="${category.parent}">
hello
</g:if>

私はこれが存在をテストするだろうという印象を受けました。カテゴリに親がある場合は正常に機能しますが、parentid=0になるとすぐに爆発します。

No row with the given identifier exists: [ProductCategory#0]

== 0をチェックしようとしましたが、機能しませんでした。「親」がオブジェクトであると想定されているためだと思います。

では、parentid=0がparent=nullと同じである、または親がないと仮定するようにするにはどうすればよいですか?

ありがとう

4

3 に答える 3

1

私は答えを見つけたかもしれないと思います:

parent column: 'parentid', ignoreNotFound: true;

ignoreNotFound はドキュメントのどこにもありませんが、うまくいくようです!

于 2010-01-27T03:29:29.520 に答える
0

parentidは 0 であってはなりませんnull

あなたの質問で私が理解できないのは、どうすればparentid == 0を持つことができるのですか?

于 2010-01-27T01:11:10.967 に答える
-1

親IDを手動で処理する必要はありません。次のようなドメインクラスを定義するとすぐに:

Class Foo {
    Bar bar
}

Gorm / Grailsは、外部キー列を自動的に作成します。そして、プロパティをnullableと定義すると、次のようになります。

Class Foo {
     Bar bar
     static constraints = {
         bar(nullable:true)
     }
}

...これをnullに設定して、nullをテストできます。

def f = new Foo(bar:null)
if (f.bar == null) { ... }
于 2010-01-27T12:37:15.680 に答える