Saturday, December 27, 2008

Simplistic CRUD application with Grails on GlassFish V3 Prelude

My colleague Matt wrote a brilliant tutorial: EJB 3.1 and JSF 2.0 with GlassFish V3 Prelude.

A commenter on Matt's post claimed that using Grails to create the same application would be a tremendous simplification.
I wanted to find out if this was really true, and how much effort it would actually take to achieve the same functionality with Grails.

This post is not a Grails tutorial, and it is also not the goal to create an intelligent application. The goal is to reimplement Matt's application as efficiently as possible.

I assume, that you have GlassFish V3 Prelude installed.

Step 1: Install Grails
Start the updatetool or start glassfish an go to the updatetool inside the admin console. Select Grails for installation and perform the installation. Set the environment variable GRAILS_HOME to point to the Grails installation inside GlassFish, and include the Grails binary in your PATH.

On my OS X system this meant putting the following lines in my .profile:
export GRAILS_HOME=/Applications/NetBeans/glassfish-v3-prelude/glassfish/grails

Step 2: Create a new Grails application
Execute grails create-app CRUD-GRAILS in on the console.
This creates a directory named CRUD-GRAILS with a fully functional Grails project skeleton.

You can start your new Grails project with grails run-app. This starts up GlassFish and loads the application. The application is available at http://localhost:8080/CRUD-GRAILS.
As you can see there is not much there apart from a welcome screen.

Step 3: Create the Book entity
Execute grails create-domain-class book from inside your project directory.
This creates the file grails-app/domain/Book.goovy that contains a skeleton class for the book entity.
You could also create the file manually, the grails command just also creates a file for tests...

Edit Book.groovy to add properties:
class Book {
	String name
	String isbn

Step 4: Create a controller
Execute grails create-controller book from inside your project directory.
This creates the file grails-app/controllers/BookController.goovy that contains a skeleton class for the book controller.

As with the Book entity, you could also create the file manually, the grails command just also creates a file for tests...

Edit BookController.groovy to provide the CRUD operations on the book entity (this is called scaffolding):
class BookController {

    def scaffold = true;

That's basically it! The application can create, update, show and delete books.

It took typing three commands and writing three lines of code ... all completed in less than 10 minutes ... not bad I would say!

Let's see what Grails has to say: grails stats
	| Name                 | Files |  LOC  |
	| Controllers          |     1 |     3 | 
	| Domain Classes       |     1 |     4 | 
	| Integration Tests    |     2 |     8 | 
	| Totals               |     4 |    15 | 

Ok, using dynamic scaffolding is a bit like comparing apples with oranges ... lets change that: grails generate-all
This generates the concrete views and the controller for the book entity, that have been generated dynamically by scaffolding up to now...
... now you can look at the code and adjust it to your needs.

Another little thing is getting rid of the Grails logo. Edit grails-app/view/layouts/main.gsp: Delete the lines between the <body></body> tags except <g:layoutBody /> ... I think we stay with the nice CSS and icons :-)

Ok, I don't claim that Grails is a silver bullet, but it is quite impressive how fast you can achieve some core functionality! The question is now how well it scales for real-world-requirements ...

Matt are you ready to implement some entity-relations, validations, conversations, AJAX-UI ... ? I would be ready for the challenge :-)

Friday, December 26, 2008

Using JPA and Hibernate with Maven

I have been fighting a bit to set up a Maven project with JPA and Hibernate as persistence provider.

The final solution is very simple. But when you get on a wrong track at the beginning, you end up in a terrible maze...

