Why Spring Boot causes Ehcache to throw ClassCastException when declaring Key or Value in configuration?

142 views Asked by At

In a Spring Boot 3 application I implemented Ehcache for Hibernate 6. As documentated here and at tons of websites elsewhere too, I've set up a simple cache configuration for a simple class (no further realtionships, no whats'o'ever tricky somewhat):

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.ehcache.org/v3"
        xmlns:jsr107="http://www.ehcache.org/v3/jsr107"
        xsi:schemaLocation="http://www.ehcache.org/v3
                            http://www.ehcache.org/schema/ehcache-core.xsd">

    <cache-template name="default">
        <expiry>
            <ttl unit="seconds">600</ttl>
        </expiry>

        <resources>
            <!-- Only the use of 'unit' is deprecated, not the property itself. -->
            <heap>1000</heap>
            <offheap unit="MB">10</offheap>
        </resources>
    </cache-template>

    <cache alias="email-status-cache" uses-template="default">
        <key-type>java.lang.Integer</key-type>
        <value-type>com.example.tryout.EmailStatus</value-type>
    </cache>
</config>

The entity is annotated as follows:

@Entity
@Access(AccessType.FIELD)
@org.hibernate.annotations.Cache(region = "email-status-cache", usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "email_status")
public class EmailStatus implements Serializable {

    //...
}

Now, doing it this way, the start up of the application fails with a ClassCastException:

java.lang.ClassCastException: Invalid key type, expected : java.lang.Integer but was : org.hibernate.cache.internal.BasicCacheKeyImplementation

Removing the key-type while the value-type remains throws another ClassCastException:

java.lang.ClassCastException: Invalid value type, expected : com.example.tryout.EmailStatus but was : org.hibernate.cache.spi.entry.StandardCacheEntryImpl

Only when I abstain from declaring any types within the configuration file the application is willing to run.

Since all documatations, which I've seen, declare theses properties, I'm not quite sure, whether I'm missing something or this is the correct way. Unfortunately, there is not a single documentation/example to find which is up to date, so this may be a thing only regarding the current tech stack - but I'm not that expert to declare this behavior normal.

Can anybody, who is familiar with Spring Boot 3, Hibernate 6 and Ehcache 3 explain what the proper configuration is and why?

0

There are 0 answers