Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClassCastException with domain objects in hierarchy #1499

Closed
4 tasks done
miq opened this issue Jul 21, 2021 · 1 comment
Closed
4 tasks done

ClassCastException with domain objects in hierarchy #1499

miq opened this issue Jul 21, 2021 · 1 comment

Comments

@miq
Copy link

miq commented Jul 21, 2021

Task List

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

Steps to Reproduce

  1. Create 3 Domain classes, where one is a subclass of another and the third (container) contains a reference to the superclass
  2. Create an instance of the containing class, assign an instance of the subclass and save it
  3. Try to access a property of the referenced superclass in the loaded container instance

Expected Behaviour

The value of the property should be returned. In OO the subclass can be used everywhere the superclass is expected which is not the case here.

Actual Behaviour

A ClassCastException is thrown:
Caused by ClassCastException: four.zero.gorm.SuperChild$HibernateProxy$dMa7Faoo cannot be cast to four.zero.gorm.SubChild
->> 10 | doCall in ContainerController.groovy


| 211 | invoke in org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker
| 188 | invoke . . . . . in org.grails.core.DefaultGrailsControllerClass
| 90 | handle in org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter
| 1040 | doDispatch . . . in org.springframework.web.servlet.DispatcherServlet
| 943 | doService in ''
| 1006 | processRequest . in org.springframework.web.servlet.FrameworkServlet
| 898 | doGet in ''
| 883 | service . . . . in ''
| 77 | doFilterInternal in org.grails.web.servlet.mvc.GrailsWebRequestFilter
| 67 | doFilterInternal in org.grails.web.filters.HiddenHttpMethodFilter
| 1149 | runWorker in java.util.concurrent.ThreadPoolExecutor
| 624 | run . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker
^ 748 | run in java.lang.Thread

Environment Information

  • Operating System: Windows 10
  • Grails Version: 4.0.9
  • JDK Version: 1.8.0_252

Example Application

https://github.com/miq/grails-class-cast

Start the app and just invoke /container/saveSub to see the exception. Using /container/saveSuper you can see that it works as expected if the referenced entity is of the super class type.

@puneetbehl
Copy link
Contributor

It seems to be a duplicate of #1468

@puneetbehl puneetbehl transferred this issue from grails/grails-core Jul 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants