apache / apache/grails-core

GORM id and version added to abstract base-class not to domain class extending base-class

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

Description

### Expected Behavior

When using a common abstract base class for both entities and command objects (injecting `Validateable`), the generated
GORM code (`id` and `version`) are injected into the domain class.

### Actual Behaviour

When using a common abstract base class for both entities and command objects (injecting `Validateable`), the generated GORM code (`id` and `version`) are injected into the abstract base class, and not into the entity object as expected.

This results in a validation error on a command object extending an abstract-base class that is also used to extend a domain class, on the fields `id` and `version`, even though they should not be part of the command object.

### Example:

Base class:
```groovy
abstract class BaseFoo {
String foo
}
```

Domain class:
```groovy
class Foo extends BaseFoo {

static constraints = {
foo nullable: false
}
}
```

Command class:
Domain class:

```groovy
class FooCommand extends BaseFoo implements Validateable {

static constraints = {
foo nullable: false
}
}
```

The decompiled `BaseFoo.class` contains `id` and `version` fields.

```java
public abstract class BaseFoo implements GroovyObject {
private String foo;
Long id;
Long version;

// Getters and setters are generated into the base class as well.
}
```

### Steps To Reproduce

Check out the example project and run

```./gradlew clean test```

And the only test will fail.

### Environment Information

Ubuntu (irrelevant)

### Example Application

https://github.com/sbglasius/grails-gorm-abstract-id-bug

### Version

6.2.3 and 7.0.x

Contributor guide

Open the contributing guide

Research direction

Start with the linked example application and run ./gradlew clean test to reproduce the failing test. Compare the generated or decompiled BaseFoo, Foo, and FooCommand classes, focusing on where id and version are added. Done means the fields and accessors are generated on the domain class rather than the shared abstract base class, and the test passes.

Written by the indexing model from the issue text.

Assessment

Domain
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.