apache / apache/grails-core

Unable to persist domain classes from the Groovy console

Open
#11,654 1 comment 0 reactions 0 assignees View on GitHub
relates-to: spring boot
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

### Task List

- [x] Steps to reproduce provided
- [x] Stacktrace (if present) provided
- [x] Example that reproduces the problem uploaded to Github
- [x] Full description of the issue provided (see below)

### Steps to Reproduce

1. Create a new Grails app with a domain class named `Foo`
2. Start a Groovy console from Grails
3. Attempt to `save` a `Foo` instance

i.e.,

$ grails create-app scratch
$ cd scratch
$ ./grailsw create-domain-class Foo
$ ./grailsw console

followed by

new scratch.Foo().save()

in GroovyConsole.

### Expected Behaviour

The `Foo` instance should be persisted to the (in memory, default H2) DB.

### Actual Behaviour

java.lang.IllegalArgumentException: Argument [scratch.Foo : (unsaved)] is not an instance of [class scratch.Foo] which this validator is configured for
at grails.gorm.validation.PersistentEntityValidator.validate(PersistentEntityValidator.groovy:61)
at org.grails.orm.hibernate.AbstractHibernateGormInstanceApi.save(AbstractHibernateGormInstanceApi.groovy:124)
at org.grails.datastore.gorm.GormInstanceApi.save(GormInstanceApi.groovy:119)
at org.grails.datastore.gorm.GormEntity$Trait$Helper.save(GormEntity.groovy:100)
at org.grails.datastore.gorm.GormEntity$Trait$Helper$save.call(Unknown Source)
at scratch.Foo.save(Foo.groovy)
at scratch.Foo.save(Foo.groovy)
at org.grails.datastore.gorm.GormEntity$save.call(Unknown Source)
at ConsoleScript1.run(ConsoleScript1:1)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

### Environment Information

- **Operating System**: macOS 10.14.6
- **Grails Version:** 4.1.0.M2
- **JDK Version:** 11.0.9 (problem also occurs under 15.0.1)
- **Container Version (If Applicable):** N/A

### Related issue (indirect example on GH)

This issue was also outlined in a separate repository, https://github.com/grails/grails-data-mapping/issues/970, with a proposed workaround.

Removing the `spring-boot-devtools` dependency allows `Foo` to be saved from the console but this adversely affects ongoing development—as such, it's a bit of a kludge.

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with a Grails 4.1.0.M2 app, the Foo domain class, and the Grails Groovy console, with and without spring-boot-devtools. Start at grails.gorm.validation.PersistentEntityValidator.groovy:61 and the save calls in AbstractHibernateGormInstanceApi.groovy and GormInstanceApi.groovy. Done means new scratch.Foo().save() succeeds from the console and persists to the default H2 database.

Written by the indexing model from the issue text.

Assessment

Tech stack
groovy, spring-boot
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.