How can I use the built-in GORM classes in Grails?

Following the GORM docs , I tried using the following domain class with Grails 2.2.1:

package grailscompositiontest class ScafPerson { String name ScafAddress homeAddress ScafAddress workAddress static constraints = { name(nullable: false, blank: false) } static embedded = ['homeAddress', 'workAddress'] } class ScafAddress { String number String code } 

The controller simply uses scaffolding:

 package grailscompositiontest class ScafPersonController { static scaffold = true } 

Unfortunately, this will not work, it causes a server error as soon as I look at the "create" view:

 URI: /GrailsCompositionTest/scafPerson/create Class: java.lang.NullPointerException Message: Cannot get property 'id' on null object 

Any idea what I'm doing wrong?

+4
source share
2 answers

I found that the grails documentation indicates the built-in class definitions, either the same source file as the domain class, or is placed in src / groovy. I put Address.groovy in src / groovy and created a user interface for my Family class that has an inline object. The generated creation page now displays correctly, and the address fields are included in the Family page. However, I still had to change the name and value fields of each form field in the generated _form.gsp.

For example, the following generated code ...

 <g:textField name="line1" required="" value="${addressInstance?.line1}"/> 

has been changed to ...

 <g:textField name="address.line1" required="" value="${familyRegistrationInstance.address?.line1}"/> 

If the main domain object is the FamilyRegistration domain class (specified in the generated _form.jsp as familyRegistrationInstance), which has a built-in Address object. I am using grails 2.3.11.

The save operation now works with correctly filled fields of the embedded object. In my case, this means that FamilyRegistration.address is populated correctly.

Here is the relevant information from the Grails reference document ...

"The built-in component class is usually declared in the same source file as the owner class or in its own file in src / groovy. You can place the component class under grails-app / domain, but if you do this, the Grails will automatically create a dedicated table for it Putting a class under src / groovy is usually the best option because you can share the component for multiple domain classes.

http://grails.org/doc/2.2.4/ref/Domain%20Classes/embedded.html

+4
source

I ran into this problem the other day. I believe that there may be a mistake in the templates used by the functionality of the forests. You can either update the template, or if you do not want to guess with the templates, run generate-all , as Benoit mentioned, then correct the generated view.

To update the template:

  • grails> install-templates
  • open src / templates / scaffolding / renderEditor.template
  • find the following line:
  sb << 'value = "$ {' << domainInstance << '.'  << property.name << '} "'

and change to

  sb << 'value = "$ {' << domainInstance << '?.'  << property.name << '} "'

To fix the generated view:

  • grails> generate-all grailscompositiontest.ScafPerson
  • open views / scafPerson / _form.gsp
  • to seek
  <g: field name = "id" type = "number" value = "$ {scafAddressInstance.id}" required = "" />
     <g: field name = "version" type = "number" value = "$ {scafAddressInstance.id}" required = "" />

and change to

  <g: field name = "id" type = "number" value = "$ {scafAddressInstance? .id}" required = "" />
     <g: field name = "version" type = "number" value = "$ {scafAddressInstance? .id}" required = "" />

Note , you will do this twice since you have homeAddress / workAddress

+5
source

All Articles