Hatem Jaber
Hatem Jaber

Reputation: 2402

How to Properly Set Property Value in Grails Domain Class

Just doing some testing and Google searching and ran across a suggestion stating that you should set the defaultValue parameter in the mapping for a property if you want to set a default value. Originally I had it set like so:

property = value

I can see the values set in the database as per my defaults. When I switched it to this:

static mapping = {
    property defaultValue: value
}

So at this point I have both set like the examples above and was wondering if I really needed that. It was working fine for me without the mappings, but maybe the mappings do something else that i'm not yet aware of.

Upvotes: 1

Views: 558

Answers (1)

Joshua Moore
Joshua Moore

Reputation: 24776

It depends on your expected behavior of your application. Both are valid, and work perfectly fine.

The first, by assigning a default on the property of the domain class will not only set a default at the database schema level but provide a default on a new instance of your domain class.

The second, by setting a default value within the mappings closure will only set a default at the database schema level.

So, depending on how you expect a new instance of your domain class to behave you have a choice. Default at the instance and database schema level, or just at the database schema level.

The choice is yours. Flexibility.

Upvotes: 2

Related Questions