0

I have a domain with named query:

class Publication {
   String title
   String author

   static namedQueries = {
       publicationsWithBookInTitle {
           like 'title', '%Book%'
       }
   }

    @Override
    String toString() {
        return title
    }
}

and in controller I have

def show() {
    Publication publicationInstance = Publication.publicationsWithBookInTitle.get()

    println "Get: ${publicationInstance}"
    println "List: ${Publication.publicationsWithBookInTitle.list()}"

    respond publicationInstance
}

and my unit test:

void "test show"() {
    given:
    Publication publication = new Publication(title: 'MyBook', author: 'MB').save(flush: true, failOnError: true)

    when:
    controller.show()

    then:
    model.publicationInstance == publication
}

When I run the test it gives me null when using get() method and my test fails. System output is

Get: null
List: [MyBook ]

I search for this but not found any solution of this issue.

Why this is happening and what is the solution or workaround of this issue?

NOTE:- I am currently using grails 2.3.9. But my project initially created in grails 2.3.5 then upgraded to grails 2.3.9.

MKB
  • 7,587
  • 9
  • 45
  • 71
  • a dumb thing, but sometimes when strange errors happens i try to clean the project. Have you tried this? – leomeurer Aug 12 '14 at 13:33
  • @meurer Yes I have tried this but no luck. – MKB Aug 12 '14 at 13:50
  • 1
    Worked for me in 2.3.9 and 2.4.2. – dmahapatro Aug 12 '14 at 14:24
  • @dmahapatro - OK, Then may be project specific issue. I will create a sample project and test it. Will update you soon. Thanks. – MKB Aug 12 '14 at 14:45
  • 1
    @dmahapatro - Yes you are right, it is working in `grails 2.3.9`. My project initially created in `grails 2.3.5` then upgraded to `grails 2.3.9`. I have created a sample app in `grails 2.3.5` and it is failing in it. I have also upgraded it and the result is same, failing. – MKB Aug 12 '14 at 17:09
  • I tend to create a whole new app with the newest version and then port code from old to the new project to avoid these kind of hassles. Good that you found it out. – dmahapatro Aug 12 '14 at 17:12
  • 2
    @dmahapatro - I have found the solution. The hibernate plugin version in the application is `hibernate:3.6.10.6` when I upgrade it to `hibernate:3.6.10.15` fix the issue (https://grails.org/2.3.9+Release+Notes). Your comment shows me the correct way. Want to mark that as answer. Can you post your comment with this information. Thanks..,. – MKB Aug 12 '14 at 18:16
  • 1
    I just validated the issue. I would rather suggest "you" to add your finding as an answer and **accept it** for others who would face this issue in future. Glad that you spent time to find the root cause. :) – dmahapatro Aug 12 '14 at 18:37

1 Answers1

2

Finally with the help of dmahapatro comment I figure out the root cause (@dmahapatro THANKS).

Named queries gives correct result in grails 2.3.9. But my project is upgraded from grails 2.3.5 which is using hibernate:3.6.10.6 and haven't changed to the hibernate:3.6.10.15 as recommended in the grails 2.3.9 release notes. Once I upgraded the project hibernate version my issue resolved.

NOTE:- You will face the issue if you are using grails 2.3.5.

MKB
  • 7,587
  • 9
  • 45
  • 71