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
Copy file name to clipboardExpand all lines: src/main/asciidoc/faq.adoc
-16
Original file line number
Diff line number
Diff line change
@@ -22,22 +22,6 @@ I'd like to get more detailed logging information on what methods are called ins
22
22
</aop:config>
23
23
----
24
24
25
-
[[faq.infrastructure]]
26
-
== Infrastructure
27
-
28
-
[qanda]
29
-
Currently I have implemented a repository layer based on `HibernateDaoSupport`. I create a `SessionFactory` by using Spring's `AnnotationSessionFactoryBean`. How do I get Spring Data repositories working in this environment? :: You have to replace `AnnotationSessionFactoryBean` with the `HibernateJpaSessionFactoryBean`, as follows:
30
-
+
31
-
.Looking up a `SessionFactory` from a `HibernateEntityManagerFactory`
Copy file name to clipboardExpand all lines: src/main/asciidoc/jpa.adoc
+59-57
Original file line number
Diff line number
Diff line change
@@ -11,48 +11,9 @@ This section describes the basics of configuring Spring Data JPA through either:
11
11
* "`<<jpa.namespace>>`" (XML configuration)
12
12
* "`<<jpa.java-config>>`" (Java configuration)
13
13
14
-
[[jpa.namespace]]
15
-
=== Spring Namespace
16
-
17
-
The JPA module of Spring Data contains a custom namespace that allows defining repository beans. It also contains certain features and element attributes that are special to JPA. Generally, the JPA repositories can be set up by using the `repositories` element, as shown in the following example:
18
-
19
-
.Setting up JPA repositories by using the namespace
Using the `repositories` element looks up Spring Data repositories as described in "`<<repositories.create-instances>>`". Beyond that, it activates persistence exception translation for all beans annotated with `@Repository`, to let exceptions being thrown by the JPA persistence providers be converted into Spring's `DataAccessException` hierarchy.
39
-
40
-
[[jpa.namespace.custom-namespace-attributes]]
41
-
==== Custom Namespace Attributes
42
-
Beyond the default attributes of the `repositories` element, the JPA namespace offers additional attributes to let you gain more detailed control over the setup of the repositories:
43
-
44
-
.Custom JPA-specific attributes of the `repositories` element
45
-
[options = "autowidth"]
46
-
|===============
47
-
|`entity-manager-factory-ref`|Explicitly wire the `EntityManagerFactory` to be used with the repositories being detected by the `repositories` element. Usually used if multiple `EntityManagerFactory` beans are used within the application. If not configured, Spring Data automatically looks up the `EntityManagerFactory` bean with the name `entityManagerFactory` in the `ApplicationContext`.
48
-
|`transaction-manager-ref`|Explicitly wire the `PlatformTransactionManager` to be used with the repositories being detected by the `repositories` element. Usually only necessary if multiple transaction managers or `EntityManagerFactory` beans have been configured. Default to a single defined `PlatformTransactionManager` inside the current `ApplicationContext`.
49
-
|===============
50
-
51
-
NOTE: Spring Data JPA requires a `PlatformTransactionManager` bean named `transactionManager` to be present if no explicit `transaction-manager-ref` is defined.
52
-
53
14
[[jpa.java-config]]
54
15
=== Annotation-based Configuration
55
-
The Spring Data JPA repositories support can be activated not only through an XML namespace but also by using an annotation through JavaConfig, as shown in the following example:
16
+
The Spring Data JPA repositories support can be activated through both JavaConfig as well as a custom XML namespace, as shown in the following example:
56
17
57
18
.Spring Data JPA repositories using JavaConfig
58
19
====
@@ -97,6 +58,47 @@ NOTE: You must create `LocalContainerEntityManagerFactoryBean` and not `EntityMa
97
58
98
59
The preceding configuration class sets up an embedded HSQL database by using the `EmbeddedDatabaseBuilder` API of `spring-jdbc`. Spring Data then sets up an `EntityManagerFactory` and uses Hibernate as the sample persistence provider. The last infrastructure component declared here is the `JpaTransactionManager`. Finally, the example activates Spring Data JPA repositories by using the `@EnableJpaRepositories` annotation, which essentially carries the same attributes as the XML namespace. If no base package is configured, it uses the one in which the configuration class resides.
99
60
61
+
[[jpa.namespace]]
62
+
=== Spring Namespace
63
+
64
+
The JPA module of Spring Data contains a custom namespace that allows defining repository beans. It also contains certain features and element attributes that are special to JPA. Generally, the JPA repositories can be set up by using the `repositories` element, as shown in the following example:
65
+
66
+
.Setting up JPA repositories by using the namespace
TIP: Which is better, JavaConfig or XML? XML is how Spring was configured long ago. In today's era of fast-growing Java, record types, annotations, and more, new projects typically use as much pure Java as possible. While there is no immediate plan to remove XML support, some of the newest features MAY not be available through XML.
86
+
87
+
Using the `repositories` element looks up Spring Data repositories as described in "`<<repositories.create-instances>>`". Beyond that, it activates persistence exception translation for all beans annotated with `@Repository`, to let exceptions being thrown by the JPA persistence providers be converted into Spring's `DataAccessException` hierarchy.
88
+
89
+
[[jpa.namespace.custom-namespace-attributes]]
90
+
==== Custom Namespace Attributes
91
+
Beyond the default attributes of the `repositories` element, the JPA namespace offers additional attributes to let you gain more detailed control over the setup of the repositories:
92
+
93
+
.Custom JPA-specific attributes of the `repositories` element
94
+
[options = "autowidth"]
95
+
|===============
96
+
|`entity-manager-factory-ref`|Explicitly wire the `EntityManagerFactory` to be used with the repositories being detected by the `repositories` element. Usually used if multiple `EntityManagerFactory` beans are used within the application. If not configured, Spring Data automatically looks up the `EntityManagerFactory` bean with the name `entityManagerFactory` in the `ApplicationContext`.
97
+
|`transaction-manager-ref`|Explicitly wire the `PlatformTransactionManager` to be used with the repositories being detected by the `repositories` element. Usually only necessary if multiple transaction managers or `EntityManagerFactory` beans have been configured. Default to a single defined `PlatformTransactionManager` inside the current `ApplicationContext`.
98
+
|===============
99
+
100
+
NOTE: Spring Data JPA requires a `PlatformTransactionManager` bean named `transactionManager` to be present if no explicit `transaction-manager-ref` is defined.
101
+
100
102
[[jpa.bootstrap-mode]]
101
103
=== Bootstrap Mode
102
104
@@ -274,6 +276,23 @@ Using `distinct` sometimes requires writing the query by hand and using `@Query`
Annotation-based configuration has the advantage of not needing another configuration file to be edited, lowering maintenance effort. You pay for that benefit by the need to recompile your domain class for every new query declaration.
282
+
283
+
.Annotation-based named query configuration
284
+
====
285
+
[source, java]
286
+
----
287
+
@Entity
288
+
@NamedQuery(name = "User.findByEmailAddress",
289
+
query = "select u from User u where u.emailAddress = ?1")
290
+
public class User {
291
+
292
+
}
293
+
----
294
+
====
295
+
277
296
[[jpa.query-methods.named-queries]]
278
297
=== Using JPA Named Queries
279
298
@@ -295,23 +314,6 @@ To use XML configuration, add the necessary `<named-query />` element to the `or
295
314
296
315
The query has a special name that is used to resolve it at runtime.
Annotation-based configuration has the advantage of not needing another configuration file to be edited, lowering maintenance effort. You pay for that benefit by the need to recompile your domain class for every new query declaration.
301
-
302
-
.Annotation-based named query configuration
303
-
====
304
-
[source, java]
305
-
----
306
-
@Entity
307
-
@NamedQuery(name = "User.findByEmailAddress",
308
-
query = "select u from User u where u.emailAddress = ?1")
0 commit comments