Grails 3.0.1 and Hibernate Session error - grails

I am following a book example (Grails in Action 2nd edition) which is based on Grails 2.* but I am using the new Grails 3.0.1.
When I create a domain class that looks like:
package qotd
class Quote {
String content
String author
Date created = new Date()
}
I get an Exception thrown whenever I try to interact with the DB through the groovy console.
org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session;
Caused by: org.hibernate.HibernateException: No Session found for current thread
I have tried to add #Transactional to the domain class and also swith to a lower JDK version(7) but none of them works.
I have also tested with Grails 3.0 and results are same.
If I downgrade to Grails 2.5.0 it works so it is a Grails 3.* issue. Gradle could be the issue.

I wrapped everything inside a transaction for Grails 3
qotd.Quote.withTransaction {
new qotd.Quote(author: 'Larry Wall',
content: 'There is more than one method to our madness.').save()
}

Related

Register custom constraints

I'm trying to upgrade Grails 2.3.7 project to Grails 3.2.3. In 2.3.7, I used custom constraints and register them in /conf/Config.groovy using:
org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)
Then I can use something like this in domain:
static constraints = {
approvedDate(description: '>= applyDate')
}
However, in Grails 3.2.3, When I put above command (and remove org.codehaus.groovy from package name) in /conf/application.groovy I got following error:
Error occurred running Grails CLI: No signature of method: groovy.util.ConfigObject.registerNewConstraint() is applicable for argument types: (groovy.util.ConfigObject, groovy.util.ConfigObject) values: [[:], [DESCRIPTION_CONSTRAINT:[:]]]
I've notice that validation class is somewhat changed in Grails 3. However using constraint class from Grails-validation still got the same error.
All validation plugins I found were long abandoned before Grails 3. And I can't find any document for register new constraint in Grails 3.2.
Calling ConstrainedProperty.registerNewConstraint in /grails-app/init/BootStrap.groovy works. (tested with Grails 3.2.4)
class BootStrap {
def init = { servletContext ->
grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)
// The rest of bootstrap code
}
}
Note. Previously, I call it from main() in /grails-app/init/Application.groovy. It works for running application. However, it does not work with integration test.
Another way you can create the runtime.groovy under config and register your constraints in the runtime.groovy as in grails 2.x.x:
org.codehaus.groovy.grails.validation.ConstrainedProperty.registerNewConstraint('description', my.validation.DescriptionConstraint)

Grails access variable in build.gradle from Service class

I am sitting in front of a Grails 3.1.4 Application. The build.gradle file looks like this:
buildscript {
...
}
version "01.01.12.99"
...
ext {
...
}
repositories {
...
}
dependencyManagement {
...
}
dependencies {
...
}
From one of my Service Classes I want to access the version variable. Is this somehow built in into Grails or am I trying to do something impossible? Are there ways to load this variable from a Service class?
I searched a bit and found this, but it accesses the Grails version not the project version.
Well, I recently started using Grails 3. Here I got the solution:
println System.properties.getProperty("info.app.version")
And you will get "01.01.12.99". This is working on Grails 3.1.8 for me.
Another way to achieve this (in addition to the solution provided by Shashank Agrawal) is relying on grailsApplication bean, with the following code:
grailsApplication.config.getProperty('info.app.version')
I tested this with Grails 3.1.9 and 3.2.7.

How we can use A template profiler plugin in grails 2.4.3

Forgive me for my English. Actually I want to use A template profiler plugin in grails 2.4.3. But it is unable to install in this grails version. It is available for the grails version 1.3.2. And I want to use it for grails 2.4.3. When I am trying to install. It shows an error
| Error Compilation error: startup failed:
C:\Project\target\work\plugins\profile-template-0.1\grails-app\services\profile\te
mplate\ProfileTemplateService.groovy: 5: unable to resolve class
org.codehaus.groovy.grails.commons.Configurat ionHolder # line 5,
column 1. import
org.codehaus.groovy.grails.commons.ConfigurationHolder ^
C:\Projects\target\work\plugins\profile-template-0.1\grails-app\services\profile\te
mplate\ProfileTemplateService.groovy: 8: unable to resolve class
org.codehaus.groovy.grails.commons.Configurat ionHolder # line 8,
column 26.
boolean isRecording = !(ConfigurationHolder.config?.profile?.template?.disabled as Boolean)
^
2 errors
Can anybody help me to get me out of this. Or You can suggest me another alternate option for this.
Thanks in advance.
If you have access to the source code of the plugin you could follow this:
In Grails 2 we no longer use ConfigurationHolder we use GrailsApplication.getConfig() instead. In the ProfileTemplateService add field GrailsApplication grailsApplication (Spring will inject it during app boot up) and change the:
ConfigurationHolder.config?.profile....
to
grailsApplication.config.profile...
Your service class should look like:
class ProfileTemplateService {
GrailsApplication grailsApplication
def method() {
a = grailsApplication.config.profile
}
}
Obviously there can more hoops to jump through before you actually make the plugin work.

