Skip to content

Commit 36c4d1d

Browse files
committed
Improve wording on transactional methods inherited from SimpleJpaRepository and repository fragments.
Closes #2207
1 parent 59605a3 commit 36c4d1d

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/main/asciidoc/jpa.adoc

+10-4
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,12 @@ include::query-by-example.adoc[leveloffset=+1]
869869
[[transactions]]
870870
== Transactionality
871871

872-
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:
873878

874879
.Custom transaction configuration for CRUD
875880
====
@@ -894,12 +899,11 @@ Another way to alter transactional behaviour is to use a facade or service imple
894899
[source, java]
895900
----
896901
@Service
897-
class UserManagementImpl implements UserManagement {
902+
public class UserManagementImpl implements UserManagement {
898903
899904
private final UserRepository userRepository;
900905
private final RoleRepository roleRepository;
901906
902-
@Autowired
903907
public UserManagementImpl(UserRepository userRepository,
904908
RoleRepository roleRepository) {
905909
this.userRepository = userRepository;
@@ -915,6 +919,7 @@ class UserManagementImpl implements UserManagement {
915919
user.addRole(role);
916920
userRepository.save(user);
917921
}
922+
}
918923
}
919924
----
920925
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,
925930

926931
[[transactional-query-methods]]
927932
=== Transactional query methods
933+
928934
To let your query methods be transactional, use `@Transactional` at the repository interface you define, as shown in the following example:
929935

930936
.Using @Transactional at query methods
931937
====
932938
[source, java]
933939
----
934940
@Transactional(readOnly = true)
935-
public interface UserRepository extends JpaRepository<User, Long> {
941+
interface UserRepository extends JpaRepository<User, Long> {
936942
937943
List<User> findByLastname(String lastname);
938944

0 commit comments

Comments
 (0)