开发者

Flattening an object model with Gorm?

开发者 https://www.devze.com 2023-02-18 12:28 出处:网络
Is there a way I can easily flatten an object to one table in Gorm? I have several conceptual entities which always need to be joined to their parent class. That is, I have this:

Is there a way I can easily flatten an object to one table in Gorm? I have several conceptual entities which always need to be joined to their parent class. That is, I have this:

class A{
   B other;
   String name;
   Str开发者_高级运维ing value;
}

class B{
   String val1;
   String val2;
}

Is there a way to annotate this so that val1 and val2 appear exclusively in table A?


Add other to the embedded list in class A:

class A{
   B other;
   String name;
   String value;

   static embedded = ['other']
}

See section 5.2.2, Composition in GORM: http://grails.org/doc/1.0.x/guide/5.%20Object%20Relational%20Mapping%20%28GORM%29.html


Mark the field other embedded with a static property:

class A {
    B other
    String name
    String value
    static embedded = ['other']
}

The autogenerated schema will then contain two fields called other_val1 and other_val2 in the table for `A'.

If you want B objects to only be stored as part of an A object, move B.groovy from grails-app/domain to src/groovy

0

精彩评论

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