Database cache not being stored by Springboot

Asked

Viewed 106 times

1

I am trying to store the cache of a database search made through JPA, for that I am using the annotation @Cacheable of the package org.springframework.cache.annotation upon the method getPerfisPermitidos() that’s in my @Entity User

@Cacheable
public List<Perfil> getPerfisPermitidos() {
    return perfisPermitidos;
}

I also already put the note @EnableCaching within the initial Springboot class

//importações

@SpringBootApplication(
        scanBasePackages = "xx.xxx.xxxxx.xx", 
        exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }
)
@EnableCaching
public class Application extends SpringBootServletInitializer {
    //implementação
}

but when I perform the following search:

List<Perfil> perfisDoUsuario = usuarioRepository.findById(username).get().getPerfisPermitidos();
List<Perfil> perfisDoUsuario2 = usuarioRepository.findById(username).get().getPerfisPermitidos();
List<Perfil> perfisDoUsuario3 = usuarioRepository.findById(username).get().getPerfisPermitidos();

The Hibernate does 3 searches in the bank instead of only 1° and the other 2 are cached.

Hibernate: 
    select
        usuario0_.email as email1_14_0_,
        usuario0_.nome as nome2_14_0_,
        usuario0_.sobrenome as sobrenom3_14_0_,
        perfisperm1_.email as email2_15_1_,
        perfil2_.perfil as perfil1_15_1_,
        perfil2_.perfil as perfil1_9_2_,
        perfil2_.descricao as descrica2_9_2_ 
    from
        Usuario usuario0_ 
    left outer join
        UsuarioPerfil perfisperm1_ 
            on usuario0_.email=perfisperm1_.email 
    left outer join
        Perfil perfil2_ 
            on perfisperm1_.perfil=perfil2_.perfil 
    where
        usuario0_.email=?
Hibernate: 
    select
        usuario0_.email as email1_14_0_,
        usuario0_.nome as nome2_14_0_,
        usuario0_.sobrenome as sobrenom3_14_0_,
        perfisperm1_.email as email2_15_1_,
        perfil2_.perfil as perfil1_15_1_,
        perfil2_.perfil as perfil1_9_2_,
        perfil2_.descricao as descrica2_9_2_ 
    from
        Usuario usuario0_ 
    left outer join
        UsuarioPerfil perfisperm1_ 
            on usuario0_.email=perfisperm1_.email 
    left outer join
        Perfil perfil2_ 
            on perfisperm1_.perfil=perfil2_.perfil 
    where
        usuario0_.email=?
Hibernate: 
    select
        usuario0_.email as email1_14_0_,
        usuario0_.nome as nome2_14_0_,
        usuario0_.sobrenome as sobrenom3_14_0_,
        perfisperm1_.email as email2_15_1_,
        perfil2_.perfil as perfil1_15_1_,
        perfil2_.perfil as perfil1_9_2_,
        perfil2_.descricao as descrica2_9_2_ 
    from
        Usuario usuario0_ 
    left outer join
        UsuarioPerfil perfisperm1_ 
            on usuario0_.email=perfisperm1_.email 
    left outer join
        Perfil perfil2_ 
            on perfisperm1_.perfil=perfil2_.perfil 
    where
        usuario0_.email=?
  • 1

    In reality you are doing wrong, you are seeking usuario and not perfis. Your cache instruction should be made in usuarioRepository.findById and not in the user class getter. Anyway, if you don’t want to cache the entire user, you should customize your cache area by entity (using ehcache configs for example)

  • Hello @nullptr would you have any link to a possible implementation that I can rely on to follow in my project, or specific documentation? because the problem I notice is that my Usuariorepository inherits from an Interface called Baserepository, and this in turn inherits from Jparepository, there is also a Baserepositoryimpl that extends Simplejparepository and also implements Baserepository... What I mean by this is that it is not helpful to go in the Usuariorepository interface and put the @Cacheable because it is not caching the user.

  • 1

    The tutorial and documentation of Spring are very good, give a look here and here

  • 1

    managed to adjust its cache or still needs help?

  • yes it is working partially, it is already caching in the local environment, however it is giving error in deploy for Appengine. As soon as I can solve this bug I come back here to post the complete solution of the problem, alias your tip was precious, it was enough for me to rewrite the query in the class I inherited from Simplejparepository and he started caching.

  • 1

    Great :) I’m happy to help

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.