Logo

dev-resources.site

for different kinds of informations.

DataBinding and GORM objects

Published at
5/20/2020
Categories
grails
groovy
gorm
Author
joemccall86
Categories
3 categories in total
grails
open
groovy
open
gorm
open
Author
11 person written this
joemccall86
open
DataBinding and GORM objects

In Groovy, you are able to do this:

class Widget {
  String name
}

def widget = new Widget(name: '')
assert widget.name == ''

Enter fullscreen mode Exit fullscreen mode

When this widget is a GORM object (in a Grails application), the default configuration doesn’t allow for this:

// Inside grails-app/domain
class PendingDeletion {
  String name
  String path
}

// Elsewhere in code
def pendingDeletion = new PendingDeletion(name: 'foo', path: '')
assert pendingDeletion.path == '' // Fails; pendingDeletion.path is null

Enter fullscreen mode Exit fullscreen mode

This is because the default configuration instructs the Grails data-binder to convert empty strings to null 1.

I’m not exactly sure why this is the default. In order to make the above snippet work in your Grails application, specify the following in application.yml:

grails:
  databinding:
    convertEmptyStringsToNull: false

Enter fullscreen mode Exit fullscreen mode

This allows the GORM map constructor to behave the same as a normal POGO map constructor.

Alternatively, just do not use the GORM map constructor when the field values can be null.

  1. http://docs.grails.org/3.3.9/ref/Constraints/nullable.html
gorm Article's
26 articles in total
Favicon
Gorm: Sneak Peek of Custom Data Types
Favicon
GORM, PostgreSQL & Atlas
Favicon
Gorm Pagination With Ease
Favicon
How to Use Arrays as Parameters in Golang?
Favicon
Basic CRUD Operations Using Golang, Gin Gonic, and GORM
Favicon
GORM and Goose Migrations
Favicon
Gin + Gorm Practical Guide, Implementing a Simple Q&A Community Backend Service in One Hour
Favicon
A secret weapon to improve the efficiency of golang development, a community backend service was developed in one day
Favicon
Building CRUD Operations in Golang 🎉
Favicon
Some GORM Tips and Notes
Favicon
Gorm level UP: how to upgrade and start to use Gorm v2
Favicon
5chan API - Golang, GORM, Go-fiber
Favicon
Pagination using Gorm scopes
Favicon
GOlang URL shortener service using postgres, redis, bulma
Favicon
RestFull API Detailing
Favicon
Daily Blog
Favicon
Rest API Database Blog Post
Favicon
Prevent updating query updates all records in GORM
Favicon
Init project
Favicon
Creating a complete golang rest api from zero to Hero
Favicon
ตัวอย่างการสร้าง Many to Many Association ผ่าน GORM
Favicon
DataBinding and GORM objects
Favicon
Creating an opinionated GraphQL server with Go - Part 4
Favicon
Creating an opinionated GraphQL server with Go - Part 3
Favicon
Grails: Mocking domain objects/criteria that reference Joda Time objects
Favicon
Go's ORM framework GORM best practices indefinite parameter usage

Featured ones: