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

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

    @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);

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="" xmlns="" xmlns:jsr107=""
        <jsr107:defaults enable-management="true" enable-statistics="true" default-template="default" />

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

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

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

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


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: