Combining Groovy, Grails, MySQL, and the Java Persistence API (Carol McDonald)

, , , , , July 31st, 2008

Original Source

Carol McDonald has written an excellent article describing how to get a Grails application running based on Sun’s open source technology stack, MySQL in the database end, Glassfish v2 as the server engine, NetBeans 6.1 as the IDE of choice. JPA is also thrown into the mix as the EnterpriseTips site is all about JEE related topics.

With the addition of support for scripting languages in the Java platform, there has been a lot of interest in combining into web applications scripting languages such as Groovy, Java technologies such as the Java Persistence API (JPA), and databases such as MySQL. Last year I wrote a Tech Tip titled Combining JavaServer Faces Technology, Spring, and the Java Persistence API that showed how you can use JavaServer Faces Technology, Spring, and the JPA to create an application that displays an online catalog of pets. In this tip, I’ll show you how to create an online catalog application using the Groovy language, the Grails framework, the MySQL database, and the Java Persistence API.

A package that contains the code for the sample application accompanies the tip. The code examples in the tip are taken from the source code of the sample (which is included in the package). In this tip, you’ll use NetBeans IDE 6.5 Milestone 1 to build the application and deploy it on the GlassFish application server. The NetBeans IDE is a modular, standards-based, integrated development environment (IDE) written in the Java programming language. The latest NetBeans IDE offering, NetBeans IDE 6.5 Milestone 1 (or M1 for short), offers many new features including support for Groovy and Grails. GlassFish is a free, open source application server that implements the newest features in the Java EE 5 platform.

A Summary of the Languages, Technologies, and Frameworks in the Sample Application

If you’re not familiar with Groovy, Grails, MySQL, or the Java Persistence API, here are brief descriptions:

  • Groovy is an agile and dynamic language for the Java Virtual Machine1. It compiles to Java bytecode and combines popular features from languages such as Smalltalk, Python, and Ruby.
  • Grails is an open-source web application framework that leverages the Groovy language and complements Java Web development. It aims to bring the “coding by convention” paradigm to Groovy. Grails is a Model-View-Controller based framework that simplifies the development of web applications by reducing the need for configuration files and by generating a lot of the things needed in a database-backed web application.
  • MySQL is the world’s most popular open-source database. It offers consistently fast performance, high reliability and ease of use.
  • The Java Persistence API provides a (plain old Java object) POJO-based persistence model for Java EE and Java SE applications. It handles the details of how relational data is mapped to Java objects, and it standardizes Object/Relational (O/R) mapping.

The Sample Application

The sample application displays an online catalog of pets sold in a pet store. Figure 1 shows the Catalog Listing page, which allows a user to page through a list of items in a store.

Catalog Listing Page

The article is well balanced, you will find references to common Grails artifacts as controllers, domain classes and views. Carol also shows how the datasource must be configured and the steps you must follow to get the application running. Continue reading the whole article at this link.

Tags: , , , , ,

Grails Documentation Widget (Marc Palmer)

, July 31st, 2008

Original Source

Marc Palmer has released the first version of the Grails Documentation Widget for Os X’s Dashboard.

I finally polished up and released the first version of my Grails documentation widget. I normally don’t dig widgets much but it’s really useful to be able to rapidly jump to the ref docs for a taglib etc without having to go to grails.org

This OS X Dashboard Widget gives you immediate access to the 1.0.x Grails documentation as found in the Reference section of grails.org

Using incremental search you just type the start of the method, property or tag etc and it will search the reference index extremely quickly.

NOTE: This widget requires internet access to work, it accesses the live grails.org docs.

You can download the widget from here.

Tags: ,

G2One QuickCast Site Launched! (Jeff Brown)

, , July 30th, 2008

Original Source

From Jeff Brown we learned that G2One has launched a new site devoted to screencasts on Groovy and Grails.

G2One have launched the G2One QuickCast site. A G2One QuickCast is fast and furious little movie demonstrating something interesting about the Groovy language and/or the Grails framework.

The first available screencast is Grails and Git ,this QuickCast demonstrates how a powerful version control system like Git can play a role in supporting a really agile development process. This show demonstrates that while an application is running in development, branches may be created and the developer may switch back and forth between those branches and see those changes in the browser immediately without the need to shutdown and restart the container.

If your version control system and web framework don’t work together to support this level of agility, you may not be using the most powerful tools for the job.

Enjoy!

Tags: , ,

[OSCON 2008] Web Frameworks of the Future: Flex, GWT, Grails and Rails (Matt Raible)

, July 29th, 2008

Original Source