Error with #CompileStatic on grails service that uses google Cache

Using grails 2.3.2, Java 1.6.0_65, trying to compile the following placed in the services directory. Even if it is not a service, and just put in the src/groovy directory, it still causes the same compile error.
I installed groovy 2.1.9 (and tried 2.2.0) which appears to be the version used by grails 2.3.2 and ran groovyc -cp quava-13.0.1.jar TestCache.groovy and it worked fine. So it appears to be something related to grails.
package somewhere
import com.google.common.cache.Cache
import groovy.transform.CompileStatic
#CompileStatic
class TestCacheService {
private final Cache<URL, String> cache
TestCacheService() {
cache = null
}
}
I receive the following error:
General error during instruction selection: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
at sun.reflect.annotation.AnnotationParser.parseEnumArray(AnnotationParser.java:673)
at sun.reflect.annotation.AnnotationParser.parseArray(AnnotationParser.java:462)
at sun.reflect.annotation.AnnotationParser.parseMemberValue(AnnotationParser.java:286)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:222)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3127)
at java.lang.Class.getAnnotation(Class.java:3086)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:113)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.reflect.Method.declaredAnnotations(Method.java:693)
at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:686)
at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:175)
at org.codehaus.groovy.vmplugin.v5.Java5.configureClassNode(Java5.java:362)
at org.codehaus.groovy.ast.ClassNode.lazyClassInit(ClassNode.java:258)
at org.codehaus.groovy.ast.ClassNode.getInterfaces(ClassNode.java:353)
at org.codehaus.groovy.ast.ClassNode.declaresInterface(ClassNode.java:945)
at org.codehaus.groovy.ast.ClassNode.implementsInterface(ClassNode.java:925)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.getResultType(StaticTypeCheckingVisitor.java:2629)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:421)
at org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:49)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:69)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:193)
at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:163)
at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorOrMethod(StaticTypeCheckingVisitor.java:1435)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructor(ClassCodeVisitorSupport.java:119)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1051)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitClass(StaticTypeCheckingVisitor.java:162)
at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitClass(StaticCompilationVisitor.java:110)
at org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:60)
at org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:132)
at org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:176)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1036)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:572)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:506)
at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:59)
at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:215)
at org.codehaus.groovy.ant.Groovyc.runCompiler(Groovyc.java:1104)
at org.codehaus.groovy.ant.Groovyc.compile(Groovyc.java:1155)
at org.codehaus.groovy.grails.compiler.Grailsc.compile(Grailsc.java:78)
at org.codehaus.groovy.ant.Groovyc.execute(Groovyc.java:770)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
Is this a known bug?
Any workaround?
Google's Cache class uses the Nullable annotation. I had multiple nullable annotation implementations on my classpath. I think removing 'edu.washington.cs.types.checker:checker-framework:1.6.4' from the classpath solved this problem.

Grails 2.3.0 Restful Service Runtime Exception

I´m currently running Grails 2.3.0 and Java jdk1.7.0_40 on a Windows 7 machine, no IDE. I wanted to see how the new Rest functionality looks like. So I created the domain class as follows:
package fergara
import grails.rest.*
#Resource(uri='/Cars', formats=['json', 'xml'])
class Car {
String model
static constraints = {
}
}
The error I got when open the url:
URI
/FergaraTest/Cars/1 Class
org.codehaus.groovy.runtime.typehandling.GroovyCastException
Message
Cannot cast object 'fergara.CarController#12c43f7' with class 'fergara.CarController' to class 'org.codehaus.groovy.grails.plugins.web.api.CommonWebApi'
I feel that the Oracle´s Java SDK is the problem. To make the app run I have to pass "no-reloading" to the grails run-app command, otherwise I get an error saying that it could not start the VM and cannot open the springloaded-core-1.1.3.jar.
A have the Grails 2.3.0 installed on an Ubuntu 13.04 with OpenJDK6 and it is working good.
Hope someone can shed a light on this. Thank you.

Resources