Wednesday, October 19, 2011

No JPQL order by query parameters?

So after multiple debugging session checking my Hibernate query and the database it appears that JPQL doesn't allow you to add the order by statement as a query parameter, such as:


@NamedQuery(name = "Review.getAllReviewsForObject", 
            query = "SELECT r FROM Review r where objectType.id = ?1 and objectId = ?2 and
                     status.code = 'ACTIVE' order by ?3 desc"
)


The work around I found was to create two named queries with the two options for order by in my case. This hardly seems ideal for situations when numerous multiple order by will be needed. 

Tuesday, September 13, 2011

Groovy and Grails = A refreshing change for Java developers!

I was really going to try and only post things that I come across in everyday Java development on this blog and not promote any one framework, but I can't hold back my excitement about Groovy and Grails.

I have used most of the major Java web frameworks throughout the years in an enterprise setting including: Struts, WebWork, Struts2, JSF and Spring MVC, GWT, Flex. Never has it been easier, quicker and more fun to build a web application using Java technologies.

Its amazing how far things have come since the Struts/JDBC days and its equally amazing that its taken this long for Grails to come about. The JVM has come through in a big way allowing other languages.

Some of my favorite parts about Grails:

  1. It's built on standard Java technologies that we all use today, Spring, Hibernate, Sitemesh, Maven
  2. Convention over Configuration
  3. No more DAOs! With dymanic finder methods you can kiss DAOs goodbye.
  4. Easy relationship mapping between Entities
  5. No more complex configuration of Spring and Hibernate! Its all done behind the scenes.
  6. Plugins! Adding things like full text searching with a few lines of code is awesome.
  7. FUN, PRODUCTIVE and actually INSPIRING to use!

Many big companies are catching on too, including Netflix!
Check out the podcast here for more information about Netflix and Grails: here

Anyways, this is just my shout out to a wonderful new Java framework. Here's to hoping that Grails catches on in a big way. Once you use it you will realize what Java web development has been missing all of these years. I promise you won't be disappointed. Grails the search is over

Also check out these Grails books:
Grails in Action
The Definitive Guide to Grails

Wednesday, August 31, 2011

Spring OpenEntityManagerInViewFilter persistence issues.

The old saying "use the right tool for the job" holds true here. There is a case where the ease of use and convenience of the OpenEntityManagerInViewFilter causes more trouble than its worth.

When you have the concept of "draftable" entities, that is entities that need to be in a draft state before going live, the OpenEntityManagerInViewFilter causes some strange behavior.

If you have a controller that queries an entity and then you change that entity it will automatically flush at the end of the transaction. Your changes are then persisted even though you may not want them to be. Detaching the object doesn't seem to help in some cases.

Could this be a bad design and a misuse of JPA persistence or is it just something that has to be dealt with while using OpenEntityManagerInViewFilter?

Draftable draftableObject = draftService.loadPossiblyFromDraft(id);
draftableObject.setName("Test");

//Only trying to save the changes as a draft object and not affect the original
draftService.saveAsDraft(draftableObject);

//After the request ends with OpenEntityManagerInViewFilter enabled
//The draftableObject changes are persisted to the entity as well as the draft.