homicki opened a new issue, #16464:
URL: https://github.com/apache/grails-core/issues/16464

   ### Expected Behavior
   
   A query on an intermediate class returns the rows of that class and the rows 
of all its subclasses. In the example below, Car.count() returns 3: one Car 
row, one SportsCar row and one Sedan row. Car.list() returns the same three 
rows, and the HQL query select count(*) from Car returns [3]. 
grails-data-hibernate5 gives these results. The tests in the example 
application pass on grails-data-hibernate5.
   
   ### Actual Behaviour
   
   The query returns only the rows whose discriminator value is the 
intermediate class itself. The rows of its subclasses are missing. No error 
occurs. Dynamic finders, criteria queries and HQL all give this result.
   
   ### Steps To Reproduce
   
   1. Define a table-per-hierarchy tree with three levels:
   ```
   class Vehicle { String name }   // root class
   class Car extends Vehicle {}    // intermediate class
   class SportsCar extends Car {}  // leaf class
   class Sedan extends Car {}      // leaf class
   ```
   2. Save one Car, one SportsCar and one Sedan.
   3. Query the intermediate class:
   ```
   Car.count()                                   // returns 1, expected 3
   Car.list().size()                             // returns 1, expected 3
   Car.executeQuery('select count(*) from Car')  // returns [1], expected [3]
   ```
   
   Cause: RootBinder.addSubclass ([v8.0.0-RC1, line 
94](https://github.com/apache/grails-core/blob/v8.0.0-RC1/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/cfg/domainbinding/binder/RootBinder.java#L94))
 calls root.addSubclass(subClass) for every class in the tree. Hibernate 
expects each Subclass to be added to its direct superclass. Hibernate then 
finds the subclasses of a class through getSubclasses(). Because every class is 
added to the root, the intermediate class has no subclasses in the Hibernate 
mapping model. (RC2 is the same)
   
   Proposed fix: subClass.getSuperclass().addSubclass(subClass). A local build 
with this change passes the full grails-data-hibernate7-core test suite. New 
tests for queries on an intermediate class fail without the change and pass 
with it. I can open a pull request with these tests after this issue is 
acknowledged.
   
   ### Environment Information
   
   Grails 8.0.0-RC1
   Hibernate 7.4.10.Final
   Groovy 5.1.3
   JDK 25. 
   The problem reproduces on H2 2.4.240 and on MariaDB 10.11
   
   ### Example Application
   
   https://github.com/homicki/grails-h7-inheritance-repro
   
   ### Version
   
   8.0.0-RC1


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to