Stefan Kendall
Stefan Kendall

Reputation: 67832

Flattening an object model with Gorm?

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;
   String value;
}

class B{
   String val1;
   String val2;
}

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

Upvotes: 0

Views: 549

Answers (2)

Kaleb Brasee
Kaleb Brasee

Reputation: 51955

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

Upvotes: 2

ataylor
ataylor

Reputation: 66069

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

Upvotes: 2

Related Questions