Using Hibernate with Ehcache and Jcache

Hello guys. I’m new to hibernate. The problem I have is dealing with second-level caching. I want to have "User" objects to be saved in cache. As I understand, in Hibernate 5.3.7 I should use "hibernate-jcache" and "ehcache" libraries. I madejcache.xml config, installed these libraries using gradle. But I have an issue: when using session.get(User.class, id) I see, that hibernate is dealing with database every time, so I suppose that caching does not work.
Thanks in advance
Here us the code of User.java

@Entity
@Cacheable
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "USER")
@Table (name = "users")
public class User {

    @Id
    @Column(name = "userId")
    private int userId;
public User() {}

    public User(int userId) {
        this.userId = userId;
    }
then getter and setter

Here is the code of my DAO method:

        Session session = SessionFactoryUtil.getSessionFactory().openSession();

        User user = session.get(User.class, 299332353);
        System.out.println(user.getLogin());
        session.close();

My build.gradle

    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.7.Final'
    compile group: 'org.hibernate', name: 'hibernate-jcache', version: '5.3.7.Final'
    compile group: 'org.ehcache', name: 'ehcache', version: '3.6.2'

Jcache.xml config :

<?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
            http://www.ehcache.org/v3/jsr107
            http://www.ehcache.org/schema/ehcache-107-ext.xsd">
    <service>
        <jsr107:defaults enable-management="true" enable-statistics="true" default-template="default" />
    </service>

    <cache alias="default-query-results-region">
        <expiry>
            <tti unit="seconds">300</tti>
        </expiry>
        <heap>1024</heap>
    </cache>

    <cache alias="default-update-timestamps-region">
        <expiry>
            <none />
        </expiry>
        <heap>4096</heap>
    </cache>

    <cache alias="USER" uses-template="default">
        <expiry>
            <tti unit="seconds">300</tti>
        </expiry>
        <heap>1024</heap>
    </cache>

    <cache-template name="default">
        <expiry>
            <tti unit="seconds">300</tti>
        </expiry>
        <heap>1024</heap>
    </cache-template>



</config>

And hibernate.cfg.xml

        <property name="hibernate.cache.use_query_cache">true</property>
        <property name="hibernate.cache.use_second_level_cache">true</property>

        <property name="hibernate.cache.region.factory_class">jcache</property>
        <property name="hibernate.javax.cache.provider">org.ehcache.jsr107.EhcacheCachingProvider</property>



        <property name="hibernate.javax.cache.uri">file:src/main/resources/jcache.xml</property>

And when getting user login the console SELECTs user every time. So I guess caching is not working. Am I right?

If you fork the Hibernate ORM project, you can run the JCacheTransactionalCacheConcurrencyStrategyTest, and see that it works just fine.

Try to debug it and see why it does not work for you and compare to the Hibernate test case above.

Thank you. And one more question. Are my settings set up correctrly?

The hibernate.javax.cache.uri might not be properly set. Try debugging Hibernate and see if the URI is properly resolved.

Hmm, if I make get Request from main method, everything works. I use telegrambots library to make a bot. And in some method I use get Request. So there is a problem. In Main method it works, in another class method it does not work. I will try to figure it out

Then, it’s not a Hibernate issue.

Everything works now, thanks anyway :slight_smile: