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 ;
8488 * @since 7.0
8589 */
8690public class HibernatePersistenceConfiguration extends PersistenceConfiguration {
91+
92+ private URL rootUrl ;
93+ private final List <URL > jarFileUrls = new ArrayList <>();
94+
8795 /**
8896 * Create a new empty configuration. An empty configuration does not
8997 * typically hold enough information for successful invocation of
@@ -96,6 +104,50 @@ public HibernatePersistenceConfiguration(String name) {
96104 super ( name );
97105 }
98106
107+ /**
108+ * Create a new empty configuration with a given {@linkplain #rootUrl root URL}
109+ * and with {@linkplain PersistenceSettings#SCANNER_DISCOVERY entity discovery}
110+ * via scanning enabled.
111+ * <p>
112+ * The module {@code hibernate-scan-jandex} must be added as a dependency,
113+ * or some other implementation of the service
114+ * {@link org.hibernate.boot.archive.scan.spi.ScannerFactory} must be made
115+ * available.
116+ *
117+ * @param name the name of the persistence unit, which may be used by
118+ * the persistence provider for logging and error reporting
119+ * @param rootURL the root URL of the persistence unit
120+ *
121+ * @since 7.1
122+ */
123+ public HibernatePersistenceConfiguration (String name , URL rootURL ) {
124+ super ( name );
125+ this .rootUrl = rootURL ;
126+ property ( PersistenceSettings .SCANNER_DISCOVERY , "class" );
127+ }
128+
129+ /**
130+ * Create a new empty configuration with the {@linkplain #rootUrl root URL}
131+ * inferred from the given class file and with
132+ * {@linkplain PersistenceSettings#SCANNER_DISCOVERY entity discovery}
133+ * via scanning enabled.
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