ClassCastException on JPA Entity after re-deploying provider JAR

Running Keycloak 4.5.0.Final.

I’ve created a JPA provider with a single JPA entity class and some additional classes to implement a REST endpoint. Everything is in a single JAR file which is deployed to the server by copying the JAR to the …/standalone/deployments directory. After the server starts, everything works fine. However, if I make a change, rebuild the JAR, and re-deploy it, I’ll get a ClassCastException which basically says that FooEntity cannot be cast to FooEntity. If I re-start the server, everything is fine again.

I recognize this as a classloader issue and I’ve seen similar issues reported against WildFly, but without a definitive solution. One classloader must still have a reference to FooEntity from the previous deployment, but another classloader is loading it after the re-deployment. My JAR is a “fat JAR” containing my own provider’s classes and some org.json.* classes.

My jboss-deployment-structure.xml file is shown below. Any thoughts on how to fix this problem?

jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <dependencies>
            <module name="org.keycloak.keycloak-server-spi-private"/>
            <module name="org.keycloak.keycloak-services"/>
            <module name="org.keycloak.keycloak-model-jpa"/>
            <module name="org.hibernate"/>
        </dependencies>
    </deployment>
</jboss-deployment-structure>