Spring Data JPA XML repository configuration¶
Spring Data JPA XML repository configuration involves setting up the Spring Data JPA infrastructure using an XML application context, typically defining the EntityManagerFactory, TransactionManager, and repository scanning locations.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
XML Schema Definition¶
To enable Spring Data JPA features in XML, the appropriate namespace must be declared. This involves adding the jpa namespace and specifying the location of the schema definition (e.g., spring-jpa-1.3.xsd).^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
<jpa:repositories base-package="core.*.persistence , lott.*.persistence"
entity-manager-factory-ref="entityManagerFactory">
</jpa:repositories>
Configuration Steps¶
A typical XML configuration consists of several key beans:
1. Component Scanning¶
The context:component-scan element is used to auto-detect Spring components, such as annotated model classes.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
<context:component-scan base-package="core.*.model , lott.*.model"></context:component-scan>
2. Data Source¶
A DataSource bean is defined to provide database connectivity details (driver class, URL, username, password).^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
3. EntityManagerFactory¶
The LocalContainerEntityManagerFactoryBean is configured to manage the JPA EntityManager. Key properties include:
* DataSource reference: Links to the defined data source.
* LoadTimeWeaver: Configures instrumentation for JPA class transformation (e.g., InstrumentationLoadTimeWeaver).^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
* JPA Vendor Adapter: Specifies the implementation, such as HibernateJpaVendorAdapter, with settings like database platform (ORACLE), DDL generation, and SQL logging.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
* Packages to Scan: Specifies the packages containing @Entity classes (e.g., core.*, lott.*).^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
* JPA Properties: Defines Hibernate dialects, SQL formatting, and caching strategies.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
4. Transaction Management¶
A JpaTransactionManager is set up to handle transactions bound to the EntityManagerFactory.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md] Additionally, <tx:annotation-driven> enables the use of @Transactional annotations.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
5. Repository Scanning¶
The <jpa:repositories> element configures the repository infrastructure.
* base-package: Specifies the package(s) to scan for repository interfaces.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
* entity-manager-factory-ref: Links the repositories to the specific EntityManagerFactory bean.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
Custom Repository Beans¶
In addition to interfaces detected by <jpa:repositories>, standard Spring beans can be defined for custom repository implementations.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md] These custom beans often hold references to the EntityManager, either injected automatically by Spring or manually set via reflection in specific test scenarios.^[600-developer-java-application-server-weblogic-weblogic-spring-jpa.md]
Related Concepts¶
- Spring Data JPA
- [[JPA]]
- [[Hibernate]]
- [[Transactional]]