Query Cache Probrem in Hibernate 6.3.0.Final

In the following test code, we expect the values of the entity values to be restored from the query cache, but it seems that they are not being restored correctly.

test code

package test.querycache;

import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class QueryCacheTest {

    private SessionFactory sf;

    public void setup() {
        StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder()
                .applySetting("hibernate.show_sql", "true")
                .applySetting("hibernate.format_sql", "true")
                .applySetting("hibernate.hbm2ddl.auto", "update")
                .applySetting("hibernate.cache.use_second_level_cache", true)
                .applySetting("hibernate.cache.use_query_cache", true)
                .applySetting("hibernate.cache.region.factory_class", "jcache");

        Metadata metadata = new MetadataSources(srb.build())

        sf = metadata.buildSessionFactory();

    private static final String TEXT = "text";
    private static final String QUERY = "select E from TestEntity E where text=:text";

    public void ngTest() throws Exception {
        // create entity
        final var s1 = sf.openSession();
        final var tx1 = s1.beginTransaction();
        final var entity1 = new TestEntity();

        // save query cache from managed entity
        final var s2 = sf.openSession();
        s2.get(TestEntity.class, 1L);
        final var entity2 = s2.createQuery(QUERY, TestEntity.class)
                .setParameter("text", TEXT)
        Assert.assertEquals(TEXT, entity2.getText());

        // use query cache
        final var s3 = sf.openSession();
        final var entity3 = s3.createQuery(QUERY, TestEntity.class)
                .setParameter("text", TEXT)
        Assert.assertEquals(TEXT, entity3.getText()); // entity3.getText() is null !

test entity

package test.querycache;

import jakarta.persistence.Cacheable;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

public class TestEntity {

    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    public Long getId() {
        return id;

    public void setId(Long id) {
        this.id =  id;

    private String text;

    public String getText() {
        return text;

    public void setText(String text) {
        this.text = text;


Please create an issue in the issue tracker(https://hibernate.atlassian.net) with a test case(https://github.com/hibernate/hibernate-test-case-templates/blob/master/orm/hibernate-orm-6/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java) that reproduces the issue.

Thank you for your guidance.

I will now report this to the issue tracker with a test case.

Reported below.
I am afraid I am new to this, but I would appreciate it if you could point out any problems with the reporting process.


1 Like