为什么我们必须在Data

为什么我们必须在Data

本文介绍了为什么我们必须在Data Jpa中对查询使用@Modifying注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

例如我在我的CRUD界面中有一个从数据库中删除用户的方法:

for example I have a method in my CRUD interface which deletes a user from the database:

public interface CrudUserRepository extends JpaRepository<User, Integer> {

    @Transactional
    @Modifying
    @Query("DELETE FROM User u WHERE u.id=:id")
    int delete(@Param("id") int id, @Param("userId") int userId);
}

此方法仅适用于注释@Modifying。但这里的注释需要什么?为什么不能分析查询并理解它是一个修改查询?

This method will work only with the annotation @Modifying. But what is the need for the annotation here? Why cant spring analyze the query and understand that it is a modifying query?

推荐答案

这将触发注释给方法的查询为更新查询而不是选择一个。由于EntityManager在执行修改查询后可能包含过时的实体,因此我们会自动清除它(有关详细信息,请参阅EntityManager.clear()的JavaDoc)。这将有效地删除EntityManager中仍未处理的所有未刷新的更改。如果您不希望自动清除EntityManager,您可以将@Modifying注释的clearAutomatically属性设置为false;

This will trigger the query annotated to the method as updating query instead of a selecting one. As the EntityManager might contain outdated entities after the execution of the modifying query, we automatically clear it (see JavaDoc of EntityManager.clear() for details). This will effectively drop all non-flushed changes still pending in the EntityManager. If you don't wish the EntityManager to be cleared automatically you can set @Modifying annotation's clearAutomatically attribute to false;

以获取更多详细信息,您可以点击此链接: -

for further detail you can follow this link:-

这篇关于为什么我们必须在Data Jpa中对查询使用@Modifying注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:25