Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringBoot-html表显示过期数据_Java_Spring_Spring Boot_Transactions - Fatal编程技术网

Java SpringBoot-html表显示过期数据

Java SpringBoot-html表显示过期数据,java,spring,spring-boot,transactions,Java,Spring,Spring Boot,Transactions,我有SpringBoot项目,我正在使用JPA。 我的一个控制器返回一个页面“myclass.html”,上面有一个表 @Secured("ROLE_USER") @Controller public class UserController { @RequestMapping("/myClass") public String myClass(Model model) { model.addAttribute("students", studentService.findCu

我有SpringBoot项目,我正在使用JPA。 我的一个控制器返回一个页面“myclass.html”,上面有一个表

@Secured("ROLE_USER")
@Controller
public class UserController {

  @RequestMapping("/myClass")
  public String myClass(Model model) {

   model.addAttribute("students", studentService.findCurrentStudents());
   return "user/myclass";
  }

}
studentService做了两件事(通过在UserRepository上调用两个单独的方法):

  • 它会更新STUDENTS表,根据实际日期进行一些更改
  • 它返回学生表
甚至更新是在前面定义的,然后列表返回,当我测试它时,表包含旧数据(更新之前的数据),如果我刷新窗口,在刷新之后,它将最终显示修改后的数据。 我是否需要将它们封装到单个事务中以保持执行顺序?如果我必须这样做,我怎么能用最简单的方法来做呢

我复制上述服务的实际部分:

    @Override
    public List<Student> findCurrentStudents() {

        studentRepository.updateByUserAndDate(currentUserId(), new Date());
        return studentRepository.findCurrentStudentsByUser(currentUserId());

    }
@覆盖
公共列表findCurrentStudents(){
updateByUserAndDate(currentUserId(),new Date());
return studentRepository.findCurrentStudentsByUser(currentUserId());
}
以及存储库:

@Repository
public interface StudentRepository extends CrudRepository<Student, Long> {

@Query(value="SELECT * FROM Students WHERE user_id = :currentUserId, nativeQuery = true)
List<Student> findCurrentStudentsByUser(@Param("userId") Integer currentUserId);

@Transactional
@Modifying
@Query(value="UPDATE Students SET last_review = :newDate WHERE user_id = :userId", nativeQuery = true)
void updateByUserAndDate(@Param("userId")Long id, @Param("newDate") Date date);
@存储库
公共界面StudentRepository扩展了crudepository{
@查询(value=“SELECT*FROM Students,其中user\u id=:currentUserId,nativeQuery=true)
列出FindCurrentStudentsByser(@Param(“userId”)整数currentUserId);
@交易的
@修改
@查询(value=“updatestudents SET last\u review=:newDate其中user\u id=:userId”,nativeQuery=true)
void updateByUserAndDate(@Param(“userId”)长id,@Param(“newDate”)Date;

是否需要将它们封装到单个事务中:是:与数据库的每次交互都应该在一个事务中完成。使用
@Transactional
注释您的服务或控制器。不知道存储库的代码无助于解释问题。我已添加了存储库的代码。不幸的是@事务性注释并没有解决这个问题(我在顶部的服务实现中添加了注释),但希望存储库的代码能够向您解释。您是否尝试过在浏览器的开发人员工具中禁用缓存?是的,很遗憾,我已经禁用了缓存。