grails-core
grails-core copied to clipboard
Referring to Thing.gormPersistentEntity from inside domain class Thing causes exception during build
Expected Behavior
./gradlew build
should not throw exception
Actual Behaviour
$ ./gradlew build
Welcome to Gradle 7.2!
Here are the highlights of this release:
- Toolchain support for Scala
- More cache hits when Java source files have platform-specific line endings
- More resilient remote HTTP build cache behavior
For more details see https://docs.gradle.org/7.2/release-notes.html
Starting a Gradle Daemon, 1 stopped Daemon could not be reused, use --status for details
> Task :configureChromeDriverBinary
Downloading https://chromedriver.storage.googleapis.com/2.45/chromedriver_linux64.zip
Unpacking /home/peter/.gradle/webdriver/chromedriver/2.45.0/chromedriver_linux64/1ropgkuyqn7cy6rtlx2wlvynq/chromedriver_linux64.zip to /home/peter/.gradle/webdriver/chromedriver/2.45.0/chromedriver_linux64/1ropgkuyqn7cy6rtlx2wlvynq
> Task :configureGeckoDriverBinary
Downloading https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz
Unpacking /home/peter/.gradle/webdriver/geckodriver/0.30.0/geckodriver-v0.30.0-linux64.tar/euthfmvi7pbf1d5mgtkqs707x/geckodriver-v0.30.0-linux64.tar.gz to /home/peter/.gradle/webdriver/geckodriver/0.30.0/geckodriver-v0.30.0-linux64.tar/euthfmvi7pbf1d5mgtkqs707x
> Task :test
ThingSpec > initializationError FAILED
org.springframework.beans.factory.BeanCreationException at ConstructorResolver.java:315
Caused by: org.springframework.beans.BeanInstantiationException at BeanUtils.java:224
Caused by: java.lang.ExceptionInInitializerError at Unsafe.java:1042
Caused by: java.lang.IllegalStateException at GormEnhancer.groovy:467
1 test completed, 1 failed
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> There were failing tests. See the report at: file:///home/peter/Nextcloud/Hal-Con/example/build/reports/tests/test/index.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 1m
13 actionable tasks: 13 executed
Steps To Reproduce
$ grails --version
| Grails Version: 5.1.2
| JVM Version: 11.0.15
$ grails create-app example
$ cd example
$ grails create-domain-class Thing
$ nano grails-app/domain/example/Thing.groovy
# Add the following line inside the Thing domain class
static tmp = Thing.gormPersistentEntity
$ ./gradlew build
Environment Information
$ uname -a Linux redacted 3.10.0-1160.2.1.el7.x86_64 #1 SMP Mon Sep 21 21:00:09 EDT 2020 x86_64 x86_64 x86_64 GNU/Linux $ java --version openjdk 11.0.15 2022-04-19 LTS OpenJDK Runtime Environment Corretto-11.0.15.9.1 (build 11.0.15+9-LTS) OpenJDK 64-Bit Server VM Corretto-11.0.15.9.1 (build 11.0.15+9-LTS, mixed mode)
Example Application
https://github.com/spierepf/example2
Version
5.1.2
I am not sure about the reason to do the above but you could workaround the problem by doing the following:
static getTmp() { Thing.gormPersistentEntity }
I think the reason you are seeing the above behavior is that GORM is not loaded when you are actually setting Thing.gormPersistentEntity. So, making it a static method instead of property may work because now it will ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
I believe this is expected behaviour and not a bug.
gormPersistentEntitfy is required by the GORM GraphQL plugin:
https://grails.github.io/gorm-graphql/snapshot/guide/index.html#existing:~:text=dataFetcher(new%20SingleEntityDataFetcher%3C%3E(-,Author.gormPersistentEntity,-)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%40Override%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20protected
I believe that gormPersistentEntity is already a static method, so adding a layer of indirection as suggested will not actually improve the situation.
Yes, I think you should directly access the gormPersistentEntity method instead of wrapping it. But, I am a little confused regarding improvement. Is there some part I am missing because I don't see any improvement in assigning the method call result to a static variable? which is why you are seeing the above error.
Thanks!
@peter-hal-con Can you articulate why it is that you want to have a static property in a domain class whose value is that of the domain class' gormPersistentEnity property? (https://github.com/spierepf/example2/blob/044f540562f8e555889fed4743c6dc3aa3b07d70/grails-app/domain/example/Thing.groovy#L8)