Grails Anti-Pattern: Everything is a Service

The context

Grails makes it very easy to put any logic of your application in a service. Just grails create-service and you’re good to go. There’s a single instance by default, injectable anywhere. Powerful stuff and makes it easy to get up ‘n running very fast!

Creating a new application, following so-called “best practices” from blogs like these 🙂 and the ‘idiomatic Grails-way’ described in the docs and in tutorials work in the beginning, but there’s always a tipping point — where the application has grown a reasonable size — where one should start following a different, maybe less-Grailsey, strategy.

So what can go wrong by creating services in your application?

Continue reading “Grails Anti-Pattern: Everything is a Service”

Debugging Grails 3, Spring Boot or Gradle with Eclipse

This is a small post for those who’s primary IDE is Eclipse for developing Grails 3 apps or plugins — and are still wondering how to debug their application.

First of all, to clarify: there’s no “Grails” debugging, it’s just Spring Boot and Gradle these days, with Grails 3. Any way to debug these kind of applications allows you to debug a Grails 3 application.

There’s lots of SO posts and Google hits about this subject, so I’m creating this post also as a short “simplest solution” summary for myself and for others.
Continue reading “Debugging Grails 3, Spring Boot or Gradle with Eclipse”

Grails Expert Tip #8 – There’s a Bean for That

grails-expert-tipRecently I code reviewed some of the Grails (2.4.5) code of another team (of course :-)) and here’s a snippet I came across.

In some “search helper service”:

 public int countListWithoutFetching(Collection collection) {
   def session = grails.util.Holders.grailsApplication.mainContext.sessionFactory.currentSession
   return ((Integer) session.createFilter(collection, "select count(*)" ).list().get(0)).intValue()

This team somehow needed some ‘generic’ way of counting the amount of records in the database, I thought.

Continue reading “Grails Expert Tip #8 – There’s a Bean for That”

Quicktip! Grails scaffolding: enums and i18n

Grails scaffolding feature is a great asset for quickly generating create/read/update/delete (CRUD) screens for your domain classes. Create an entity, create a controller and put static scaffold on top. For various types of properties within your domain class a suitable (HTML) input is used, to take the user’s input: booleans become checkboxes, associations with other domain classes become (depending on ownership and constraints) e.g. dropdowns or multi-selects, etc.

Even an enum works out of the box as one could imagine 🙂 This post serves as a small quicktip – primarily for me and other developers looking for these kind of things – of internationalizing scaffolded screens in general and enums in particular using  toString() and Spring’s MessageSourceResolvable.

Take the following Product with a Status:

class Product {

	enum Status {

	String name
	Status status

	static constraints = { name blank: false, unique: true }

class ProductController {

	def scaffold = true

Opening up our /product/create url will show what you would expect.

Continue reading “Quicktip! Grails scaffolding: enums and i18n”

Hibernate class metadata through Spring

On a Spring-Hibernate project I’m working on we use a custom way of filling and clearing specific database tables in unit tests. Yes, something like DbUnit but ofcourse completely hand-made, tailored to our situation 🙂 In a TestNG testcase we do something like this:

public void setUp() {
	DatabaseFiller.fillTable("UserAccounts", "useraccounts.txt");

public void tearDown() {

It bugged a little bit, that in this testcase e.g. we’re testing operations on the User entity (e.g. User.class) – which for database specific reasons was mapped to a “UserAccounts” table – but we still have to find the actual table name for ourselves. There are many reasons to map explicitly to a certain table name: prevent database reserved keywords (“user” is often reserved), enforce a naming convention (“tbl_user”), etc. If no specifics have been set for Hibernate it will take its own naming convention, else one could indicate the desired name through the javax.persistence.Table annotation – if using JPA. Continue reading “Hibernate class metadata through Spring”