Skip to content

Commit cdcf88f

Browse files
committed
Improve reference documentation.
Closes #2647.
1 parent 2266350 commit cdcf88f

File tree

3 files changed

+60
-74
lines changed

3 files changed

+60
-74
lines changed

src/main/asciidoc/faq.adoc

-16
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,6 @@ I'd like to get more detailed logging information on what methods are called ins
2222
</aop:config>
2323
----
2424

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`
32-
====
33-
[source, xml]
34-
----
35-
<bean id="sessionFactory" class="org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean">
36-
<property name="entityManagerFactory" ref="entityManagerFactory"/>
37-
</bean>
38-
----
39-
====
40-
4125
[[faq.auditing]]
4226
== Auditing
4327

src/main/asciidoc/glossary.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ Hibernate :: Object relational mapper implementing JPA - link:$$https://hibernat
1818

1919
JPA :: Jakarta Persistence API
2020

21-
Spring :: Java application framework - link:$$https://projects.spring.io/spring-framework$$[https://projects.spring.io/spring-framework]
21+
Spring :: Java application framework - link:$$https://spring.io/projects/spring-framework$$[https://spring.io/projects/spring-framework]

src/main/asciidoc/jpa.adoc

+59-57
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,9 @@ This section describes the basics of configuring Spring Data JPA through either:
1111
* "`<<jpa.namespace>>`" (XML configuration)
1212
* "`<<jpa.java-config>>`" (Java configuration)
1313

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
20-
====
21-
[source, xml]
22-
----
23-
<?xml version="1.0" encoding="UTF-8"?>
24-
<beans xmlns="http://www.springframework.org/schema/beans"
25-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26-
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
27-
xsi:schemaLocation="http://www.springframework.org/schema/beans
28-
https://www.springframework.org/schema/beans/spring-beans.xsd
29-
http://www.springframework.org/schema/data/jpa
30-
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
31-
32-
<jpa:repositories base-package="com.acme.repositories" />
33-
34-
</beans>
35-
----
36-
====
37-
38-
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-
5314
[[jpa.java-config]]
5415
=== 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:
5617

5718
.Spring Data JPA repositories using JavaConfig
5819
====
@@ -97,6 +58,47 @@ NOTE: You must create `LocalContainerEntityManagerFactoryBean` and not `EntityMa
9758

9859
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.
9960

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
67+
====
68+
[source, xml]
69+
----
70+
<?xml version="1.0" encoding="UTF-8"?>
71+
<beans xmlns="http://www.springframework.org/schema/beans"
72+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
73+
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
74+
xsi:schemaLocation="http://www.springframework.org/schema/beans
75+
https://www.springframework.org/schema/beans/spring-beans.xsd
76+
http://www.springframework.org/schema/data/jpa
77+
https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
78+
79+
<jpa:repositories base-package="com.acme.repositories" />
80+
81+
</beans>
82+
----
83+
====
84+
85+
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+
100102
[[jpa.bootstrap-mode]]
101103
=== Bootstrap Mode
102104

@@ -274,6 +276,23 @@ Using `distinct` sometimes requires writing the query by hand and using `@Query`
274276
to capture the result set.
275277
====
276278

279+
[[jpa.query-methods.named-queries.annotation-based-configuration]]
280+
==== Annotation-based Configuration
281+
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+
277296
[[jpa.query-methods.named-queries]]
278297
=== Using JPA Named Queries
279298

@@ -295,23 +314,6 @@ To use XML configuration, add the necessary `<named-query />` element to the `or
295314

296315
The query has a special name that is used to resolve it at runtime.
297316

298-
[[jpa.query-methods.named-queries.annotation-based-configuration]]
299-
==== Annotation-based Configuration
300-
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")
309-
public class User {
310-
311-
}
312-
----
313-
====
314-
315317
[[jpa.query-methods.named-queries.declaring-interfaces]]
316318
==== Declaring Interfaces
317319
To allow these named queries, specify the `UserRepositoryWithRewriter` as follows:

0 commit comments

Comments
 (0)