DEV Community

Joe McCall
Joe McCall

Posted on • Originally published at joemccall86.gitlab.com on

2 1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more