Hibernate 4 second level cache hibernate is not working with association (EhCache)

Bilal BBB

I'm new to hibernate and I'm trying the second level cache.

My entities are Team : OneToMany : Player

The team entity :

@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Team {

    ...

    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    @OneToMany(fetch=FetchType.LAZY, mappedBy="team")
    @Cascade(CascadeType.ALL)
    private List<Player> players; 

    ...
}

the Player entity :

@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Player extends Person {

    ... 

    @Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
    @ManyToOne(fetch=FetchType.LAZY)
    @Cascade(CascadeType.SAVE_UPDATE)
    private Team team;

    ....    
}

My TeamDao is :

    @Repository
    public class TeamDao {

        @Autowired
        private SessionFactory sessionFactory;

        ...

        @Transactional
        public Team getTeam(Integer id) {
            return (Team) sessionFactory.getCurrentSession().createQuery("from team t inner join fetch t.players p where t.id = :tid")
                  .setParameter("tid", id)
                  .setCacheable(true)
                  .uniqueResult();

        }

        ...
    }

When i load a team for example id=1, I find it in the second level cache. But I don't find the players list of the team loaded even if the association is declared cacheable. session.getCache().contains(Team.class, 1); returns true but session.getCache().contains(Player.class, 1) returns false

When I try to get a player of the loaded team,

Player player = playerDao.getPlayer(1); 

Hibernate generates a select to get the Player even if The Team loaded contains this player

Player player = team.getPlayer().get(0);  

Is there a probleme in my entities annotations ? I don't know why it doesn't work for me.

Thanks for helping

Bilal BBB

The solution is to configure the query cache, using spring xml configuration :

<prop key="hibernate.cache.use_query_cache">true</prop>

because the HQL request createQuery(query) doesn't add the result to the second cache level, not like hibernate methods such get, load, save, update, ...

The use of setCacheable(true) in the HQL query tells hibernate to put the result in the query cache.

The only remaining little problem is that it is not working for a collection of teams (It works for just one Team).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Ehcache second level cache not working with JPA and Hibernate?

From Dev

Second Level Cache never hits using, spring3, hibernate4, ehcache?

From Dev

Collections not read from hibernate/ehcache second-level-cache

From Dev

Hibernate + ehcache second level cache miss in simple example

From Dev

Unable to configure second level cache of hibernate-ehcache-4.3.5.Final.jar in Hibernate 5.1.0.Final

From Dev

Hibernate second level cache example

From Dev

Hibernate : Invalidating Second Level cache

From Dev

infinispan as second level cache hibernate

From Dev

Hazelcast Hibernate second-level cache configuration

From Dev

How to enable second level cache in Hibernate

From Dev

dirty reads of associations in hibernate second level cache

From Dev

How hibernate second level cache works internally?

From Dev

Entity update using hibernate second level cache

From Dev

Configuring Infinispan as remote second level cache for Hibernate

From Dev

hibernate second level cache with Redis -will it improve performance?

From Dev

How hibernate second level cache works internally?

From Dev

Is second level cache is Session specific in Hibernate

From Dev

When to use second level cache in hibernate

From Dev

How to enable second level cache in Hibernate 5.2.2?

From Dev

EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

From Dev

With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

From Dev

With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

From Dev

How hibernate ensures second level cache is updated with latest data in database

From Dev

Spring Boot + JPA2 + Hibernate - enable second level cache

From Dev

Hibernate: Is it possible to save a transient field in second level cache?

From Dev

Will HQL query use Hibernate second-level cache

From Dev

Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region

From Dev

Hibernate: Is it possible to save a transient field in second level cache?

From Dev

Hibernate Second-Level Cache, Shared between different JVM

Related Related

  1. 1

    Ehcache second level cache not working with JPA and Hibernate?

  2. 2

    Second Level Cache never hits using, spring3, hibernate4, ehcache?

  3. 3

    Collections not read from hibernate/ehcache second-level-cache

  4. 4

    Hibernate + ehcache second level cache miss in simple example

  5. 5

    Unable to configure second level cache of hibernate-ehcache-4.3.5.Final.jar in Hibernate 5.1.0.Final

  6. 6

    Hibernate second level cache example

  7. 7

    Hibernate : Invalidating Second Level cache

  8. 8

    infinispan as second level cache hibernate

  9. 9

    Hazelcast Hibernate second-level cache configuration

  10. 10

    How to enable second level cache in Hibernate

  11. 11

    dirty reads of associations in hibernate second level cache

  12. 12

    How hibernate second level cache works internally?

  13. 13

    Entity update using hibernate second level cache

  14. 14

    Configuring Infinispan as remote second level cache for Hibernate

  15. 15

    hibernate second level cache with Redis -will it improve performance?

  16. 16

    How hibernate second level cache works internally?

  17. 17

    Is second level cache is Session specific in Hibernate

  18. 18

    When to use second level cache in hibernate

  19. 19

    How to enable second level cache in Hibernate 5.2.2?

  20. 20

    EhCache Hibernate 2nd level cache maxBytesLocalHeap slow

  21. 21

    With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

  22. 22

    With Sping's cache support (for ehcache), do we still need Hibernate 2nd level cache?

  23. 23

    How hibernate ensures second level cache is updated with latest data in database

  24. 24

    Spring Boot + JPA2 + Hibernate - enable second level cache

  25. 25

    Hibernate: Is it possible to save a transient field in second level cache?

  26. 26

    Will HQL query use Hibernate second-level cache

  27. 27

    Mapping two CacheConcurrencyStrategy for the same Hibernate second-level cache region

  28. 28

    Hibernate: Is it possible to save a transient field in second level cache?

  29. 29

    Hibernate Second-Level Cache, Shared between different JVM

HotTag

Archive