You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
By default, CRUD methods on repository instances are transactional. For read operations, the transaction configuration `readOnly` flag is set to `true`. All others are configured with a plain `@Transactional` so that default transaction configuration applies. For details, see JavaDoc of link:$$https://docs.spring.io/spring-data/data-jpa/docs/current/api/index.html?org/springframework/data/jpa/repository/support/SimpleJpaRepository.html$$[`SimpleJpaRepository`]. If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface, as follows:
872
+
By default, CRUD methods on repository instances inherited from link:$$https://docs.spring.io/spring-data/data-jpa/docs/current/api/org/springframework/data/jpa/repository/support/SimpleJpaRepository.html$$[`SimpleJpaRepository`] are transactional.
873
+
For read operations, the transaction configuration `readOnly` flag is set to `true`.
874
+
All others are configured with a plain `@Transactional` so that default transaction configuration applies.
875
+
Repository methods that are backed by transactional repository fragments inherit the transactional attributes from the actual fragment method.
876
+
877
+
If you need to tweak transaction configuration for one of the methods declared in a repository, redeclare the method in your repository interface, as follows:
873
878
874
879
.Custom transaction configuration for CRUD
875
880
====
@@ -894,12 +899,11 @@ Another way to alter transactional behaviour is to use a facade or service imple
894
899
[source, java]
895
900
----
896
901
@Service
897
-
class UserManagementImpl implements UserManagement {
902
+
public class UserManagementImpl implements UserManagement {
898
903
899
904
private final UserRepository userRepository;
900
905
private final RoleRepository roleRepository;
901
906
902
-
@Autowired
903
907
public UserManagementImpl(UserRepository userRepository,
904
908
RoleRepository roleRepository) {
905
909
this.userRepository = userRepository;
@@ -915,6 +919,7 @@ class UserManagementImpl implements UserManagement {
915
919
user.addRole(role);
916
920
userRepository.save(user);
917
921
}
922
+
}
918
923
}
919
924
----
920
925
This example causes call to `addRoleToAllUsers(…)` to run inside a transaction (participating in an existing one or creating a new one if none are already running). The transaction configuration at the repositories is then neglected, as the outer transaction configuration determines the actual one used. Note that you must activate `<tx:annotation-driven />` or use `@EnableTransactionManagement` explicitly to get annotation-based configuration of facades to work.
@@ -925,14 +930,15 @@ Note that the call to `save` is not strictly necessary from a JPA point of view,
925
930
926
931
[[transactional-query-methods]]
927
932
=== Transactional query methods
933
+
928
934
To let your query methods be transactional, use `@Transactional` at the repository interface you define, as shown in the following example:
929
935
930
936
.Using @Transactional at query methods
931
937
====
932
938
[source, java]
933
939
----
934
940
@Transactional(readOnly = true)
935
-
public interface UserRepository extends JpaRepository<User, Long> {
0 commit comments