apache / apache/grails-core

Table per subclass doesn't work with abstract base class in hierarchy

Open
#14,521 4 comments 1 reaction 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I wanted to create a hierarchy of classes with the **table per subclass** strategy (see example below). Since we have common functionality in our domain classes, we have an **abstract base class** that is extended by all other domain classes.

Note: if I remove the abstract base class from the hierarchy, the application starts and works as expected.

### 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
Put the following classes in the grails domain folder.
```
abstract class BaseEntity {
// common behavior for all entities in the application
}

class X extends BaseEntity {
static mapping = {
tablePerHierarchy false
}
}

class A extends X {
static hasMany = [bs: B]
}

class B extends X {
A a
}
```

### Expected Behaviour
I think the given class hierarchy is not that complex and pretty common and should just work.

### Actual Behaviour
An exception is thrown during application boot.
```
Caused by: org.hibernate.MappingException: Entities [example.A] and [example.B] are mapped with the same discriminator value 'null'.
at org.hibernate.persister.entity.SingleTableEntityPersister.addSubclassByDiscriminatorValue(SingleTableEntityPersister.java:450)
at org.hibernate.persister.entity.SingleTableEntityPersister.(SingleTableEntityPersister.java:424)
... 57 common frames omitted
```

### Environment Information

- **Operating System**: Windows 10
- **GORM Version:** 7.0.1
- **Grails Version (if using Grails):** Grails 3.3.10
- **JDK Version:** 8

### Example Application
The 4 given classes under "Steps to Reproduce" are sufficient to reproduce the exception in a newly created grails application.

Contributor guide

Open the contributing guide

Research direction

Create a new Grails application and place the BaseEntity, X, A, and B classes from the reproduction in its domain folder. Start by running the application to confirm the Hibernate MappingException during boot, then inspect GORM table-per-subclass and abstract-base-class mapping behavior. Done means the hierarchy boots without duplicate null discriminator values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.