Matt Raible has made available the slides form his Web Frameworks of the Future: Flex, GWT, Grails and Rails presentation at OSCON 2008. As you probably know Matt Raible is currently working on Grails/Rails related projects at LinkedIn.

Below is the presentation I’m delivering at OSCON today. Unfortunately, I had to remove slides on GWT and Flex to fit w/in the 45 minute time limit. I hope to expand this presentation in the future, as well as continue to develop the side project I’m working on using these technologies.

Tags: ,

Functional Test Driven Development with Grails and WebTest (Lee Butts)

, , July 24th, 2008

Original Source

Lee Butts has written a good post on applying functional testing to Grails apps using Canoo WebTest and the WebTest plugin. This is not an introduction to the WebTest plugin nor an article on why functional testing matters, the advantages of both may be clear to many by now, so Lee concentrates in two issues that gave him some trouble, at least on how he wanted the tests to be run. These are

  • the speed of execution of a set of tests
  • how to specify a subset of test to be run (a custom TestSuite if you will)

The Grails WebTest plugin comes with a script which will start your application, run all your test, and shut down the application. This is great for checking your code before checking in or for running on your continuous integration server. However, if you would like to use a WebTest to drive out some functionality it is far too slow.

To get around this I have been using a custom script and parent class for my WebTests. The script is basically a copy of RunWebtest.groovy supplied by plugin with the start/stop application code removed. It also has a small section of code to parse a class patttern and method pattern as arguments. It needs to be placed in the plugins/webtest-0.5/scripts directory of your application so that it can access the required WebTest configuration and resources.

RunWebtestOnly.groovy

This allows for a command such as:

grails run-webtest-only MyDomain edit

which will run all test methods containing the word edit but only those methods found in classes with a name containing the word MyDomain.

That is half of the solution, as it let you specify a subset of tests. One thing to notice is that because the database state is not refreshed between tests it forces you to code in a ’stateless’ manner, some may object on this as functional testing (or integration testing) relies on several components working together, where a predefined state must be set before the actual test. The other half of the solution is to plug in a custom TestSuite so that the subset of tests can be run.

The second part of the solution is the custom parent class for the WebTests. By default, the WebTest plugin requires you to add a suite method to your WebTest to specify the order in which to run the test cases contained within it. This is very useful if there are dependencies between the cases and testA must run before testB for example. To me, this is a bit of a smell that your tests are too fragile. If testA must run before testB in order to set up some state, refactor the code that creates that state into a method and re-use it in testB with unique values so that you can run testB regardless of database state.

Of course there are always exceptions, especially when it comes to keeping test execution time to a minimum, and in reality it may be better to set up common data once and re-use it in several tests. My opinion above is given with a “in an ideal world” disclaimer.

Apologies, I’m getting side tracked! What my custom parent class does is model JUnit in that it automatically builds a suite from all methods beginning in ‘test’. It also applies the class and method filters you gave to the run-webtest-only script which are passed through from Gant via system properties.

MyWebTest.groovy

Now with the help of the WebTestRecorder firefox plugin I can start to drive out a new page, menu item or behaviour.

Finally, following the spirit of TDD, Lee demonstrates a typical scenario for adding new functionality on a particular app. Starting with the WebTestRecorder firefox plugin, he launches a web session recording every single step of the new scenario, tweaking the test code when it makes sense and finally running the failing test (remember a good test starts red), then he simply writes enough code to make the test pass.

Tags: , ,

Grails, Jetty, Glassfish and JNDI Data Sources (Glen Smith)

, July 24th, 2008

Original Source

Glen has been busy updating groovyblogs.org to the latest Grails 1.0.3 release. He also took this opportunity to update some features and rework the site to accommodate messaging (JMS). Now he writes about his experience setting up JNDI datasources.

Today I’ve moved a bunch of my Grails apps over to JNDI Data Sources, and while it’s fresh in my mind, I thought I’d document a few of the tips and tricks involved.

First of all, why would you even want to use JNDI data sources? A few good reasons:

  • Most importantly, your app server can probably manage your database connections a lot better than you can
  • You don’t have to embed your username/password data in config files, and then end up committing them to public repos (this has led to several password changes for me in the past… the shame!)
  • Developers can call their local databases whatever they want (and they can be whatever type of database server they like) - just map the JNDI reference to whichever server and database floats your boat.
  • Does away with the need to ship db-specific drivers with your app (well.. in theory.. if you’re using Jetty for your DEV work , this may well not be true)

Glen shows portions of his configuration files, while at the same time giving a valuable warning: setting JNDI datasources with embedded Jetty on the development environment requires different settings compared to the production environment. He then proceeds to explain what to change between production environments.

