31
31
import jakarta .persistence .SharedCacheMode ;
32
32
import jakarta .persistence .ValidationMode ;
33
33
import jakarta .persistence .spi .PersistenceUnitInfo ;
34
+ import jakarta .validation .NoProviderFoundException ;
35
+ import jakarta .validation .Validation ;
34
36
import org .apache .commons .logging .Log ;
35
37
import org .apache .commons .logging .LogFactory ;
36
38
48
50
import org .springframework .jdbc .datasource .lookup .JndiDataSourceLookup ;
49
51
import org .springframework .jdbc .datasource .lookup .MapDataSourceLookup ;
50
52
import org .springframework .lang .Nullable ;
53
+ import org .springframework .util .ClassUtils ;
51
54
import org .springframework .util .ObjectUtils ;
52
55
import org .springframework .util .ResourceUtils ;
53
56
@@ -100,6 +103,9 @@ public class DefaultPersistenceUnitManager
100
103
public static final String ORIGINAL_DEFAULT_PERSISTENCE_UNIT_NAME = "default" ;
101
104
102
105
106
+ private static final boolean beanValidationPresent = ClassUtils .isPresent (
107
+ "jakarta.validation.Validation" , DefaultPersistenceUnitManager .class .getClassLoader ());
108
+
103
109
protected final Log logger = LogFactory .getLog (getClass ());
104
110
105
111
private String [] persistenceXmlLocations = new String [] {DEFAULT_PERSISTENCE_XML_LOCATION };
@@ -449,7 +455,7 @@ public void preparePersistenceUnitInfos() {
449
455
pui .setPersistenceUnitRootUrl (determineDefaultPersistenceUnitRootUrl ());
450
456
}
451
457
452
- // Override DataSource and cache/validation mode
458
+ // Override DataSource and shared cache mode
453
459
if (pui .getJtaDataSource () == null && this .defaultJtaDataSource != null ) {
454
460
pui .setJtaDataSource (this .defaultJtaDataSource );
455
461
}
@@ -459,9 +465,16 @@ public void preparePersistenceUnitInfos() {
459
465
if (this .sharedCacheMode != null ) {
460
466
pui .setSharedCacheMode (this .sharedCacheMode );
461
467
}
468
+
469
+ // Override validation mode or pre-resolve provider detection
462
470
if (this .validationMode != null ) {
463
471
pui .setValidationMode (this .validationMode );
464
472
}
473
+ else if (pui .getValidationMode () == ValidationMode .AUTO ) {
474
+ pui .setValidationMode (
475
+ beanValidationPresent && BeanValidationDelegate .isValidationProviderPresent () ?
476
+ ValidationMode .CALLBACK : ValidationMode .NONE );
477
+ }
465
478
466
479
// Initialize persistence unit ClassLoader
467
480
if (this .loadTimeWeaver != null ) {
@@ -697,4 +710,21 @@ public PersistenceUnitInfo obtainPersistenceUnitInfo(String persistenceUnitName)
697
710
return pui ;
698
711
}
699
712
713
+
714
+ /**
715
+ * Inner class to avoid a hard dependency on the Bean Validation API at runtime.
716
+ */
717
+ private static class BeanValidationDelegate {
718
+
719
+ public static boolean isValidationProviderPresent () {
720
+ try {
721
+ Validation .byDefaultProvider ().configure ();
722
+ return true ;
723
+ }
724
+ catch (NoProviderFoundException ex ) {
725
+ return false ;
726
+ }
727
+ }
728
+ }
729
+
700
730
}
0 commit comments