amador-duran-toro opened a new issue, #14277:
URL: https://github.com/apache/grails-core/issues/14277
In Grails 4.0.3 and Grails 3.3.11, if you have an inheritance hierarchy with
an abstract superclass, e.g.
```{groovy}
package mypackage
abstract class A {
String name
// ...
// properties
// ...
}
```
```{groovy}
package mypackage
class B extends A {
String b
// ...
// properties
// ...
}
```
```{groovy}
package mypackage
class C extends A {
String c
// ...
// properties
// ...
}
```
And you populate the concrete classes in `Bootstrap.groovy`, e.g.
```{groovy}
package mypackage
class BootStrap {
def init = { servletContext ->
A.withTransaction { status ->
def listOfB = []
(1..25).each { i ->
listOfB << new B(name:"b${i}",b:"${i}st")
}
B.saveAll( listOfB )
def listOfC = []
(1..25).each { i ->
listOfC << new C(name:"c${i}",c:"${i}nd")
}
C.saveAll( listOfC )
}
}
def destroy = {
}
}
```
If you use scaffolding, the _index_ page for `A` shows an empty list instead
of a paginated list of 50 objects (25 B objects and other 25 C objects).
Apart from that, it shows a _new_ button that generates a
`java.lang.InstantiationException` when clicked because it tries to instantiate
an abstract class.
A quick and dirty workaround for this problem is the following:
1. Generate views for the abstract class (only `index.gsp` is relevant)
2. Edit the generated `index.gsp` in the following way:
* Comment the _new_ button code
```{html}
<!-- no create button -->
<!--
<li><g:link class="create" action="create"><g:message
code="default.new.label" args="[entityName]" /></g:link></li>
-->
```
* Comment the `<f:table>` element and add the following code
```{html}
<!-- new arguments for f:table -->
<g:set var="collection" value="${mypackage.A.list(params)}" />
<g:set var="domainClass"
value="${grailsApplication.getDomainClass('mypackage.A')}" />
<g:set var="domainProperties"
value="${domainClass.getConstrainedProperties().keySet()}" />
<!-- old f:table -->
<!-- <f:table collection="${aList}" /> -->
<!-- new f:table -->
<f:table collection="${collection}" properties="${domainProperties}" />
```
With respect to `<f:table>` attributes:
* The `aList` variable originally passed to `<f:table>` is `null`.
* Do not include `domainClass="mypackage.A"`, it generates a
`java.lang.InstantiationException`.
* The `properties` attribute must be present and contain the list of
properties of the abstract class **only**. If it is not present, and objects of
classes `A` and `B` appear in the same pagination, a
`org.springframework.beans.NotReadablePropertyException` is thrown because
`<f:table>` tries to read properties of class `B` on objects of class `C` and
viceversa.
Apart from that, the `create` and `edit` pages are still available for `A`.
I have `generated-all` for `A` and tried to delete those methods from the
generated controller, but my knowledge of Grails is limited and I am having
lots of errors with that, so I went back to scaffolding (which is my main
reason for using Grails).
I think that Grails should not show empty lists for abstract classes in
scaffolding, but I do not know if that behaviour is inside the scaffolding
controller, the `<f:table>` tag, or harcoded in `_table.gsp`.
I also think that Grails should not generate `create` and `update` methods
in the scaffolding controllers for abstract classes.
--
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]