Tags: ,

Creating a Grails Plugin in NetBeans IDE (Geertjan Wielenga)

, , July 23rd, 2008

Original Source

Geertjan Wielenga continues its exploration of the new Groovy/Grails features available in NetBeans 6.5 milestone 1. This time he describes the steps required to create a sample Grails plugin, but he doesn’t stop there, he also describes how to install it in an application and put it to work. With this guide and the other links he mentions you will be able to code your own plugins using NetBeans in no time.

Tags: , ,

Grails Development Made Even Simpler Using NetBeans IDE 6.5 (Meera Subbarao)

, , July 21st, 2008

Original Source

Meera Subbarao writes about her latest testing of Groovy/Grails support with NetBeans 6.5. You may remember Meera from a previous post (Highlights of Beginning Groovy and Grails: From Novice to Professional). She notes that this particular test began as a means to corroborate what Beginning Groovy and Grails proposes: Groovy and Grails increase your productivity. But to have a good level of productivity you require tools, given that Geertjan’s NetBeans to Grails guide was posted during the weekend, she decided to give it a try. In her own words:

After having read the book Beginning Groovy and Grails: From Novice to Professional, I was wondering if it was ever going to be as simple as a few clicks in any IDE for Grails development. Just yesterday, I read an article by Geertjan Wielenga(who is also my colleague at Javalobby/DZone) about how to get started with Grails in NetBeans IDE 6.5 in 5 simple steps. I had worked with NetBeans quite a lot for EJB3 development but I had never used it for either Groovy or Grails; the choice earlier was always Eclipse IDE.

I first followed the Book Demo and later moved to a more real life example. This was a litmus test which I thought NetBeans had to pass for developers to continue using the same for Groovy/Grails development. The example in the book has a few relationships; which is what we would generally have in any enterprise application.
domain-classes
I was able to create all the domain classes, controllers, manage relationships without ever leaving the IDE. Creating a Domain class or even a Controller, is as simple as right clicking on the appropriate nodes and providing meaningful names. The IDE creates the skeleton classes; we need to provide the meat within

If you are a Groovy or a Grails fan, download the latest version of NetBeans and give it a try. You can develop, test and run your Grails application without ever opening a command window. The Groovy editor has basic coloring, formatting and bracket completion. The GSP editor has coloring, highlighting of GSP tags, expressions and scriptlets. You can mix and match Java and Groovy as well.

Tags: , ,

Grails: Lessons Learned (James Lorenzen)

, July 21st, 2008

Original Source

James Lorenzen writes about two some lessons he has learned while working with Grials and One-to-many relationships. The first one is related to eager vs lazy fetching of collections. James writes:

I by no means am a grails expert, but based on my experience don’t set the eager fetching in your Domain as the example shows (unless you have a very good reason and understand the consequences). Lazy vs Eager fetching is well described in the grails documentation, so I won’t repeat it, but anyone using One-to-many relationships needs to know the differences.

The default behavior in grails is lazy fetching, which results in n+1 queries. In some cases this might be ideal, in others it may not. When it’s not you have a couple of choices. The example in the grails documentation sets a fetchMode property on the Domain. This sets it globally and every time the Domain is accessed, grails is going to load all it’s many relationships. The path I recommend is to specify the fetch mode when retrieving the data. For example, the list() method has a parameter called fetch and can be used like this: Book.list(fetch: [authors: “eager”]). This gives you the most flexibility by not specifying the fetch mode globally, but allowing you to fetch eagerly when necessary.

The second lesson is related to child deletion from a parent’s collection, we are sure other developers have encountered this problem before, int this case James solved it by piggybacking on Hibernate Events. In order to effectively delete a child you need to remove it form the parent’s collection and call delete() on it, so instead of writing two lines of code everywhere you just define a beforeDelete event handler in the Child domain class. James writes:

The hibernate events are interesting. By default grails supports 4 events: beforeInsert, beforeUpdate, beforeDelete, onLoad. However, there is a recent plugin called Hibernate Events Plugin that adds 7 more events: beforeLoad, afterLoad, beforeSave, afterSave, afterInsert, afterUpdate, afterDelete.

Tags: ,

Grails: This Time With Tools (Geertjan Wielenga)

, , July 20th, 2008

Original Source

Geertjan Wielenga, a well known NetBeans advocate and zone leader at Javalobby , has written a detailed step by step guide to run a sample Grails application with the latest NetBeans 6.5 milestone. Go check it out and don’t forget to send feedback to the NetBeans teams in order to get better support for Groovy/Grails in the official release of version 6.5.

Tags: , ,