44 */
55package org .hibernate .jpa ;
66
7+ import java .net .URL ;
8+ import java .util .ArrayList ;
79import java .util .Collection ;
810import java .util .Collections ;
11+ import java .util .List ;
912import java .util .Map ;
1013import java .util .Objects ;
1114
1720import org .hibernate .cfg .JdbcSettings ;
1821import org .hibernate .cfg .JpaComplianceSettings ;
1922import org .hibernate .cfg .MappingSettings ;
23+ import org .hibernate .cfg .PersistenceSettings ;
2024import org .hibernate .cfg .SchemaToolingSettings ;
2125import org .hibernate .cfg .StatisticsSettings ;
2226import org .hibernate .resource .jdbc .spi .StatementInspector ;
6771 * <li>properties specified in {@code hibernate.properties}.
6872 * </ul>
6973 * <p>
70- * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
71- * entity discovery} is explicitly enabled, and {@code hibernate-scan-jandex}
72- * or some other {@link org.hibernate.boot.archive.scan.spi.ScannerFactory}
73- * service is available, the current working directly is scanned for entity
74- * classes, alleviating the program of the need to call {@link #managedClass}.
74+ * When a {@linkplain #rootUrl() root URL} is supplied, or when at least
75+ * one {@linkplain #jarFileUrls() JAR file URL} is supplied, and when
76+ * {@code hibernate-scan-jandex} or some other service implementing
77+ * {@link org.hibernate.boot.archive.scan.spi.ScannerFactory} is available,
78+ * the current working directly is scanned for entity classes, alleviating
79+ * the program of the need to call {@link #managedClass}.
7580 *
7681 * @apiNote The specification explicitly encourages implementors to extend
7782 * {@link PersistenceConfiguration} to accommodate vendor-specific
8489 * @since 7.0
8590 */
8691public class HibernatePersistenceConfiguration extends PersistenceConfiguration {
92+
93+ private URL rootUrl ;
94+ private final List <URL > jarFileUrls = new ArrayList <>();
95+
8796 /**
8897 * Create a new empty configuration. An empty configuration does not
8998 * typically hold enough information for successful invocation of
@@ -96,6 +105,49 @@ public HibernatePersistenceConfiguration(String name) {
96105 super ( name );
97106 }
98107
108+ /**
109+ * Create a new empty configuration with a given {@linkplain #rootUrl root URL}
110+ * used for {@linkplain PersistenceSettings#SCANNER_DISCOVERY entity discovery}
111+ * via scanning.
112+ * <p>
113+ * The module {@code hibernate-scan-jandex} must be added as a dependency,
114+ * or some other implementation of the service
115+ * {@link org.hibernate.boot.archive.scan.spi.ScannerFactory} must be made
116+ * available.
117+ *
118+ * @param name the name of the persistence unit, which may be used by
119+ * the persistence provider for logging and error reporting
120+ * @param rootURL the root URL of the persistence unit
121+ *
122+ * @since 7.1
123+ */
124+ public HibernatePersistenceConfiguration (String name , URL rootURL ) {
125+ super ( name );
126+ this .rootUrl = rootURL ;
127+ }
128+
129+ /**
130+ * Create a new empty configuration with the {@linkplain #rootUrl root URL}
131+ * inferred from the given class file and used for
132+ * {@linkplain PersistenceSettings#SCANNER_DISCOVERY entity discovery}
133+ * via scanning.
134+ * <p>
135+ * The module {@code hibernate-scan-jandex} must be added as a dependency,
136+ * or some other implementation of the service
137+ * {@link org.hibernate.boot.archive.scan.spi.ScannerFactory} must be made
138+ * available.
139+ *
140+ * @param name the name of the persistence unit, which may be used by
141+ * the persistence provider for logging and error reporting
142+ * @param classFromRootUrl a class loaded from the root URL of the
143+ * persistence unit
144+ *
145+ * @since 7.1
146+ */
147+ public HibernatePersistenceConfiguration (String name , Class <?> classFromRootUrl ) {
148+ this ( name , classFromRootUrl .getProtectionDomain ().getCodeSource ().getLocation () );
149+ }
150+
99151 /**
100152 * Create a new {@link SessionFactory} based on this configuration.
101153 */
@@ -464,7 +516,7 @@ public HibernatePersistenceConfiguration managedClasses(Collection<Class<?>> man
464516 /**
465517 * Add the specified resource names as {@linkplain #mappingFiles() mapping files}.
466518 *
467- * @see #mappingFiles
519+ * @see #mappingFiles()
468520 */
469521 public HibernatePersistenceConfiguration mappingFiles (String ... names ) {
470522 Collections .addAll ( mappingFiles (), names );
@@ -474,13 +526,43 @@ public HibernatePersistenceConfiguration mappingFiles(String... names) {
474526 /**
475527 * Add the specified resource names as {@linkplain #mappingFiles() mapping files}.
476528 *
477- * @see #mappingFiles
529+ * @see #mappingFiles()
478530 */
479531 public HibernatePersistenceConfiguration mappingFiles (Collection <String > names ) {
480532 mappingFiles ().addAll ( names );
481533 return this ;
482534 }
483535
536+ /**
537+ * Add the specified URL as a {@linkplain #jarFileUrls() JAR file}.
538+ *
539+ * @see #jarFileUrls()
540+ */
541+ public HibernatePersistenceConfiguration jarFileUrl (URL url ) {
542+ jarFileUrls .add ( url );
543+ return this ;
544+ }
545+
546+ /**
547+ * Add the specified URLs as {@linkplain #jarFileUrls() JAR files}.
548+ *
549+ * @see #jarFileUrls()
550+ */
551+ public HibernatePersistenceConfiguration jarFileUrls (URL ... urls ) {
552+ Collections .addAll ( jarFileUrls , urls );
553+ return this ;
554+ }
555+
556+ /**
557+ * Add the specified URLs as {@linkplain #jarFileUrls() JAR files}.
558+ *
559+ * @see #jarFileUrls()
560+ */
561+ public HibernatePersistenceConfiguration jarFileUrls (Collection <URL > urls ) {
562+ jarFileUrls .addAll ( urls );
563+ return this ;
564+ }
565+
484566 /**
485567 * Specify the {@linkplain Action action} to take in terms of automatic
486568 * database schema tooling.
@@ -549,4 +631,32 @@ public HibernatePersistenceConfiguration property(String name, Object value) {
549631 public HibernatePersistenceConfiguration properties (Map <String , ?> properties ) {
550632 return (HibernatePersistenceConfiguration ) super .properties ( properties );
551633 }
634+
635+ /**
636+ * URLs of JAR files.
637+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
638+ * entity discovery} is enabled, the JAR files will be scanned for entities.
639+ *
640+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
641+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getJarFileUrls
642+ *
643+ * @since 7.1
644+ */
645+ public List <URL > jarFileUrls () {
646+ return jarFileUrls ;
647+ }
648+
649+ /**
650+ * Root URL of the persistence unit.
651+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
652+ * entity discovery} is enabled, this root URL will be scanned for entities.
653+ *
654+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
655+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getPersistenceUnitRootUrl
656+ *
657+ * @since 7.1
658+ */
659+ public URL rootUrl () {
660+ return rootUrl ;
661+ }
552662}
0 commit comments