1

Is there a way to add my own auto generated field to domain like id and version , If yes the please guide me . provide me URL form where i can read and under stand the core concept of Grails and Domain specific language .

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
user3074874
  • 95
  • 1
  • 8
  • Do you want the auto generated fields to be represented in the source code of all newly created domain classes, do you want the auto generated fields to be added to all domain classes at compile time, do you want the auto generated fields added to all domain classes at runtime, or something else? All of those are simple to do and which one to use depends on what you are really trying to accomplish. – Jeff Scott Brown Apr 15 '14 at 19:45

3 Answers3

7

use install-template in the app to get all default templates:

grails install-template

after which you would be able to see /src/templates (newly created)

Modify DomainClass.groovy under /src/templates/artifacts as below:

@artifact.package@class @artifact.name@ {

    //according to your need
    Long myId
    Integer myVersion

    static constraints = {
    }
}

Done!!!!

Henceforth, when create-domain-class command is used to create a domain class, those fields will be auto populated.

dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • 1
    Thanks dmahapatro, How we write logic for this eg version is auto incremented and createDate get date of create . Also any URL form where i can read and under stand the core concept of Grails and Domain specific language . – user3074874 Apr 16 '14 at 05:28
  • You can start from [latest Grails documentation](http://grails.org/doc/2.3.x/) and can Google for Domain Specific Languages with Groovy to get ample of resources. BTW here is the doc for [`install-template`](http://grails.org/doc/2.3.x/ref/Command%20Line/install-templates.html) – dmahapatro Apr 16 '14 at 05:36
  • Thanks for help dmahapatro – user3074874 Apr 16 '14 at 09:44
1

I am not sure I am understanding your question correctly but here is the link to the web features of Grails documentation. The "Link Generation API" may be something you are asking after.

If you would like to manage ID and version than using Spring Security (plugin or full docs) or SQL features may be the direction you want to read more about.

EDIT: Try this Stackoverflow question and answer on using inheritance. Seems to be very similar to what you are asking.

Community
  • 1
  • 1
Nathan
  • 3,082
  • 1
  • 27
  • 42
  • 1
    In domain class id and version are automatically added. so my question is that how i add some other field like these to every domain – user3074874 Apr 15 '14 at 12:30
  • Here is a [discussion](http://grails.1312388.n4.nabble.com/How-to-add-fields-to-every-domain-class-td2536729.html) on the Grails user group about adding extra fields to a domain class. – Nathan Apr 15 '14 at 12:38
  • No its some thing else. In these user find some other way to do it.but question Is there a way to add my own auto generated field to domain like id and version ? – user3074874 Apr 15 '14 at 12:44
1

You would need to write an AST transform to inject the fields you want to add automatically. The one that injects ‘id’ and ‘version’ can be found here as an example:

https://github.com/grails/grails-core/blob/master/grails-core/src/main/groovy/org/codehaus/groovy/grails/compiler/injection/DefaultGrailsDomainClassInjector.java

You would then need to write a GORM event listener to automatically update the values of these properties. See

https://github.com/grails/grails-data-mapping/blob/master/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/events/AutoTimestampEventListener.java

For an example of the one that updates the dateCreated/lastUpdated properties.

These can both be written in a separate Gradle/Maven project which you then reference in the dependencies of your BuildConfig.groovy file.

Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37
  • Thanks for reply . Let check these . Do you have any site URL or doc on web for meta programming and DSL for grails – user3074874 Apr 16 '14 at 09:48