禁用Spring数据jpa中的第一级缓存

时间:2017-11-20 06:53:07

标签: hibernate jpa spring-boot spring-data-jpa

我的项目有两个应用程序。一个是API模块,另一个是后端管理。他们使用相同的数据库,我使用spring boot 1.3.7和数据jpa 1.9.4以及hibernate 4.3.11 .API是http restful端点,供用户登录和处理他们的业务。管理系统是维护系统的数据。当我通过管理UI插入新数据时,这是一个问题,API模块无法在大约5分钟后立即检索数据。一种情况是我在后端添加新用户,在API中,方法findByUsername不会得到我刚才添加的用户名。有什么想法解决这个问题吗?任何建议都将不胜感激!

API

package com.brahalla.Cerberus.service.impl;

import com.brahalla.Cerberus.domain.entity.User;
import com.brahalla.Cerberus.model.factory.CerberusUserFactory;
import com.brahalla.Cerberus.model.security.CerberusUser;
import com.brahalla.Cerberus.repository.UserRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

  @Autowired
  private UserRepository userRepository;

  /**
   * API :findByUsername
   */
  @Override
  public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = this.userRepository.findByUsername(username);

    if (user == null) {
      throw new UsernameNotFoundException(String.format("No user found with username '%s'.", username));
    } else {
      return CerberusUserFactory.create(user);
    }
  }

}

后端管理

package com.brahalla.Cerberus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.brahalla.Cerberus.domain.entity.User;
import com.brahalla.Cerberus.repository.UserRepository;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserRepository userRepository;

    /**
     * Backend add new user
     * 
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public String create(User user) {
        user.setId(null);
        userRepository.save(user);
        return "redirect:/";
    }
}

UserRepository

package com.brahalla.Cerberus.repository;

import com.brahalla.Cerberus.domain.entity.User;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {

  public User findByUsername(String username);

}

1 个答案:

答案 0 :(得分:0)

抱歉,第一级缓存无法禁用