So here the most important hint:
The JBoss repository ( is quite a mess.
In the root of the repository you find directories (matching Maven GroupIds) for hibernate , hibernate-annotations and hibernate-entitymanager.
Do not use them!
They are outdated, do not provide current versions and most annoying do not declare their dependencies... so your pom will get a mess.

All dependencies should use the GroupId org.hibernate (browse into the org directory in the Maven repository).

So here is my working pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">




Thursday, December 25, 2008

Don't be too clever...

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.

--Brian Kernighan

Wednesday, December 24, 2008

Take it slow ...

A unit test that takes 1/10th of a second to run is a slow unit test.

- Michael C. Feathers
Working effectively with legacy code

Thursday, December 18, 2008

If programming languages were ...

braindead-closeup.jpg After Web Framework Trolling and Language Wars now come the comparisons:

  • If programming languages were religions...
  • If programming languages were cars...

  • My favorites:
    APL would be Scientology - There are many people who claim to follow it, but you've always suspected that it's a huge and elaborate prank that got out of control.

    Eiffel is a car that includes a built-in driving instructor with a French accent. He will help you quickly identify and learn from your mistakes, but don't you dare argue with him or he'll insult you and throw you out of the car.

    Wednesday, December 17, 2008

    Go go java! ... play catch-up with C#!

    z1xgZtgVQr5h.jpg I am reading Stephen Colebourne's JDK 7 language changes - Devoxx votes!.

    At the devoxx conference people voted on possible future features of the Java language.

    As a developer with a .NET background, I can't help to compare with C# ...
  • Properties: In C# since 1.0
  • Null handling: In C# 2.0 with nullable types and according operators
  • List/Map syntax: In C# since 1.0 with Indexers
  • Extension methods: In C# since 3.0. See here, but C# is going much further with LINQ
  • Method pointers: In C# since 1.0 with delegates. Later extended with anonymous delegates and Lambda Expressions
  • Multiline Strings: In C# since 1.0 with the @-syntax
  • Infer generics: In C# since 3.0 with general type inference

  • ... go go Java! C# is not resting ... 4.0 is in the pipes, offering another truckload of innovation (dynamic capabilities, co-contravariant features for generics, optional parameters (small thumbs up for Java) and named parameters ...)

    Update: The post is discussed at Hacker News.

    Tuesday, December 16, 2008

    UML redefined

    Mona ASCII.gif
    True, a picture says more than thousand words. Yet UML is not a picture but graphically arranged text.

    -- Daniel Tobler (Zühlke LAT)

    Monday, December 15, 2008

    The root of all evil ... For years Donald Knuths famous quote was standing:
    Premature optimization is the root of all evil

    But in his recent blog-post Adam Bien is taking it up with Donald Knuth:
    Premature encapsulation is the root of all evil

    In his sweeping post Adam also kills a pattern (seems to be his latest hobby), encompasses leaky abstraction and promotes KISS/YAGNI.

    Interesting read...

    Thursday, November 27, 2008

    New family member again!

    It has been less than 9 month since the last happy event, but my family has received a new member again:

    Unfortunately it has been a bit of a premature delivery ... it will take another two month before it will be allowed to go outside ...

    Musing about Code Generation, MDSD and DSLs

    Disclaimer: I am a developer of business applications. The following view is certainly shaped by my experiences in trenches of enterprise IT. I am aware, that there are other domains of software development. Especially in industrial and/or embedded systems development the world might look different. I have blogged about MDSD and code generation before: Code Generation vs. Code Synthesis, Sunday Morning on InfoQ, Part 2 and Generic and generated - an oxymoron?.

    I think MDSD is still too much tied to code generation.

    Code generation is only useful, when your code contains a lot of noise and/or is very repetitive. In other words, when your code can not mainly focus on the essential complexity, but is polluted with accidental complexity.

    In my opinion the trend in current platforms and frameworks is exactly to remove accidental complexity and letting the application code focus on essential complexity.

    So these new platforms/frameworks take a lot of the wind out of the sails of the MDSD movement.

    DSLs (textual ones) are another trend that tries to enable the sole focus on essential complexity. While DSLs can be used as source for code generation, they are not primarily tied to code generation. DSLs (especially internal DSLs) basically let it open to be interpreted/executed at runtime. [runtime code generation is somewhere in between].

    So even if DSLs are often mentioned together with MDSD, I think they are really an alternative to MDSD. And given the current hype, they also take the momentum out of the MDSD movement.

    If you have reached the goal of ultimately removing accidental complexity out of your code (I know this is fictitious), then you have arrived at a textual model of your business problem. This cannot be further simplified!

    Nice boxes and diagrams do not offer another simplification or elevation of the abstraction level! They may be good for visualization, but even that is questionable. A picture is not always the best representation to grasp complexity!

    Further more, the current state of the tooling involved in MDSD adds another level of accidental complexity (think: synchronization, diffing/merging, refactoring ...) which basically nullifies the ultimate goal of simplification!

    Look at the following ActiveRecord model, as an illustration of my theory:
    class Firm < ActiveRecord::Base
       has_many   :clients
       has_one    :account
       belongs_to :conglomorate
    I dont think that this can be any more simplified. Also any graphical representation with boxes and lines would be no simplification, and would not offer any more convenience (think about layouting, refactoring, searching, diffing ...).

    Saturday, November 22, 2008

    Some requirements are different...

    Last week I was at a requirement engineering course.

    One thing that stuck was, the Kano model, which can be used to classify requirements:

    Depending on their category (excitement-, performance- or basic-attibutes) the corresponding requirements have a very different influence on customer-satisfaction.

    Basic attributes can easily be overlooked, if you are not familiar with the domain, because often they are not explicitly mentioned.

    Excitement attributes are the factor that make the difference.

    Over time, with each new product-version, given attributes drift from excitement to performance to basic.

    Wednesday, November 19, 2008

    Fowler, a Werewolf and no Silver Bullets

    Fowler as a werewolf is just hilarious:

    This is from the No Silver Bullet Reloaded Retrospective OOPSLA Panel 2007.

    There are a lot of gems in the quote from this discussion! The sarcastic comments from Fowler alone are worth the read of the summary...

    Reading the summary there seems to be a general consensus that there is really no silver bullet.

    But this is also a dangerous consensus. As Bertrand Meyer (also in the summary) poses it:

    ... many people, especially managers, rejected new ideas of the ‘80s and ‘90s like OOP, because they did not believe the new technology proposed to them would have a serious impact on development ...

    Using the insight, that there is no silver bullet as an excuse to stop trying to improve is certainly not the way to go!

    I was a student of Bertrand Meyer at university, and he was a critic of Fred Brooks paper, because he claimed that it was smothering innovation and the desire for improvement.

    IntelliJ IDEA 8 on OSX crashes on 64-bit JVM

    Smart Java Beans 3.0 Ready IDE
    for Error Free EJB Development
    I am playing around with IntelliJ IDEA 8.

    The IDE kept crashing on me after building a project.

    The problem did not occur any more after configuring the default JVM to J2SE 5.0 32-bit (with Java Preferences).
    For some reasons I had changed that to Java SE 6 64-bit ...

    I am using: OSX Leopard 10.5.5 and IntelliJ IDEA IDEA 8 build #9013

    This could be the same problem as reported here. But I did not test the suggested solution (delete growl.jar).

    Tuesday, November 18, 2008

    Alternatives to Hibernate updated

    hibernate_icon.gif Recently I stumbled over another ActiveRecord implementation for Java: activeobjects.

    Instead of updating the list from my last blog post, I decided to start a Wiki-page: ORM solutions for Java

    The goal is not to list every existing framework, but solutions that are actually valuable when starting a new project today.

    Feel free to complement the list ...

    Tuesday, November 11, 2008

    Work smart not hard: another step in the right direction

    Today SpringSource acquired G2One, THE company behind Grails.

    Read the announcement from SpringSource.
    ... and the blog posts from the Graeme Rocher and Guillaume Laforge.

    I think this is a very pleasant step for getting Groovy and Grails into the enterprise and consequently a big step for enterprise developers that want to work smart not hard.

    In my experience it has been hard to bring Grails or Rails into the enterprise. Decision-makers in corporate-IT seem to be terribly afraid of new languages. In my opinion this is not justified, given the fact that other technologies are happily adapted, regardless of their over-complexity in many cases!

    If Groovy and Grails becomes a part of the Spring portfolio, the argument becomes much easier or even obsolete, because Spring is widely accepted in the enterprise.

    I have to start looking for a Grails project ...

    Friday, November 7, 2008

    Two views on the importance of code Today I had some time catching up with my podcast-backlog. Here the two most memorable quotes:

    Code Quality is very low on the list of things that matter.

    - Jeff Atwood on Hanselminutes #135

    You have nothing to do in Software engineering if you didn't ever code.

    - Markus Völter on SE Radio #112

    Slightly controversial... though I like to share Markus' opinion, I think Jeff's quote is the sad truth...

    Wednesday, November 5, 2008

    Anemic vs. Rich Domain Models

    There is an interesting discussion on stackoverflow: Why do we need entities?

    When I read through this discussion, I get the impression that most people here are looking at entities in the sense of an Anemic Domain Model. A lot of people are considering the Anemic Domain Model as an antipattern!

    There is value in rich domain models. That is what Domain Driven Design is all about. I personally believe that OO is a way to conquer complexity. This means not only technical complexity (like data-access, ui-binding, security ...) but also complexity in the business domain!

    If we can apply OO techniques to analyze, model, design and implement our business problems, this is a tremendous advantage for the maintainability and extensibility of non-trivial applications!

    There are differences between your entities and your tables. Entities should represent your model, tables just represent the data-aspect of your model!

    s_wedding-ring.jpg It is true that data lives longer than apps, but consider this quote:
    Models are forever ... data is a happy side effect.

    -David Laribee

    Some more links on this topic:
  • Why Setters and Getters are evil
  • Return of pure OO
  • Generalization - Another Gap between DB- and OO-Models
  • POJO vs. NOJO
  • Super Models Part 2
  • TDD, Mocks and Design

  • Tuesday, November 4, 2008

    Architectural cornerstones

    Recently I was listening to the excellent episode of Software Engineering Radio about the new website.

    There is also an article about the same project on InfoQ, focussing on the DDD aspects.

    As an aspiring architect I am sucking up qualitative and quantitive characteristics of interesting enterprise applications.

    So here is my summary of the facts as I understood them:

    The application stack consists of
  • Velocity as view technology
  • Spring
  • Hibernate3
  • Oracle

  • The interesting points here are:
  • No EJBs are used, just WAR deployment.
  • No higher-level view-framework is used, just templating with velocity.
  • The productive system is depoyed on Caucho Resin, no heavyweight appication server.

  • The nonfunctional requirements are:
  • 180 million hits a month
  • 18 million unique users
  • 17 Pages/s per Server with 8 servers as peak-performance

  • The developer-team has peaked at 104 members. Those were split up in 4 smaller teams.

    One of the most interesting aspect from a development perspective is the fact, that the business people are actually responsible for the domain model, not the programmers!

    Monday, November 3, 2008

    What it takes to stay happy: More!

    We act as though comfort and luxury were the chief requirements of life, when all that we need to make us happy is something to be enthusiastic about.

    — Charles Kingsley

    Recently at the shnit-festival I have seen the brilliant shortfilm "More" by Mark Osborne (better quality on youtube):

    The underlying theme of the movie is a frequent topic on my blog:
  • Scrum pigs - musing about commitment
  • Salary vs. Suckage - is there a relation?
  • Care and Commitment - Don't even get started without them
  • Job Conclusion
  • Motivation and Productivity

  • Friday, October 31, 2008

    Microsoft-ORM: Quo vadis? some time things were looking good for Object-Relational Mapping in the Microsoft space... but the tide seems to have changed:

    There is the Vote of No Confidence for the ADO .NET Entity Framework (Microsoft is reacting on this with the DP Advisory Council).

    And now Microsoft seems to kill their alternative LINQ to SQL!

    So what should developers with a decent sense for domain modeling do? I dont think returning to the DataSet is really an option ...

    Independent solutions will profit from this uncertainty: Genome, NHibernate, Vanatec ...

    Wednesday, October 29, 2008

    We are legion :-)

    blogging_monkeys.jpgMy colleague Matt started blogging!
    He seems to have attracted already some interesting commenters... what a start!

    You can find his blog here:

    Slowly the bloggers from our business-unit in Bern seem to challenge the blogging predominance from our London colleagues :-)

    Of course Matts posts will also be aggregated in my zühlke-stream.

    Tuesday, October 28, 2008

    Generic and generated - an oxymoron?

    oxymoron.jpg< quick and not well thought out comment >

    Last week I was at an interesting presentation.

    Some developers were presenting some stuff they were doing with Eclipse EMF and CDO. This was interesting and gave me some things to ponder about ...

    But thinking about it I have a big question mark:
    One of the marketing-slogans I heard over and over again was in the sense of:
    We are doing code-generation, but the cool thing is that it still remains totally generic!
    Well, sounds cool ... but ... is generated code and generic code not quite an oxymoron?

    If it is generic, why generate it? Wouldn't it be better to refactor it out in a generic component? That's the idea behind Code Generation vs. Code Synthesis.

    Is it not the benefit of code-generation to generate highly specific code? Since the code can be regenerated at any time, it is no flaw to be very concrete and verbose, those LOC do not count for maintainability! (I am not the only one to oppose this kind of reasoning, but it is a common argument...)

    Once more I had the feeling that I am just not getting the whole Code-Generation-Model-Driven-What-So-Ever-Movement ...

    </ quick and not well thought out comment >

    Monday, October 27, 2008

    We are all amateurs blindly stumbling in the dark!Ä._027.jpg/493px-Pieter_Bruegel_d._Ä._027.jpg Programming has become too complex. I suspect that we have lost it for good!

    Here is another evidence:

    Look at this blog post by Jeff Atwood about database deadlocks and look at the comments.

    The density of contradictions in the comments is just frightening!

    Considering that the commenters are probably the upper class of developers out there, it seems a miracle that there are actually working transactional systems out there...

    I am not considering me any better at all, a lot of the things the commenters are talking about seems like black magic to me. But I feel terrified by that fact ... thats the basics man! If we don't know how transactions work, how can we start building business applications?

    Thursday, October 23, 2008

    stackoverflow - let it flow some more! I am pretty late in joining the choir, but I think is an amazing site.

    This is the very definition of Web 2.0. Just try it: ask a question and be amazed ...

    The brains behind the site are the heavyweights Joel "on Software" Spolsky and Jeff "Coding Horror" Atwood.

    There are two interesting podcasts with Jeff Atwood, discussing the creation of the site: Herding Code #14 and Hanselminutes #134.

    The podcasts reveal some particular interesting facts about the technical realization [see also here]:
  • The site is based on the Microsoft ASP.NET MVC framework, which is still beta!
  • The DBMS is SQL Server 2005
  • The whole site is running on one server: two quad-core CPUs with 4GB RAM
  • Web-Server and Database take about the same load
  • The DB-schema consists of about 16 tables

  • Basing a heavy-traffic application on beta-technology, thats probably what they call extreme courage.

    The server seems quite a lightweight! I have seen enterprise applications with a lot less load that supposedly needed much more horsepower...

    I would have expected more tables in the schema... but maybe they were talking only about the dynamic part of the data...

    Monday, October 20, 2008

    Java EE: The blue pill of enterprise development?

    One thing that stroke me, when I first came across Carbonado was that it was originally developed by Amazon for internal use.

    This is another example where a real big boy is not using standard technologies that are brain-fed to foot-soldiers like me out here in the trenches of enterprise development. [Other examples are all the upcoming cloud-technologies: Google AppEngine, Amazon SimpleDB, Microsoft Strata ...]

    matrix_wideweb__430x326.jpgSometimes I think I am stuck in some kind of matrix: I am brainwashed that Java EE gives me the right tools for enterprise development. Those tools are not really attractive and provoke a lot of suffering, but hey that's the price for being part of the enterprise!

    Strangely, very often I stumble across enterprise-applications, that were entirely developed with those tools in a totally brainwashed and conform way. But they still suffer from exactly the problems that the tools promise to prevent (like performance, scalability, maintainability ...).

    On the other hand, sometimes I get a glimpse behind the scenes of the real big boys, like eBay, Amazon or Google... and I get the impression, that there is not much of Java EE there.

    I wonder, why might that be? Maybe I should start looking for the red pill...

    Friday, October 17, 2008

    Optimize your app - play Quake!

    The quake optimization rule:
    Your boss comes to you because your code is running too slow. You take a week to look at the code. Then you go back to your boss an say, that it takes you about 18 month to get a performance improvement by factor two.
    Your boss then says, well thats all right.
    You go back to your cube and play Quake for 18 month and wait for the next generation of Intel processors. That is an interesting application of Moors Law. But Ted's point is, that those times are over. Scaling processor power with higher frequencies has reached its limits.
    Increasing the cores in processors, does not make your existing applications faster for free. Entirely new programming concepts have to be applied to make use of the additional power.
    Functional languages can be helpful in implementing those concepts.

    Monday, October 13, 2008

    EJB 3 - The complete makeover?

    EJB has undergone quite a makeover!

    While EJB 1/2 was considered obese and ugly , EJB 3 now claims to be the complete opposite.

    I find the following observation illustrates the complete makeover:

    EJB 2 was one of the main reasons for the development of the Spring Framework. Spring aimed to provide a developer-friendly alternative to EJB:
    I wrote this book for architects and developers who have grown increasingly frustrated with traditional approaches to J2EE design, especially EJB. It shows what you can do right now t implement cleaner, more productive alternatives to EJB and move into the next era of web applications.
    - Rod Johnson (founder of Spring), 2004,
    J2EE Development without EJB

    During several years Spring was THE lightweight alternative to the heavyweight EJB model.

    Now with EJB 3 this seems to have changed to the exact opposite:
    The Spring framework then would be "Just Another EJB Container On Steroids" (JAEC :-)) - it would make, however, the integration between EJB 3 and Spring easier, than even now. This lowers the entry barrier for EJB 3 as well: the migration to pure Spring environment, in case EJB 3 wouldn't be sufficient for functional, or non-functional requirements, should be not that hard.

    EJB is now presented as a quick and easy technology, very lightweight but maybe not ready for advanced enterprise scenarios.
    Spring on the other hand earns more and more critique as beeing overly complex, verbose, xml-heavy [see Bob Lee here and here, Guice Comparison, another blog ...].

    One of my last projects was a simple web-application. We decided that EJB would be an overkill and went with a straight JSF-and-Hibernate-in-a-single-war-approach.
    Today I think that leveraging EJB3 would have made the implementation easier and not more complex, especially when implementing stateful-conversations.

    Backing up iTunes Library

    I have a Netgear Ready NAS NV+.

    I am using rsync to backup my music library to a share on the NV+. This is the exact command I am using:
    rsync -av --progress --stats Music/iTunes/ /Volumes/media/Music/iTunes/

    [I am posting this mostly as a reference for myself, because I keep forgetting rsync syntax]

    Saturday, October 11, 2008

    Are we all toddlers?

    Apache Foundation was founded in June 1999.

    Eclipse was announced in November 2001.

    Hibernate was registered on SourceForge in November 2001.

    Those are all indispensable cornerstones of my current professional life. At the time those cornerstones were built, I was finishing my studies at university.
    This means during most of the time in which I was being prepared for my future professional life, the stage in which this professional life should take place was not even nearly defined!

    This is like being a physics student in 1687, at the time when Isaac Newton published his Philosophiæ Naturalis Principia Mathematica...

    You could argue, that it's only the tools that are moving so fast in our industry, the concepts are remaining the same. But I dont't think that is true. We are still in a phase where the concepts are constantly developed and evolved. They have to, for being able to satisfy the ever growing expectations and requirements:

    Programming today is not the same as 10 years ago! Most of us have been thought that computer science is about mathematics. We had been learning about algorithm and data structures etc...
    Most of this is not really relevant today any more!
    [Discaimer: maybe my perception is a bit distorted from suffeing too long in the trenches of enterprise development, but I think nobody can generally deny the change]

    Compare this to the following statement from Teach Yourself Programming in Ten Years:
    Researchers (Bloom (1985), Bryan & Harter (1899), Hayes (1989), Simmon & Chase (1973)) have shown it takes about ten years to develop expertise in any of a wide variety of areas, including chess playing, music composition, telegraph operation, painting, piano playing, swimming, tennis, and research in neuropsychology and topology. There appear to be no real shortcuts.

    What does this mean for our industry? Are we all still toddlers, playing inside our little baby-fences?
    Related Posts Plugin for WordPress, Blogger...