In a previous post we learned how we can use Hibernate native SQL queries in our Grails application. We can also execute custom SQL with Groovy SQL. We must create a new instance of groovy.sql.Sql
in our code to execute SQL code. The easiest way is to use a javax.sql.DataSource
as a constructor argument for the groovy.sql.Sql
class. In a Grails application context we already have a DataSource
and we can use it to inject it into our code. We must use the name dataSource
to reference the default datasource in a Grails application.
In the following sample we invoke a custom query (for Firebird) using Groovy SQL. Notice we define a property dataSource
in the Grails service PersonService
and Grails will automatically inject a DataSource
instance.
Continue reading →
Sometimes we want to use Hibernate native SQL in our code. For example we might need to invoke a selectable stored procedure, we cannot invoke in another way. To invoke a native SQL query we use the method createSQLQuery()
which is available from the Hibernate session object. In our Grails code we must then first get access to the current Hibernate session. Luckily we only have to inject the sessionFactory
bean in our Grails service or controller. To get the current session we invoke the getCurrentSession()
method and we are ready to execute a native SQL query. The query itself is defined as a String
value and we can use placeholders for variables, just like with other Hibernate queries.
In the following sample we create a new Grails service and use a Hibernate native SQL query to execute a selectable stored procedure with the name organisation_breadcrumbs
. This stored procedure takes one argument startId
and will return a list of results with an id, name and level column.
Continue reading →
In our Grails applications we might have fields that need the same combination of constraints. For example we want all email fields in our application to have a maximum size of 256 characters and must apply to the email constraint. If we have different classes with an email field, like domain classes and command objects, we might end of duplicating the constraints for this field. But in Grails we can combine multiple constraints for a field into a single constraint with a new name. We do this in grails-app/conf/Config.groovy
where we add the configuration property grails.gorm.default.constraints
. Here we can define global constraints with can be used in our Grails application.
Let's add a custom email constraint in our application:
Continue reading →
IntelliJ IDEA 13 added the Terminal tool window to the IDE. We can open a terminal window with Tools | Open Terminal.... To change the font of the terminal we must open the preferences and select IDE Settings | Editor | Colors & Fonts | Console Font. Here we can choose a font and change the font size:
Continue reading →
In the latest Grails releases we can execute our tests in so-called forked mode. This means a separate JVM is started with an isolated classpath from the Grails build system. When we want to run our tests in forked mode from within IntelliJ IDEA we get the following error: Error running forked test-app: Could not load grails build listener class (Use --stacktrace to see the full trace)
. To make running tests in forked mode work with IntelliJ IDEA we must add one of the IntelliJ IDEA supplied JAR files to the Grails classpath.
We need to search for the file grails-rt.jar
in the directory where we installed IntelliJ IDEA. For example on Mac OSX this would be Applications/IntelliJ IDEA 13.app/plugins/Grails/lib/grails-rt.jar
. We need to copy this file to the lib
directory of our Grails project. On *nix systems we can actually define a soft link to this location in the lib
directory. For example with the following command $ ln -s /Applications/IntelliJ\ IDEA\ 13.app/plugins/Grails/lib/grails-rt.jar lib/intellij-grails-rt.jar
.
Continue reading →
IntelliJ IDEA 13 has a new feature Search Everywhere. With this feature we can search for files, actions, classes, settings and more using a simple search dialog box. We must press the Shift button twice to get the Search Everywhere dialog.
Continue reading →
When we convert a List
or Set
to XML using the Grails XML marshalling support the name of the root element is either <list>
or <set>
. We can change this name by extending the org.codehaus.groovy.grails.web.converters.marshaller.xml.CollectionMarshaller
. We must override the method supports()
to denote the type of collection we want to customize the root element name for. And we must override the method getElementName()
which returns the actual name of the root element for the List
or Set
.
Let's first see the default output of a collection of Book
domain classes. In a controller we have the following code:
Continue reading →
When we use for example the compile
or war
command Grails will create files and stores them by default in the project's working directory. The location of the project working directory can be customized in our grails-app/conf/BuildConfig.groovy
configuration file. We remove the generated files with the Grails clean
command. This command will remove all compiled class files, the WAR file, cached scripts and test reports. But this doesn't remove all files in the project working directory. For example plugins or a temporary web.xml
file, which are stored in the project working directory are not removed. We must use the clean-all
command to also remove those files from the project working directory completely.
Let's take a look at the default settings in our grails-app/conf/BuildConfig.groovy
configuration file when we create a new Grails application:
Continue reading →
Since Grails 2.2 by default the run-app
command will launch the Grails application in a separate Java Virtual Machine. This is called forked Tomcat execution in Grails. This way the class path of the Grails build system and the application will not intervene and also both processes will have their own memory settings. We can see the settings in grails-app/conf/BuildConfig.groovy
where we find the configuration property grails.project.fork.run
. When we want to debug our application in an IDE like IntelliJ IDEA we cannot use the Debug command, because this will only allow us to debug the Grails build system. We will not reach breakpoints in our source code. But Grails 2.3 introduces an extra argument for the run-app
command: --debug-fork
. If we use this extra argument the JVM running the Grails application will stop and listen for a debug session to be attached and then continue. We can configure a Debug configuration in IntelliJ IDEA (or another IDE) to attach to the waiting Grails application and use breakpoints and other debugging tools like we are used to.
Suppose we have a Grails application named forked-debug and we have created a project in IDEA for this application. We click on the Select Run/Debug Configuration button and select Edit Configurations...:
Continue reading →
To generate an HTML select
we can use the Grails tag <g:select .../>
. We use the optionValue
attribute to specify a specific property we want to be used as the value. But we can also define a closure for the optionValue
attribute to further customize the value that is shown to the user.
Suppose we have a simple domain class Book
with a couple of properties. We want to combine multiple properties as the text for the HTML select
options. In the following GSP we define first a <g:select .../>
tag where we simply use the title
property. In the next <g:select .../>
tag we use a closure to combine multiple properties.
Continue reading →
Grails 2.3 added a lot of support for RESTful services. For example we can now use a respond()
method in our controllers to automatically render resources. The respond()
method accepts a resource instance as argument and a map of attributes that can be passed. We can use the includes
and excludes
keys of the map to pass which fields of our resources need to be included or excluded in the response. This way we can render partial responses based on a request parameter value.
First we start with a simple domain class Book
:
Continue reading →
When we convert data to JSON or XML (or any other format actually) in our Grails application we can register custom marshallers. These marshallers must contain the logic to convert an input object to a given format. For example we could have a book class and we want to convert it to our own JSON format. We have different options in Grails to achieve this, but for now we will create a custom marshaller class CustomBookMarshaller
. This class must implement the ObjectMarshaller<C>
interface. The generic type is the converter the marshaller is for and is in most cases either grails.converters.XML
or grails.converters.JSON
. Next we must make sure Grails uses our custom marshaller and we must register it. In the Grails documentation is explained how to do this via grails-app/conf/Bootstrap.groovy
where we invoke for example JSON.registerMarshaller(new CustomBookMarshaller())
. Or via grails-app/conf/spring/resources.groovy
where we must write an extra component with a method annotated @PostConstruct
where JSON.registerMarshaller(new CustomBookMarshaller())
is invoked.
But there is also another way using org.codehaus.groovy.grails.web.converters.configuration.ObjectMarshallerRegisterer
. This is a Spring bean just for configuring extra marshallers. The bean has a priority
property we can use to define the priority for this marshaller. Grails will use a marshaller with the highest priority if for the same class multiple marshallers are defined. We can assign a marshaller to the marshaller
property. And finally we must set the converter class, for example grails.converters.XML
or grails.converters.JSON
with the converter
property.
Continue reading →