diff --git a/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc b/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc index 8193305116b..611009b73f4 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/classpath-scanning.adoc @@ -674,9 +674,7 @@ By default, the `AnnotationBeanNameGenerator` is used. For Spring xref:core/beans/classpath-scanning.adoc#beans-stereotype-annotations[stereotype annotations], if you supply a name via the annotation's `value` attribute that name will be used as the name in the corresponding bean definition. This convention also applies when the -following JSR-250 and JSR-330 annotations are used instead of Spring stereotype -annotations: `@jakarta.annotation.ManagedBean`, `@javax.annotation.ManagedBean`, -`@jakarta.inject.Named`, and `@javax.inject.Named`. +`@jakarta.inject.Named` annotation is used instead of Spring stereotype annotations. As of Spring Framework 6.1, the name of the annotation attribute that is used to specify the bean name is no longer required to be `value`. Custom stereotype annotations can diff --git a/framework-docs/modules/ROOT/pages/integration/email.adoc b/framework-docs/modules/ROOT/pages/integration/email.adoc index 46493a7de9a..420bc481c9d 100644 --- a/framework-docs/modules/ROOT/pages/integration/email.adoc +++ b/framework-docs/modules/ROOT/pages/integration/email.adoc @@ -11,9 +11,7 @@ Spring Framework's email support: * The https://jakartaee.github.io/mail-api/[Jakarta Mail] library This library is freely available on the web -- for example, in Maven Central as -`com.sun.mail:jakarta.mail`. Please make sure to use the latest 2.x version (which uses -the `jakarta.mail` package namespace) rather than Jakarta Mail 1.6.x (which uses the -`javax.mail` package namespace). +`org.eclipse.angus:angus-mail`. **** The Spring Framework provides a helpful utility library for sending email that shields diff --git a/framework-docs/modules/ROOT/pages/testing/annotations/integration-junit-jupiter.adoc b/framework-docs/modules/ROOT/pages/testing/annotations/integration-junit-jupiter.adoc index d359c708ea7..41b16558e1a 100644 --- a/framework-docs/modules/ROOT/pages/testing/annotations/integration-junit-jupiter.adoc +++ b/framework-docs/modules/ROOT/pages/testing/annotations/integration-junit-jupiter.adoc @@ -171,9 +171,9 @@ the parameters of a test class constructor are autowired from components in the If `@TestConstructor` is not present or meta-present on a test class, the default _test constructor autowire mode_ will be used. See the tip below for details on how to change -the default mode. Note, however, that a local declaration of `@Autowired`, -`@jakarta.inject.Inject`, or `@javax.inject.Inject` on a constructor takes precedence -over both `@TestConstructor` and the default mode. +the default mode. Note, however, that a local declaration of `@Autowired` or +`@jakarta.inject.Inject` on a constructor takes precedence over both `@TestConstructor` +and the default mode. .Changing the default test constructor autowire mode [TIP] diff --git a/framework-platform/framework-platform.gradle b/framework-platform/framework-platform.gradle index 804c6fa58ab..25979bcdf7c 100644 --- a/framework-platform/framework-platform.gradle +++ b/framework-platform/framework-platform.gradle @@ -40,7 +40,6 @@ dependencies { api("com.squareup.okhttp3:mockwebserver:3.14.9") api("com.squareup.okhttp3:okhttp:3.14.9") api("com.sun.activation:jakarta.activation:2.0.1") - api("com.sun.mail:jakarta.mail:2.0.1") api("com.sun.xml.bind:jaxb-core:3.0.2") api("com.sun.xml.bind:jaxb-impl:3.0.2") api("com.sun.xml.bind:jaxb-xjc:3.0.2") @@ -61,32 +60,30 @@ dependencies { api("io.undertow:undertow-servlet:2.3.18.Final") api("io.undertow:undertow-websockets-jsr:2.3.18.Final") api("io.vavr:vavr:0.10.4") - api("jakarta.activation:jakarta.activation-api:2.0.1") - api("jakarta.annotation:jakarta.annotation-api:2.0.0") + api("jakarta.activation:jakarta.activation-api:2.1.3") + api("jakarta.annotation:jakarta.annotation-api:3.0.0") api("jakarta.ejb:jakarta.ejb-api:4.0.1") - api("jakarta.el:jakarta.el-api:4.0.0") - api("jakarta.enterprise.concurrent:jakarta.enterprise.concurrent-api:2.0.0") - api("jakarta.faces:jakarta.faces-api:3.0.0") + api("jakarta.el:jakarta.el-api:6.0.1") + api("jakarta.enterprise.concurrent:jakarta.enterprise.concurrent-api:3.1.1") + api("jakarta.faces:jakarta.faces-api:4.1.2") api("jakarta.inject:jakarta.inject-api:2.0.1") api("jakarta.inject:jakarta.inject-tck:2.0.1") - api("jakarta.interceptor:jakarta.interceptor-api:2.0.0") - api("jakarta.jms:jakarta.jms-api:3.0.0") - api("jakarta.json.bind:jakarta.json.bind-api:2.0.0") - api("jakarta.json:jakarta.json-api:2.0.1") - api("jakarta.mail:jakarta.mail-api:2.0.1") - api("jakarta.persistence:jakarta.persistence-api:3.0.0") - api("jakarta.resource:jakarta.resource-api:2.0.0") + api("jakarta.interceptor:jakarta.interceptor-api:2.2.0") + api("jakarta.jms:jakarta.jms-api:3.1.0") + api("jakarta.json.bind:jakarta.json.bind-api:3.0.1") + api("jakarta.json:jakarta.json-api:2.1.3") + api("jakarta.mail:jakarta.mail-api:2.1.3") + api("jakarta.persistence:jakarta.persistence-api:3.2.0") + api("jakarta.resource:jakarta.resource-api:2.1.0") api("jakarta.servlet.jsp.jstl:jakarta.servlet.jsp.jstl-api:3.0.2") api("jakarta.servlet.jsp:jakarta.servlet.jsp-api:4.0.0") api("jakarta.servlet:jakarta.servlet-api:6.1.0") api("jakarta.transaction:jakarta.transaction-api:2.0.1") - api("jakarta.validation:jakarta.validation-api:3.0.2") + api("jakarta.validation:jakarta.validation-api:3.1.0") api("jakarta.websocket:jakarta.websocket-api:2.2.0") api("jakarta.websocket:jakarta.websocket-client-api:2.2.0") api("jakarta.xml.bind:jakarta.xml.bind-api:3.0.1") - api("javax.annotation:javax.annotation-api:1.3.2") api("javax.cache:cache-api:1.1.1") - api("javax.inject:javax.inject:1") api("javax.money:money-api:1.1") api("jaxen:jaxen:1.2.0") api("junit:junit:4.13.2") @@ -117,9 +114,10 @@ dependencies { api("org.crac:crac:1.4.0") api("org.dom4j:dom4j:2.1.4") api("org.easymock:easymock:5.4.0") + api("org.eclipse.angus:angus-mail:2.0.3") api("org.eclipse.jetty:jetty-reactive-httpclient:4.0.8") - api("org.eclipse.persistence:org.eclipse.persistence.jpa:3.0.4") - api("org.eclipse:yasson:2.0.4") + api("org.eclipse.persistence:org.eclipse.persistence.jpa:5.0.0-B04") + api("org.eclipse:yasson:3.0.4") api("org.ehcache:ehcache:3.10.8") api("org.ehcache:jcache:1.0.1") api("org.freemarker:freemarker:2.3.33") @@ -128,8 +126,8 @@ dependencies { api("org.glassfish.tyrus:tyrus-container-servlet:2.1.3") api("org.graalvm.sdk:graal-sdk:22.3.1") api("org.hamcrest:hamcrest:2.2") - api("org.hibernate:hibernate-core-jakarta:5.6.15.Final") - api("org.hibernate:hibernate-validator:7.0.5.Final") + api("org.hibernate:hibernate-core:7.0.0.Beta2") + api("org.hibernate:hibernate-validator:9.0.0.Beta3") api("org.hsqldb:hsqldb:2.7.4") api("org.htmlunit:htmlunit:4.6.0") api("org.javamoney:moneta:1.4.4") diff --git a/integration-tests/integration-tests.gradle b/integration-tests/integration-tests.gradle index 1444b2bb210..b386be32ddb 100644 --- a/integration-tests/integration-tests.gradle +++ b/integration-tests/integration-tests.gradle @@ -26,7 +26,7 @@ dependencies { testImplementation("jakarta.servlet:jakarta.servlet-api") testImplementation("org.aspectj:aspectjweaver") testImplementation("org.hsqldb:hsqldb") - testImplementation("org.hibernate:hibernate-core-jakarta") + testImplementation("org.hibernate:hibernate-core") testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor") } diff --git a/spring-beans/spring-beans.gradle b/spring-beans/spring-beans.gradle index c4fb10eb320..b407bf0ed24 100644 --- a/spring-beans/spring-beans.gradle +++ b/spring-beans/spring-beans.gradle @@ -16,5 +16,4 @@ dependencies { testImplementation(project(":spring-core-test")) testImplementation(testFixtures(project(":spring-core"))) testImplementation("jakarta.annotation:jakarta.annotation-api") - testImplementation("javax.inject:javax.inject") } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index d0ba827163e..c187dccf97e 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -103,8 +103,6 @@ import org.springframework.util.StringUtils; * *
Also supports the common {@link jakarta.inject.Inject @Inject} annotation, * if available, as a direct alternative to Spring's own {@code @Autowired}. - * Additionally, it retains support for the {@code javax.inject.Inject} variant - * dating back to the original JSR-330 specification (as known from Java EE 6-8). * *
Only one constructor of any given bean class may declare this annotation with @@ -189,8 +187,8 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA /** * Create a new {@code AutowiredAnnotationBeanPostProcessor} for Spring's * standard {@link Autowired @Autowired} and {@link Value @Value} annotations. - *
Also supports the common {@link jakarta.inject.Inject @Inject} annotation, - * if available, as well as the original {@code javax.inject.Inject} variant. + *
Also supports the common {@link jakarta.inject.Inject @Inject} annotation + * if available. */ @SuppressWarnings("unchecked") public AutowiredAnnotationBeanPostProcessor() { @@ -206,15 +204,6 @@ public class AutowiredAnnotationBeanPostProcessor implements SmartInstantiationA catch (ClassNotFoundException ex) { // jakarta.inject API not available - simply skip. } - - try { - this.autowiredAnnotationTypes.add((Class extends Annotation>) - ClassUtils.forName("javax.inject.Inject", classLoader)); - logger.trace("'javax.inject.Inject' annotation found and supported for autowiring"); - } - catch (ClassNotFoundException ex) { - // javax.inject API not available - simply skip. - } } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHints.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHints.java index 439b1fb30e4..c4ad0a257da 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHints.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHints.java @@ -33,14 +33,10 @@ class JakartaAnnotationsRuntimeHints implements RuntimeHintsRegistrar { @Override public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) { - // javax.inject.Provider is omitted from the list, since we do not currently load - // it via reflection. Stream.of( "jakarta.inject.Inject", "jakarta.inject.Provider", - "jakarta.inject.Qualifier", - "javax.inject.Inject", - "javax.inject.Qualifier" + "jakarta.inject.Qualifier" ).forEach(typeName -> hints.reflection().registerType(TypeReference.of(typeName))); } diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java index 4281e653949..1e086230061 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java @@ -47,8 +47,7 @@ import org.springframework.util.ObjectUtils; * against {@link Qualifier qualifier annotations} on the field or parameter to be autowired. * Also supports suggested expression values through a {@link Value value} annotation. * - *
Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation (as well as its - * pre-Jakarta {@code javax.inject.Qualifier} equivalent), if available. + *
Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation if available. * * @author Mark Fisher * @author Juergen Hoeller @@ -69,8 +68,7 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa /** * Create a new {@code QualifierAnnotationAutowireCandidateResolver} for Spring's * standard {@link Qualifier} annotation. - *
Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation (as well as - * its pre-Jakarta {@code javax.inject.Qualifier} equivalent), if available. + *
Also supports JSR-330's {@link jakarta.inject.Qualifier} annotation if available. */ @SuppressWarnings("unchecked") public QualifierAnnotationAutowireCandidateResolver() { @@ -82,13 +80,6 @@ public class QualifierAnnotationAutowireCandidateResolver extends GenericTypeAwa catch (ClassNotFoundException ex) { // JSR-330 API (as included in Jakarta EE) not available - simply skip. } - try { - this.qualifierTypes.add((Class extends Annotation>) ClassUtils.forName("javax.inject.Qualifier", - QualifierAnnotationAutowireCandidateResolver.class.getClassLoader())); - } - catch (ClassNotFoundException ex) { - // JSR-330 API not available - simply skip. - } } /** diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java index 67aad00a312..5f91f6bdcee 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/RootBeanDefinition.java @@ -508,8 +508,8 @@ public class RootBeanDefinition extends AbstractBeanDefinition { /** * Register an externally managed configuration initialization method — - * for example, a method annotated with JSR-250's {@code javax.annotation.PostConstruct} - * or Jakarta's {@link jakarta.annotation.PostConstruct} annotation. + * for example, a method annotated with Jakarta's + * {@link jakarta.annotation.PostConstruct} annotation. *
The supplied {@code initMethod} may be a * {@linkplain Method#getName() simple method name} or a * {@linkplain org.springframework.util.ClassUtils#getQualifiedMethodName(Method) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHintsTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHintsTests.java index ef2e236fb68..371e93013d4 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHintsTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/JakartaAnnotationsRuntimeHintsTests.java @@ -63,14 +63,4 @@ class JakartaAnnotationsRuntimeHintsTests { assertThat(RuntimeHintsPredicates.reflection().onType(Qualifier.class)).accepts(this.hints); } - @Test // gh-33345 - void javaxInjectAnnotationHasHints() { - assertThat(RuntimeHintsPredicates.reflection().onType(javax.inject.Inject.class)).accepts(this.hints); - } - - @Test // gh-33345 - void javaxQualifierAnnotationHasHints() { - assertThat(RuntimeHintsPredicates.reflection().onType(javax.inject.Qualifier.class)).accepts(this.hints); - } - } diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java index 99508148837..531782f4043 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java @@ -21,7 +21,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.nio.file.Path; -import jakarta.annotation.ManagedBean; import jakarta.inject.Named; import jakarta.persistence.Converter; import jakarta.persistence.Embeddable; @@ -43,7 +42,6 @@ import org.springframework.context.index.sample.SampleNonStaticEmbedded; import org.springframework.context.index.sample.SampleNone; import org.springframework.context.index.sample.SampleRepository; import org.springframework.context.index.sample.SampleService; -import org.springframework.context.index.sample.cdi.SampleManagedBean; import org.springframework.context.index.sample.cdi.SampleNamed; import org.springframework.context.index.sample.cdi.SampleTransactional; import org.springframework.context.index.sample.jpa.SampleConverter; @@ -126,11 +124,6 @@ class CandidateComponentsIndexerTests { testComponent(AbstractController.class); } - @Test - void cdiManagedBean() { - testSingleComponent(SampleManagedBean.class, ManagedBean.class); - } - @Test void cdiNamed() { testSingleComponent(SampleNamed.class, Named.class); diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java b/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java deleted file mode 100644 index fb34361664d..00000000000 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/sample/cdi/SampleManagedBean.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2002-2016 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.context.index.sample.cdi; - -import jakarta.annotation.ManagedBean; - -/** - * Test candidate for a CDI {@link ManagedBean}. - * - * @author Stephane Nicoll - */ -@ManagedBean -public class SampleManagedBean { -} diff --git a/spring-context-support/spring-context-support.gradle b/spring-context-support/spring-context-support.gradle index a2f0c083e73..851125b73e5 100644 --- a/spring-context-support/spring-context-support.gradle +++ b/spring-context-support/spring-context-support.gradle @@ -23,7 +23,7 @@ dependencies { testImplementation("io.projectreactor:reactor-core") testImplementation("jakarta.annotation:jakarta.annotation-api") testImplementation("org.hsqldb:hsqldb") - testRuntimeOnly("com.sun.mail:jakarta.mail") + testRuntimeOnly("org.eclipse.angus:angus-mail") testRuntimeOnly("org.ehcache:ehcache") testRuntimeOnly("org.ehcache:jcache") testRuntimeOnly("org.glassfish:jakarta.el") diff --git a/spring-context/spring-context.gradle b/spring-context/spring-context.gradle index af48a0fa207..4a24c9ac41c 100644 --- a/spring-context/spring-context.gradle +++ b/spring-context/spring-context.gradle @@ -21,8 +21,6 @@ dependencies { optional("jakarta.inject:jakarta.inject-api") optional("jakarta.interceptor:jakarta.interceptor-api") optional("jakarta.validation:jakarta.validation-api") - optional("javax.annotation:javax.annotation-api") - optional("javax.inject:javax.inject") optional("javax.money:money-api") optional("org.apache.groovy:groovy") optional("org.apache-extras.beanshell:bsh") diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java index 3361f0560e5..d8bb37df83f 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -51,11 +51,8 @@ import org.springframework.util.StringUtils; * {@link org.springframework.stereotype.Repository @Repository}) are * themselves annotated with {@code @Component}. * - *
Also supports Jakarta EE's {@link jakarta.annotation.ManagedBean} and - * JSR-330's {@link jakarta.inject.Named} annotations (as well as their pre-Jakarta - * {@code javax.annotation.ManagedBean} and {@code javax.inject.Named} equivalents), - * if available. Note that Spring component annotations always override such - * standard annotations. + *
Also supports JSR-330's {@link jakarta.inject.Named} annotation if available. + * Note that Spring component annotations always override such standard annotations. * *
If the annotation's value doesn't indicate a bean name, an appropriate
* name will be built based on the short name of the class (with the first
@@ -219,10 +216,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
Set Also supports Jakarta EE's {@link jakarta.annotation.ManagedBean} and
- * JSR-330's {@link jakarta.inject.Named} annotations, if available.
+ * Also supports JSR-330's {@link jakarta.inject.Named} annotations, if available.
*
* @author Mark Fisher
* @author Juergen Hoeller
diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
index daeb1cd833e..6016fcf6556 100644
--- a/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
+++ b/spring-context/src/main/java/org/springframework/context/annotation/ClassPathScanningCandidateComponentProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2023 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -216,31 +216,12 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
* {@link Component @Component} meta-annotation including the
* {@link Repository @Repository}, {@link Service @Service}, and
* {@link Controller @Controller} stereotype annotations.
- * Also supports Jakarta EE's {@link jakarta.annotation.ManagedBean} and
- * JSR-330's {@link jakarta.inject.Named} annotations (as well as their
- * pre-Jakarta {@code javax.annotation.ManagedBean} and {@code javax.inject.Named}
- * equivalents), if available.
+ * Also supports JSR-330's {@link jakarta.inject.Named} annotation if available.
*/
@SuppressWarnings("unchecked")
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
- try {
- this.includeFilters.add(new AnnotationTypeFilter(
- ((Class extends Annotation>) ClassUtils.forName("jakarta.annotation.ManagedBean", cl)), false));
- logger.trace("JSR-250 'jakarta.annotation.ManagedBean' found and supported for component scanning");
- }
- catch (ClassNotFoundException ex) {
- // JSR-250 1.1 API (as included in Jakarta EE) not available - simply skip.
- }
- try {
- this.includeFilters.add(new AnnotationTypeFilter(
- ((Class extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
- logger.trace("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
- }
- catch (ClassNotFoundException ex) {
- // JSR-250 1.1 API not available - simply skip.
- }
try {
this.includeFilters.add(new AnnotationTypeFilter(
((Class extends Annotation>) ClassUtils.forName("jakarta.inject.Named", cl)), false));
@@ -249,14 +230,6 @@ public class ClassPathScanningCandidateComponentProvider implements EnvironmentC
catch (ClassNotFoundException ex) {
// JSR-330 API (as included in Jakarta EE) not available - simply skip.
}
- try {
- this.includeFilters.add(new AnnotationTypeFilter(
- ((Class extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
- logger.trace("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
- }
- catch (ClassNotFoundException ex) {
- // JSR-330 API not available - simply skip.
- }
}
/**
diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java
index a5ffc7858f8..7e65b98cdbf 100644
--- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java
+++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java
@@ -96,11 +96,6 @@ import org.springframework.util.StringValueResolver;
* and default names as well. The target beans can be simple POJOs, with no special
* requirements other than the type having to match.
*
- * Additionally, the original {@code javax.annotation} variants of the annotations
- * dating back to the JSR-250 specification (Java EE 5-8, also included in JDK 6-8)
- * are still supported as well. Note that this is primarily for a smooth upgrade path,
- * not for adoption in new applications.
- *
* This post-processor also supports the EJB {@link jakarta.ejb.EJB} annotation,
* analogous to {@link jakarta.annotation.Resource}, with the capability to
* specify both a local bean name and a global JNDI name for fallback retrieval.
@@ -154,9 +149,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
@Nullable
private static final Class extends Annotation> jakartaResourceType;
- @Nullable
- private static final Class extends Annotation> javaxResourceType;
-
@Nullable
private static final Class extends Annotation> ejbAnnotationType;
@@ -166,11 +158,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
resourceAnnotationTypes.add(jakartaResourceType);
}
- javaxResourceType = loadAnnotationType("javax.annotation.Resource");
- if (javaxResourceType != null) {
- resourceAnnotationTypes.add(javaxResourceType);
- }
-
ejbAnnotationType = loadAnnotationType("jakarta.ejb.EJB");
if (ejbAnnotationType != null) {
resourceAnnotationTypes.add(ejbAnnotationType);
@@ -212,10 +199,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
addInitAnnotationType(loadAnnotationType("jakarta.annotation.PostConstruct"));
addDestroyAnnotationType(loadAnnotationType("jakarta.annotation.PreDestroy"));
- // Tolerate legacy JSR-250 annotations in javax.annotation package
- addInitAnnotationType(loadAnnotationType("javax.annotation.PostConstruct"));
- addDestroyAnnotationType(loadAnnotationType("javax.annotation.PreDestroy"));
-
// java.naming module present on JDK 9+?
if (jndiPresent) {
this.jndiFactory = new SimpleJndiBeanFactory();
@@ -444,14 +427,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
currElements.add(new ResourceElement(field, field, null));
}
}
- else if (javaxResourceType != null && field.isAnnotationPresent(javaxResourceType)) {
- if (Modifier.isStatic(field.getModifiers())) {
- throw new IllegalStateException("@Resource annotation is not supported on static fields");
- }
- if (!this.ignoredResourceTypes.contains(field.getType().getName())) {
- currElements.add(new LegacyResourceElement(field, field, null));
- }
- }
});
ReflectionUtils.doWithLocalMethods(targetClass, method -> {
@@ -486,21 +461,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
}
}
}
- else if (javaxResourceType != null && bridgedMethod.isAnnotationPresent(javaxResourceType)) {
- if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
- if (Modifier.isStatic(method.getModifiers())) {
- throw new IllegalStateException("@Resource annotation is not supported on static methods");
- }
- Class>[] paramTypes = method.getParameterTypes();
- if (paramTypes.length != 1) {
- throw new IllegalStateException("@Resource annotation requires a single-arg method: " + method);
- }
- if (!this.ignoredResourceTypes.contains(paramTypes[0].getName())) {
- PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
- currElements.add(new LegacyResourceElement(method, bridgedMethod, pd));
- }
- }
- }
});
elements.addAll(0, currElements);
@@ -746,57 +706,6 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
}
- /**
- * Class representing injection information about an annotated field
- * or setter method, supporting the @Resource annotation.
- */
- private class LegacyResourceElement extends LookupElement {
-
- private final boolean lazyLookup;
-
- public LegacyResourceElement(Member member, AnnotatedElement ae, @Nullable PropertyDescriptor pd) {
- super(member, pd);
- javax.annotation.Resource resource = ae.getAnnotation(javax.annotation.Resource.class);
- String resourceName = resource.name();
- Class> resourceType = resource.type();
- this.isDefaultName = !StringUtils.hasLength(resourceName);
- if (this.isDefaultName) {
- resourceName = this.member.getName();
- if (this.member instanceof Method && resourceName.startsWith("set") && resourceName.length() > 3) {
- resourceName = StringUtils.uncapitalizeAsProperty(resourceName.substring(3));
- }
- }
- else if (embeddedValueResolver != null) {
- resourceName = embeddedValueResolver.resolveStringValue(resourceName);
- }
- if (Object.class != resourceType) {
- checkResourceType(resourceType);
- }
- else {
- // No resource type specified... check field/method.
- resourceType = getResourceType();
- }
- this.name = (resourceName != null ? resourceName : "");
- this.lookupType = resourceType;
- String lookupValue = resource.lookup();
- this.mappedName = (StringUtils.hasLength(lookupValue) ? lookupValue : resource.mappedName());
- Lazy lazy = ae.getAnnotation(Lazy.class);
- this.lazyLookup = (lazy != null && lazy.value());
- }
-
- @Override
- protected Object getResourceToInject(Object target, @Nullable String requestingBeanName) {
- return (this.lazyLookup ? buildLazyResourceProxy(this, requestingBeanName) :
- getResource(this, requestingBeanName));
- }
-
- @Override
- boolean isLazyLookup() {
- return this.lazyLookup;
- }
- }
-
-
/**
* Class representing injection information about an annotated field
* or setter method, supporting the @EJB annotation.
diff --git a/spring-context/src/test/java/example/indexed/IndexedJakartaManagedBeanComponent.java b/spring-context/src/test/java/example/indexed/IndexedJakartaManagedBeanComponent.java
deleted file mode 100644
index ed640a7a73d..00000000000
--- a/spring-context/src/test/java/example/indexed/IndexedJakartaManagedBeanComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2022 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.indexed;
-
-/**
- * @author Sam Brannen
- */
-@jakarta.annotation.ManagedBean
-public class IndexedJakartaManagedBeanComponent {
-}
diff --git a/spring-context/src/test/java/example/indexed/IndexedJavaxManagedBeanComponent.java b/spring-context/src/test/java/example/indexed/IndexedJavaxManagedBeanComponent.java
deleted file mode 100644
index b563b4d3797..00000000000
--- a/spring-context/src/test/java/example/indexed/IndexedJavaxManagedBeanComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.indexed;
-
-/**
- * @author Sam Brannen
- */
-@javax.annotation.ManagedBean
-public class IndexedJavaxManagedBeanComponent {
-}
diff --git a/spring-context/src/test/java/example/indexed/IndexedJavaxNamedComponent.java b/spring-context/src/test/java/example/indexed/IndexedJavaxNamedComponent.java
deleted file mode 100644
index 581be8a6f97..00000000000
--- a/spring-context/src/test/java/example/indexed/IndexedJavaxNamedComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.indexed;
-
-/**
- * @author Sam Brannen
- */
-@javax.inject.Named("myIndexedJavaxNamedComponent")
-public class IndexedJavaxNamedComponent {
-}
diff --git a/spring-context/src/test/java/example/scannable/JakartaManagedBeanComponent.java b/spring-context/src/test/java/example/scannable/JakartaManagedBeanComponent.java
deleted file mode 100644
index 6140ea0dce3..00000000000
--- a/spring-context/src/test/java/example/scannable/JakartaManagedBeanComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.scannable;
-
-/**
- * @author Sam Brannen
- */
-@jakarta.annotation.ManagedBean("myJakartaManagedBeanComponent")
-public class JakartaManagedBeanComponent {
-}
diff --git a/spring-context/src/test/java/example/scannable/JavaxManagedBeanComponent.java b/spring-context/src/test/java/example/scannable/JavaxManagedBeanComponent.java
deleted file mode 100644
index b3029035d87..00000000000
--- a/spring-context/src/test/java/example/scannable/JavaxManagedBeanComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.scannable;
-
-/**
- * @author Sam Brannen
- */
-@javax.annotation.ManagedBean("myJavaxManagedBeanComponent")
-public class JavaxManagedBeanComponent {
-}
diff --git a/spring-context/src/test/java/example/scannable/JavaxNamedComponent.java b/spring-context/src/test/java/example/scannable/JavaxNamedComponent.java
deleted file mode 100644
index a0fe78e7429..00000000000
--- a/spring-context/src/test/java/example/scannable/JavaxNamedComponent.java
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package example.scannable;
-
-/**
- * @author Sam Brannen
- */
-@javax.inject.Named("myJavaxNamedComponent")
-public class JavaxNamedComponent {
-}
diff --git a/spring-context/src/test/java/org/springframework/beans/factory/support/InjectAnnotationAutowireContextTests.java b/spring-context/src/test/java/org/springframework/beans/factory/support/InjectAnnotationAutowireContextTests.java
index 48d19fbf7e8..bfbcbb09677 100644
--- a/spring-context/src/test/java/org/springframework/beans/factory/support/InjectAnnotationAutowireContextTests.java
+++ b/spring-context/src/test/java/org/springframework/beans/factory/support/InjectAnnotationAutowireContextTests.java
@@ -40,8 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
- * Integration tests for handling JSR-330 {@link jakarta.inject.Qualifier} and
- * {@link javax.inject.Qualifier} annotations.
+ * Integration tests for handling {@link jakarta.inject.Qualifier} annotations.
*
* @author Juergen Hoeller
* @author Sam Brannen
@@ -317,16 +316,6 @@ class InjectAnnotationAutowireContextTests {
assertThat(bean.getAnimal2().getName()).isEqualTo("Jakarta Fido");
}
- @Test // gh-33345
- void autowiredConstructorArgumentResolvesJavaxNamedCandidate() {
- Class Defines {@code HibernateTemplate}'s data access methods that mirror various
- * {@link org.hibernate.Session} methods. Users are strongly encouraged to read the
- * Hibernate {@code Session} javadocs for details on the semantics of those methods.
- *
- * A deprecation note: While {@link HibernateTemplate} and this operations
- * interface are being kept around for backwards compatibility in terms of the data
- * access implementation style in Spring applications, we strongly recommend the use
- * of native {@link org.hibernate.Session} access code for non-trivial interactions.
- * This in particular affects parameterized queries where - on Java 8+ - a custom
- * {@link HibernateCallback} lambda code block with {@code createQuery} and several
- * {@code setParameter} calls on the {@link org.hibernate.query.Query} interface
- * is an elegant solution, to be executed via the general {@link #execute} method.
- * All such operations which benefit from a lambda variant have been marked as
- * {@code deprecated} on this interface.
- *
- * A Hibernate compatibility note: {@link HibernateTemplate} and the
- * operations on this interface generally aim to be applicable across all Hibernate
- * versions. In terms of binary compatibility, Spring ships a variant for each major
- * generation of Hibernate (in the present case: Hibernate ORM 5.x). However, due to
- * refactorings and removals in Hibernate ORM 5.3, some variants - in particular
- * legacy positional parameters starting from index 0 - do not work anymore.
- * All affected operations are marked as deprecated; please replace them with the
- * general {@link #execute} method and custom lambda blocks creating the queries,
- * ideally setting named parameters through {@link org.hibernate.query.Query}.
- * Please be aware that deprecated operations are known to work with Hibernate
- * ORM 5.2 but may not work with Hibernate ORM 5.3 and higher anymore.
- *
- * @author Juergen Hoeller
- * @since 4.2
- * @see HibernateTemplate
- * @see org.hibernate.Session
- * @see HibernateTransactionManager
- */
-public interface HibernateOperations {
-
- /**
- * Execute the action specified by the given action object within a
- * {@link org.hibernate.Session}.
- * Application exceptions thrown by the action object get propagated
- * to the caller (can only be unchecked). Hibernate exceptions are
- * transformed into appropriate DAO ones. Allows for returning a result
- * object, that is a domain object or a collection of domain objects.
- * Note: Callback code is not supposed to handle transactions itself!
- * Use an appropriate transaction manager like
- * {@link HibernateTransactionManager}. Generally, callback code must not
- * touch any {@code Session} lifecycle methods, like close,
- * disconnect, or reconnect, to let the template do its work.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws DataAccessException in case of Hibernate errors
- * @see HibernateTransactionManager
- * @see org.hibernate.Session
- */
- @Nullable
- This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, Serializable)
- */
- @Nullable
- Obtains the specified lock mode if the instance exists.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#get(Class, Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, Serializable, LockMode)
- */
- @Nullable
- This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance, or {@code null} if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, Serializable)
- */
- @Nullable
- Object get(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, or {@code null} if not found.
- * Obtains the specified lock mode if the instance exists.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#get(String, Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance, or {@code null} if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#get(Class, Serializable, LockMode)
- */
- @Nullable
- Object get(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, Serializable)
- */
- This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Class, Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityClass a persistent class
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, Serializable)
- */
- This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, Serializable)
- */
- Object load(String entityName, Serializable id) throws DataAccessException;
-
- /**
- * Return the persistent instance of the given entity class
- * with the given identifier, throwing an exception if not found.
- * Obtains the specified lock mode if the instance exists.
- * This method is a thin wrapper around
- * {@link org.hibernate.Session#load(String, Serializable, LockMode)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entityName the name of the persistent entity
- * @param id the identifier of the persistent instance
- * @param lockMode the lock mode to obtain
- * @return the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Class, Serializable)
- */
- Object load(String entityName, Serializable id, LockMode lockMode) throws DataAccessException;
-
- /**
- * Return all persistent instances of the given entity class.
- * Note: Use queries or criteria for retrieving a specific subset.
- * @param entityClass a persistent class
- * @return a {@link List} containing 0 or more persistent instances
- * @throws DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#createCriteria
- */
- This method is a thin wrapper around
- * {@link org.hibernate.Session#load(Object, Serializable)} for convenience.
- * For an explanation of the exact semantics of this method, please do refer to
- * the Hibernate API documentation in the first instance.
- * @param entity the object (of the target class) to load into
- * @param id the identifier of the persistent instance
- * @throws org.springframework.orm.ObjectRetrievalFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#load(Object, Serializable)
- */
- void load(Object entity, Serializable id) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * @param entity the persistent instance to re-read
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object)
- */
- void refresh(Object entity) throws DataAccessException;
-
- /**
- * Re-read the state of the given persistent instance.
- * Obtains the specified lock mode for the instance.
- * @param entity the persistent instance to re-read
- * @param lockMode the lock mode to obtain
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#refresh(Object, LockMode)
- */
- void refresh(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Check whether the given object is in the Session cache.
- * @param entity the persistence instance to check
- * @return whether the given object is in the Session cache
- * @throws DataAccessException if there is a Hibernate error
- * @see org.hibernate.Session#contains
- */
- boolean contains(Object entity) throws DataAccessException;
-
- /**
- * Remove the given object from the {@link org.hibernate.Session} cache.
- * @param entity the persistent instance to evict
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#evict
- */
- void evict(Object entity) throws DataAccessException;
-
- /**
- * Force initialization of a Hibernate proxy or persistent collection.
- * @param proxy a proxy for a persistent object or a persistent collection
- * @throws DataAccessException if we can't initialize the proxy, for example
- * because it is not associated with an active Session
- * @see org.hibernate.Hibernate#initialize
- */
- void initialize(Object proxy) throws DataAccessException;
-
- /**
- * Return an enabled Hibernate {@link Filter} for the given filter name.
- * The returned {@code Filter} instance can be used to set filter parameters.
- * @param filterName the name of the filter
- * @return the enabled Hibernate {@code Filter} (either already
- * enabled or enabled on the fly by this operation)
- * @throws IllegalStateException if we are not running within a
- * transactional Session (in which case this operation does not make sense)
- */
- Filter enableFilter(String filterName) throws IllegalStateException;
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for storing individual objects
- //-------------------------------------------------------------------------
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(Object, LockMode)
- */
- void lock(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Obtain the specified lock level upon the given object, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to lock
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#lock(String, Object, LockMode)
- */
- void lock(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(Object)
- */
- Serializable save(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance.
- * @param entityName the name of the persistent entity
- * @param entity the transient instance to persist
- * @return the generated identifier
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#save(String, Object)
- */
- Serializable save(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to update
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(Object)
- */
- void update(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Update the given persistent instance,
- * associating it with the current Hibernate {@link org.hibernate.Session}.
- * Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to update
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#update(String, Object)
- */
- void update(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(Object)
- */
- void saveOrUpdate(Object entity) throws DataAccessException;
-
- /**
- * Save or update the given persistent instance,
- * according to its id (matching the configured "unsaved-value"?).
- * Associates the instance with the current Hibernate {@code Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to save or update
- * (to be associated with the Hibernate {@code Session})
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#saveOrUpdate(String, Object)
- */
- void saveOrUpdate(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(Object, ReplicationMode)
- */
- void replicate(Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the state of the given detached instance according to the
- * given replication mode, reusing the current identifier value.
- * @param entityName the name of the persistent entity
- * @param entity the persistent object to replicate
- * @param replicationMode the Hibernate ReplicationMode
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#replicate(String, Object, ReplicationMode)
- */
- void replicate(String entityName, Object entity, ReplicationMode replicationMode) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- * Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entity the persistent instance to persist
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(Object)
- * @see #save
- */
- void persist(Object entity) throws DataAccessException;
-
- /**
- * Persist the given transient instance. Follows JSR-220 semantics.
- * Similar to {@code save}, associating the given object
- * with the current Hibernate {@link org.hibernate.Session}.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to persist
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#persist(String, Object)
- * @see #save
- */
- void persist(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Copy the state of the given object onto the persistent object
- * with the same identifier. Follows JSR-220 semantics.
- * Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate Session. In case of a new entity,
- * the state will be copied over as well.
- * Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener} if
- * you would like to have newly assigned ids transferred to the original
- * object graph too.
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(Object)
- * @see #saveOrUpdate
- */
- Similar to {@code saveOrUpdate}, but never associates the given
- * object with the current Hibernate {@link org.hibernate.Session}. In
- * the case of a new entity, the state will be copied over as well.
- * Note that {@code merge} will not update the identifiers
- * in the passed-in object graph (in contrast to TopLink)! Consider
- * registering Spring's {@code IdTransferringMergeEventListener}
- * if you would like to have newly assigned ids transferred to the
- * original object graph too.
- * @param entityName the name of the persistent entity
- * @param entity the object to merge with the corresponding persistence instance
- * @return the updated, registered persistent instance
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#merge(String, Object)
- * @see #saveOrUpdate
- */
- Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity) throws DataAccessException;
-
- /**
- * Delete the given persistent instance.
- * Obtains the specified lock mode if the instance exists, implicitly
- * checking whether the corresponding database entry still exists.
- * @param entityName the name of the persistent entity
- * @param entity the persistent instance to delete
- * @param lockMode the lock mode to obtain
- * @throws org.springframework.orm.ObjectOptimisticLockingFailureException if not found
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void delete(String entityName, Object entity, LockMode lockMode) throws DataAccessException;
-
- /**
- * Delete all given persistent instances.
- * This can be combined with any of the find methods to delete by query
- * in two lines of code.
- * @param entities the persistent instances to delete
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#delete(Object)
- */
- void deleteAll(Collection> entities) throws DataAccessException;
-
- /**
- * Flush all pending saves, updates and deletes to the database.
- * Only invoke this for selective eager flushing, for example when
- * JDBC code needs to see certain changes within the same transaction.
- * Else, it is preferable to rely on auto-flushing at transaction
- * completion.
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#flush
- */
- void flush() throws DataAccessException;
-
- /**
- * Remove all objects from the {@link org.hibernate.Session} cache, and
- * cancel all pending saves, updates and deletes.
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#clear
- */
- void clear() throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for detached criteria
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query based on a given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @return a {@link List} containing 0 or more persistent instances
- * @throws DataAccessException in case of Hibernate errors
- * @see DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- */
- List> findByCriteria(DetachedCriteria criteria) throws DataAccessException;
-
- /**
- * Execute a query based on the given Hibernate criteria object.
- * @param criteria the detached Hibernate criteria object.
- * Note: Do not reuse criteria objects! They need to recreated per execution,
- * due to the suboptimal design of Hibernate's criteria facility.
- * @param firstResult the index of the first result object to be retrieved
- * (numbered from 0)
- * @param maxResults the maximum number of result objects to retrieve
- * (or <=0 for no limit)
- * @return a {@link List} containing 0 or more persistent instances
- * @throws DataAccessException in case of Hibernate errors
- * @see DetachedCriteria#getExecutableCriteria(org.hibernate.Session)
- * @see org.hibernate.Criteria#setFirstResult(int)
- * @see org.hibernate.Criteria#setMaxResults(int)
- */
- List> findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) throws DataAccessException;
-
- /**
- * Execute a query based on the given example entity object.
- * @param exampleEntity an instance of the desired entity,
- * serving as example for "query-by-example"
- * @return a {@link List} containing 0 or more persistent instances
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.criterion.Example#create(Object)
- */
- A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- List> findByNamedQuery(String queryName, Object... values) throws DataAccessException;
-
- /**
- * Execute a named query, binding one value to a ":" named parameter
- * in the query string.
- * A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramName the name of parameter
- * @param value the value of the parameter
- * @return a {@link List} containing the results of the query execution
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- List> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding a number of values to ":" named
- * parameters in the query string.
- * A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param paramNames the names of the parameters
- * @param values the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#getNamedQuery(String)
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- List> findByNamedQueryAndNamedParam(String queryName, String[] paramNames, Object[] values)
- throws DataAccessException;
-
- /**
- * Execute a named query, binding the properties of the given bean to
- * ":" named parameters in the query string.
- * A named query is defined in a Hibernate mapping file.
- * @param queryName the name of a Hibernate query in a mapping file
- * @param valueBean the values of the parameters
- * @return a {@link List} containing the results of the query execution
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Query#setProperties
- * @see org.hibernate.Session#getNamedQuery(String)
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- List> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException;
-
-
- //-------------------------------------------------------------------------
- // Convenience query methods for iteration and bulk updates/deletes
- //-------------------------------------------------------------------------
-
- /**
- * Execute a query for persistent instances, binding a number of
- * values to "?" parameters in the query string.
- * Returns the results as an {@link Iterator}. Entities returned are
- * initialized on demand. See the Hibernate API documentation for details.
- * @param queryString a query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return an {@link Iterator} containing 0 or more persistent instances
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#iterate
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- Iterator> iterate(String queryString, Object... values) throws DataAccessException;
-
- /**
- * Immediately close an {@link Iterator} created by any of the various
- * {@code iterate(..)} operations, instead of waiting until the
- * session is closed or disconnected.
- * @param it the {@code Iterator} to close
- * @throws DataAccessException if the {@code Iterator} could not be closed
- * @see org.hibernate.Hibernate#close
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- void closeIterator(Iterator> it) throws DataAccessException;
-
- /**
- * Update/delete all objects according to the given query, binding a number of
- * values to "?" parameters in the query string.
- * @param queryString an update/delete query expressed in Hibernate's query language
- * @param values the values of the parameters
- * @return the number of instances updated/deleted
- * @throws DataAccessException in case of Hibernate errors
- * @see org.hibernate.Session#createQuery
- * @see org.hibernate.Query#executeUpdate
- * @deprecated as of 5.0.4, in favor of a custom {@link HibernateCallback}
- * lambda code block passed to the general {@link #execute} method
- */
- @Deprecated
- int bulkUpdate(String queryString, Object... values) throws DataAccessException;
-
-}
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateOptimisticLockingFailureException.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateOptimisticLockingFailureException.java
index 0246df5a281..c3bbfe98a33 100644
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateOptimisticLockingFailureException.java
+++ b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateOptimisticLockingFailureException.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2022 the original author or authors.
+ * Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,7 +35,7 @@ import org.springframework.orm.ObjectOptimisticLockingFailureException;
public class HibernateOptimisticLockingFailureException extends ObjectOptimisticLockingFailureException {
public HibernateOptimisticLockingFailureException(StaleObjectStateException ex) {
- super(ex.getEntityName(), HibernateObjectRetrievalFailureException.getIdentifier(ex), ex.getMessage(), ex);
+ super(ex.getEntityName(), ex.getIdentifier(), ex.getMessage(), ex);
}
public HibernateOptimisticLockingFailureException(StaleStateException ex) {
diff --git a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java b/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java
deleted file mode 100644
index 51ef7fd4620..00000000000
--- a/spring-orm/src/main/java/org/springframework/orm/hibernate5/HibernateTemplate.java
+++ /dev/null
@@ -1,1185 +0,0 @@
-/*
- * Copyright 2002-2023 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.springframework.orm.hibernate5;
-
-import java.io.Serializable;
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-
-import jakarta.persistence.PersistenceException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.hibernate.Criteria;
-import org.hibernate.Filter;
-import org.hibernate.FlushMode;
-import org.hibernate.Hibernate;
-import org.hibernate.HibernateException;
-import org.hibernate.LockMode;
-import org.hibernate.LockOptions;
-import org.hibernate.ReplicationMode;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.criterion.DetachedCriteria;
-import org.hibernate.criterion.Example;
-import org.hibernate.query.Query;
-
-import org.springframework.beans.factory.InitializingBean;
-import org.springframework.dao.DataAccessException;
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.lang.Nullable;
-import org.springframework.transaction.support.ResourceHolderSupport;
-import org.springframework.transaction.support.TransactionSynchronizationManager;
-import org.springframework.util.Assert;
-
-/**
- * Helper class that simplifies Hibernate data access code. Automatically
- * converts HibernateExceptions into DataAccessExceptions, following the
- * {@code org.springframework.dao} exception hierarchy.
- *
- * The central method is {@code execute}, supporting Hibernate access code
- * implementing the {@link HibernateCallback} interface. It provides Hibernate Session
- * handling such that neither the HibernateCallback implementation nor the calling
- * code needs to explicitly care about retrieving/closing Hibernate Sessions,
- * or handling Session lifecycle exceptions. For typical single step actions,
- * there are various convenience methods (find, load, saveOrUpdate, delete).
- *
- * Can be used within a service implementation via direct instantiation
- * with a SessionFactory reference, or get prepared in an application context
- * and given to services as bean reference. Note: The SessionFactory should
- * always be configured as bean in the application context, in the first case
- * given to the service directly, in the second case to the prepared template.
- *
- * NOTE: Hibernate access code can also be coded against the native Hibernate
- * {@link Session}. Hence, for newly started projects, consider adopting the standard
- * Hibernate style of coding against {@link SessionFactory#getCurrentSession()}.
- * Alternatively, use {@link #execute(HibernateCallback)} with Java 8 lambda code blocks
- * against the callback-provided {@code Session} which results in elegant code as well,
- * decoupled from the Hibernate Session lifecycle. The remaining operations on this
- * HibernateTemplate are deprecated in the meantime and primarily exist as a migration
- * helper for older Hibernate 3.x/4.x data access code in existing applications.
- *
- * @author Juergen Hoeller
- * @since 4.2
- * @see #setSessionFactory
- * @see HibernateCallback
- * @see Session
- * @see LocalSessionFactoryBean
- * @see HibernateTransactionManager
- * @see org.springframework.orm.hibernate5.support.OpenSessionInViewFilter
- * @see org.springframework.orm.hibernate5.support.OpenSessionInViewInterceptor
- */
-public class HibernateTemplate implements HibernateOperations, InitializingBean {
-
- protected final Log logger = LogFactory.getLog(getClass());
-
- @Nullable
- private SessionFactory sessionFactory;
-
- @Nullable
- private String[] filterNames;
-
- private boolean exposeNativeSession = false;
-
- private boolean checkWriteOperations = true;
-
- private boolean cacheQueries = false;
-
- @Nullable
- private String queryCacheRegion;
-
- private int fetchSize = 0;
-
- private int maxResults = 0;
-
-
- /**
- * Create a new HibernateTemplate instance.
- */
- public HibernateTemplate() {
- }
-
- /**
- * Create a new HibernateTemplate instance.
- * @param sessionFactory the SessionFactory to create Sessions with
- */
- public HibernateTemplate(SessionFactory sessionFactory) {
- setSessionFactory(sessionFactory);
- afterPropertiesSet();
- }
-
-
- /**
- * Set the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- public void setSessionFactory(@Nullable SessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- /**
- * Return the Hibernate SessionFactory that should be used to create
- * Hibernate Sessions.
- */
- @Nullable
- public SessionFactory getSessionFactory() {
- return this.sessionFactory;
- }
-
- /**
- * Obtain the SessionFactory for actual use.
- * @return the SessionFactory (never {@code null})
- * @throws IllegalStateException in case of no SessionFactory set
- * @since 5.0
- */
- protected final SessionFactory obtainSessionFactory() {
- SessionFactory sessionFactory = getSessionFactory();
- Assert.state(sessionFactory != null, "No SessionFactory set");
- return sessionFactory;
- }
-
- /**
- * Set one or more names of Hibernate filters to be activated for all
- * Sessions that this accessor works with.
- * Each of those filters will be enabled at the beginning of each
- * operation and correspondingly disabled at the end of the operation.
- * This will work for newly opened Sessions as well as for existing
- * Sessions (for example, within a transaction).
- * @see #enableFilters(Session)
- * @see Session#enableFilter(String)
- */
- public void setFilterNames(@Nullable String... filterNames) {
- this.filterNames = filterNames;
- }
-
- /**
- * Return the names of Hibernate filters to be activated, if any.
- */
- @Nullable
- public String[] getFilterNames() {
- return this.filterNames;
- }
-
- /**
- * Set whether to expose the native Hibernate Session to
- * HibernateCallback code.
- * Default is "false": a Session proxy will be returned, suppressing
- * {@code close} calls and automatically applying query cache
- * settings and transaction timeouts.
- * @see HibernateCallback
- * @see Session
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- * @see #prepareQuery
- * @see #prepareCriteria
- */
- public void setExposeNativeSession(boolean exposeNativeSession) {
- this.exposeNativeSession = exposeNativeSession;
- }
-
- /**
- * Return whether to expose the native Hibernate Session to
- * HibernateCallback code, or rather a Session proxy.
- */
- public boolean isExposeNativeSession() {
- return this.exposeNativeSession;
- }
-
- /**
- * Set whether to check that the Hibernate Session is not in read-only mode
- * in case of write operations (save/update/delete).
- * Default is "true", for fail-fast behavior when attempting write operations
- * within a read-only transaction. Turn this off to allow save/update/delete
- * on a Session with flush mode MANUAL.
- * @see #checkWriteOperationAllowed
- * @see org.springframework.transaction.TransactionDefinition#isReadOnly
- */
- public void setCheckWriteOperations(boolean checkWriteOperations) {
- this.checkWriteOperations = checkWriteOperations;
- }
-
- /**
- * Return whether to check that the Hibernate Session is not in read-only
- * mode in case of write operations (save/update/delete).
- */
- public boolean isCheckWriteOperations() {
- return this.checkWriteOperations;
- }
-
- /**
- * Set whether to cache all queries executed by this template.
- * If this is "true", all Query and Criteria objects created by
- * this template will be marked as cacheable (including all
- * queries through find methods).
- * To specify the query region to be used for queries cached
- * by this template, set the "queryCacheRegion" property.
- * @see #setQueryCacheRegion
- * @see Query#setCacheable
- * @see Criteria#setCacheable
- */
- public void setCacheQueries(boolean cacheQueries) {
- this.cacheQueries = cacheQueries;
- }
-
- /**
- * Return whether to cache all queries executed by this template.
- */
- public boolean isCacheQueries() {
- return this.cacheQueries;
- }
-
- /**
- * Set the name of the cache region for queries executed by this template.
- * If this is specified, it will be applied to all Query and Criteria objects
- * created by this template (including all queries through find methods).
- * The cache region will not take effect unless queries created by this
- * template are configured to be cached via the "cacheQueries" property.
- * @see #setCacheQueries
- * @see Query#setCacheRegion
- * @see Criteria#setCacheRegion
- */
- public void setQueryCacheRegion(@Nullable String queryCacheRegion) {
- this.queryCacheRegion = queryCacheRegion;
- }
-
- /**
- * Return the name of the cache region for queries executed by this template.
- */
- @Nullable
- public String getQueryCacheRegion() {
- return this.queryCacheRegion;
- }
-
- /**
- * Set the fetch size for this HibernateTemplate. This is important for processing
- * large result sets: Setting this higher than the default value will increase
- * processing speed at the cost of memory consumption; setting this lower can
- * avoid transferring row data that will never be read by the application.
- * Default is 0, indicating to use the JDBC driver's default.
- */
- public void setFetchSize(int fetchSize) {
- this.fetchSize = fetchSize;
- }
-
- /**
- * Return the fetch size specified for this HibernateTemplate.
- */
- public int getFetchSize() {
- return this.fetchSize;
- }
-
- /**
- * Set the maximum number of rows for this HibernateTemplate. This is important
- * for processing subsets of large result sets, avoiding to read and hold
- * the entire result set in the database or in the JDBC driver if we're
- * never interested in the entire result in the first place (for example,
- * when performing searches that might return a large number of matches).
- * Default is 0, indicating to use the JDBC driver's default.
- */
- public void setMaxResults(int maxResults) {
- this.maxResults = maxResults;
- }
-
- /**
- * Return the maximum number of rows specified for this HibernateTemplate.
- */
- public int getMaxResults() {
- return this.maxResults;
- }
-
- @Override
- public void afterPropertiesSet() {
- if (getSessionFactory() == null) {
- throw new IllegalArgumentException("Property 'sessionFactory' is required");
- }
- }
-
-
- @Override
- @Nullable
- public This execute variant overrides the template-wide
- * {@link #isExposeNativeSession() "exposeNativeSession"} setting.
- * @param action callback object that specifies the Hibernate action
- * @return a result object returned by the action, or {@code null}
- * @throws DataAccessException in case of Hibernate errors
- */
- @Nullable
- public Default implementation throws an InvalidDataAccessApiUsageException in
- * case of {@code FlushMode.MANUAL}. Can be overridden in subclasses.
- * @param session current Hibernate Session
- * @throws InvalidDataAccessApiUsageException if write operations are not allowed
- * @see #setCheckWriteOperations
- * @see Session#getFlushMode()
- * @see FlushMode#MANUAL
- */
- protected void checkWriteOperationAllowed(Session session) throws InvalidDataAccessApiUsageException {
- if (isCheckWriteOperations() && session.getHibernateFlushMode().lessThan(FlushMode.COMMIT)) {
- throw new InvalidDataAccessApiUsageException(
- "Write operations are not allowed in read-only mode (FlushMode.MANUAL): "+
- "Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition.");
- }
- }
-
- /**
- * Prepare the given Criteria object, applying cache settings and/or
- * a transaction timeout.
- * @param criteria the Criteria object to prepare
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- */
- protected void prepareCriteria(Criteria criteria) {
- if (isCacheQueries()) {
- criteria.setCacheable(true);
- if (getQueryCacheRegion() != null) {
- criteria.setCacheRegion(getQueryCacheRegion());
- }
- }
- if (getFetchSize() > 0) {
- criteria.setFetchSize(getFetchSize());
- }
- if (getMaxResults() > 0) {
- criteria.setMaxResults(getMaxResults());
- }
-
- ResourceHolderSupport sessionHolder =
- (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
- if (sessionHolder != null && sessionHolder.hasTimeout()) {
- criteria.setTimeout(sessionHolder.getTimeToLiveInSeconds());
- }
- }
-
- /**
- * Prepare the given Query object, applying cache settings and/or
- * a transaction timeout.
- * @param queryObject the Query object to prepare
- * @see #setCacheQueries
- * @see #setQueryCacheRegion
- */
- protected void prepareQuery(Query> queryObject) {
- if (isCacheQueries()) {
- queryObject.setCacheable(true);
- if (getQueryCacheRegion() != null) {
- queryObject.setCacheRegion(getQueryCacheRegion());
- }
- }
- if (getFetchSize() > 0) {
- queryObject.setFetchSize(getFetchSize());
- }
- if (getMaxResults() > 0) {
- queryObject.setMaxResults(getMaxResults());
- }
-
- ResourceHolderSupport sessionHolder =
- (ResourceHolderSupport) TransactionSynchronizationManager.getResource(obtainSessionFactory());
- if (sessionHolder != null && sessionHolder.hasTimeout()) {
- queryObject.setTimeout(sessionHolder.getTimeToLiveInSeconds());
- }
- }
-
- /**
- * Apply the given name parameter to the given Query object.
- * @param queryObject the Query object
- * @param paramName the name of the parameter
- * @param value the value of the parameter
- * @throws HibernateException if thrown by the Query object
- */
- protected void applyNamedParameterToQuery(Query> queryObject, String paramName, Object value)
- throws HibernateException {
-
- if (value instanceof Collection> collection) {
- queryObject.setParameterList(paramName, collection);
- }
- else if (value instanceof Object[] array) {
- queryObject.setParameterList(paramName, array);
- }
- else {
- queryObject.setParameter(paramName, value);
- }
- }
-
- private static >) session -> {
- Criteria criteria = session.createCriteria(entityClass);
- criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
- prepareCriteria(criteria);
- return criteria.list();
- }));
- }
-
- @Override
- public void load(Object entity, Serializable id) throws DataAccessException {
- executeWithNativeSession(session -> {
- session.load(entity, id);
- return null;
- });
- }
-
- @Override
- public void refresh(Object entity) throws DataAccessException {
- refresh(entity, null);
- }
-
- @Override
- public void refresh(Object entity, @Nullable LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(session -> {
- if (lockMode != null) {
- session.refresh(entity, new LockOptions(lockMode));
- }
- else {
- session.refresh(entity);
- }
- return null;
- });
- }
-
- @Override
- public boolean contains(Object entity) throws DataAccessException {
- Boolean result = executeWithNativeSession(session -> session.contains(entity));
- Assert.state(result != null, "No contains result");
- return result;
- }
-
- @Override
- public void evict(Object entity) throws DataAccessException {
- executeWithNativeSession(session -> {
- session.evict(entity);
- return null;
- });
- }
-
- @Override
- public void initialize(Object proxy) throws DataAccessException {
- try {
- Hibernate.initialize(proxy);
- }
- catch (HibernateException ex) {
- throw SessionFactoryUtils.convertHibernateAccessException(ex);
- }
- }
-
- @Override
- public Filter enableFilter(String filterName) throws IllegalStateException {
- Session session = obtainSessionFactory().getCurrentSession();
- Filter filter = session.getEnabledFilter(filterName);
- if (filter == null) {
- filter = session.enableFilter(filterName);
- }
- return filter;
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience methods for storing individual objects
- //-------------------------------------------------------------------------
-
- @Override
- public void lock(Object entity, LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(session -> {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entity);
- return null;
- });
- }
-
- @Override
- public void lock(String entityName, Object entity, LockMode lockMode)
- throws DataAccessException {
-
- executeWithNativeSession(session -> {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity);
- return null;
- });
- }
-
- @Override
- public Serializable save(Object entity) throws DataAccessException {
- return nonNull(executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- return session.save(entity);
- }));
- }
-
- @Override
- public Serializable save(String entityName, Object entity) throws DataAccessException {
- return nonNull(executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- return session.save(entityName, entity);
- }));
- }
-
- @Override
- public void update(Object entity) throws DataAccessException {
- update(entity, null);
- }
-
- @Override
- public void update(Object entity, @Nullable LockMode lockMode) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.update(entity);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entity);
- }
- return null;
- });
- }
-
- @Override
- public void update(String entityName, Object entity) throws DataAccessException {
- update(entityName, entity, null);
- }
-
- @Override
- public void update(String entityName, Object entity, @Nullable LockMode lockMode)
- throws DataAccessException {
-
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.update(entityName, entity);
- if (lockMode != null) {
- session.buildLockRequest(new LockOptions(lockMode)).lock(entityName, entity);
- }
- return null;
- });
- }
-
- @Override
- public void saveOrUpdate(Object entity) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.saveOrUpdate(entity);
- return null;
- });
- }
-
- @Override
- public void saveOrUpdate(String entityName, Object entity) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.saveOrUpdate(entityName, entity);
- return null;
- });
- }
-
- @Override
- public void replicate(Object entity, ReplicationMode replicationMode) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.replicate(entity, replicationMode);
- return null;
- });
- }
-
- @Override
- public void replicate(String entityName, Object entity, ReplicationMode replicationMode)
- throws DataAccessException {
-
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.replicate(entityName, entity, replicationMode);
- return null;
- });
- }
-
- @Override
- public void persist(Object entity) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.persist(entity);
- return null;
- });
- }
-
- @Override
- public void persist(String entityName, Object entity) throws DataAccessException {
- executeWithNativeSession(session -> {
- checkWriteOperationAllowed(session);
- session.persist(entityName, entity);
- return null;
- });
- }
-
- @Override
- @SuppressWarnings("unchecked")
- public
>) session -> {
- Criteria executableCriteria = criteria.getExecutableCriteria(session);
- prepareCriteria(executableCriteria);
- if (firstResult >= 0) {
- executableCriteria.setFirstResult(firstResult);
- }
- if (maxResults > 0) {
- executableCriteria.setMaxResults(maxResults);
- }
- return executableCriteria.list();
- }));
- }
-
- @Override
- public
>) session -> {
- Criteria executableCriteria = (entityName != null ?
- session.createCriteria(entityName) : session.createCriteria(exampleEntity.getClass()));
- executableCriteria.add(Example.create(exampleEntity));
- prepareCriteria(executableCriteria);
- if (firstResult >= 0) {
- executableCriteria.setFirstResult(firstResult);
- }
- if (maxResults > 0) {
- executableCriteria.setMaxResults(maxResults);
- }
- return executableCriteria.list();
- }));
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for HQL strings
- //-------------------------------------------------------------------------
-
- @Deprecated
- @Override
- public List> find(String queryString, @Nullable Object... values) throws DataAccessException {
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.list();
- }));
- }
-
- @Deprecated
- @Override
- public List> findByNamedParam(String queryString, String paramName, Object value)
- throws DataAccessException {
-
- return findByNamedParam(queryString, new String[] {paramName}, new Object[] {value});
- }
-
- @Deprecated
- @Override
- public List> findByNamedParam(String queryString, String[] paramNames, Object[] values)
- throws DataAccessException {
-
- if (paramNames.length != values.length) {
- throw new IllegalArgumentException("Length of paramNames array must match length of values array");
- }
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- for (int i = 0; i < values.length; i++) {
- applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
- }
- return queryObject.list();
- }));
- }
-
- @Deprecated
- @Override
- public List> findByValueBean(String queryString, Object valueBean) throws DataAccessException {
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.createQuery(queryString);
- prepareQuery(queryObject);
- queryObject.setProperties(valueBean);
- return queryObject.list();
- }));
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience finder methods for named queries
- //-------------------------------------------------------------------------
-
- @Deprecated
- @Override
- public List> findByNamedQuery(String queryName, @Nullable Object... values) throws DataAccessException {
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- queryObject.setParameter(i, values[i]);
- }
- }
- return queryObject.list();
- }));
- }
-
- @Deprecated
- @Override
- public List> findByNamedQueryAndNamedParam(String queryName, String paramName, Object value)
- throws DataAccessException {
-
- return findByNamedQueryAndNamedParam(queryName, new String[] {paramName}, new Object[] {value});
- }
-
- @Deprecated
- @Override
- @SuppressWarnings("NullAway")
- public List> findByNamedQueryAndNamedParam(
- String queryName, @Nullable String[] paramNames, @Nullable Object[] values)
- throws DataAccessException {
-
- if (values != null && (paramNames == null || paramNames.length != values.length)) {
- throw new IllegalArgumentException("Length of paramNames array must match length of values array");
- }
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- if (values != null) {
- for (int i = 0; i < values.length; i++) {
- applyNamedParameterToQuery(queryObject, paramNames[i], values[i]);
- }
- }
- return queryObject.list();
- }));
- }
-
- @Deprecated
- @Override
- public List> findByNamedQueryAndValueBean(String queryName, Object valueBean) throws DataAccessException {
- return nonNull(executeWithNativeSession((HibernateCallback
>) session -> {
- Query> queryObject = session.getNamedQuery(queryName);
- prepareQuery(queryObject);
- queryObject.setProperties(valueBean);
- return queryObject.list();
- }));
- }
-
-
- //-------------------------------------------------------------------------
- // Convenience query methods for iteration and bulk updates/deletes
- //-------------------------------------------------------------------------
-
- @SuppressWarnings("deprecation")
- @Deprecated
- @Override
- public Iterator> iterate(String queryString, @Nullable Object... values) throws DataAccessException {
- return nonNull(executeWithNativeSession((HibernateCallback