Hi guys,

I was working with a sample Grails application and I needed a way to fetch the name of the table using a domain object. I searched and found a solution.

Get table associated with a domain programmatically:

For example:-

If table name if defined explicitly we can simply import this:-

and use:-

and if we have an instance of the Jft class we can use this:-

But the above can only be used if table name is defined explicitly in mapping.

If we want to get a table name even if it is not specified in mapping closure, we can fetch it using SessionFactory:-

ApplicationHolder is a static singleton holder and is used to return GrailsApplication instance.

ClassMetadata is used to extract entity class metadata(data about data). Documentation

Hope it helps 🙂