Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8894501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:30:49+00:00 2026-06-14T23:30:49+00:00

Is there any way to set a default value to domain class property? I

  • 0

Is there any way to set a default value to domain class property?
I have a class called PayMethod, where I want the name property to default to "Cash" and I want this default value when I create this table, is this possible using Constraints?

package abc

import util.UserUtil
import embed.AuditUser

class PayMethod {

    String name = "Cash"

    AuditUser audit = new AuditUser()
    static embedded = ['audit']    

    static constraints = {
        name blank: false, size: 5..30, unique: true
    }

    static mapping = {
        table 't01i0010'
        id column: 'F_ID', precision: 4, scale: 0
        name column: 'F_NAME', length: 30, defaultValue: 'Cash'
        version column: 'F_REVISION'
    }

    def authUserService
    int insertIndex = 0
    int updateIndex = 0
    static transients = ['authUserService', 'insertIndex', 'updateIndex']    

    def beforeInsert = {
        audit.entryUser = UserUtil.user()
        audit.entryDate = new Date();
    }

    def beforeUpdate = {
        audit.reviseUser = UserUtil.user()
        audit.reviseDate = new Date();
    }

    def afterInsert = {
        if(insertIndex == 0){
            def user = audit.entryUser
            def date = audit.entryDate
            log.info "POST INSERT => ENTERER: ${user} ENTERED: ${date}"
        }
        insertIndex++
    }

    def afterUpdate = {
        if(updateIndex == 0){
            def user = audit.reviseUser
            def date = audit.reviseDate
            log.info "POST UPDATE => REVISE: ${user} REVISED: ${date}"
        }
        updateIndex++
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:30:50+00:00Added an answer on June 14, 2026 at 11:30 pm

    This will be possible in 2.2 which should be released this week or next. See http://jira.grails.org/browse/GRAILS-5520 for the relevant feature request. The syntax will be

    static mapping = {
       name defaultValue: "'Cash'"
    }
    

    For now you’ll need to do what you’re doing – set the value as the default value of the field. You can manually update the database schema, or do the work as part of a migration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to set the default value for a column as an
author = models.ForeignKey(User) Is there any way I can set the default value of
On colModel, is there any way to set the default value if the result
Is there any way that can I set default value as Empty.string in Model.
Is there any way to set all the columns(int) default value at a time,
By any chance, is there any easy way to set a default MasterView for
Is there any way to set a CSS class on an input item in
Is there a way to set default value of paramter in sqlplus script without
Is there any way to set up a PendingIntent with the download manager? Or
Is there any way to set order by clause in HQL by run time.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.