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,40 @@ public HibernatePersistenceConfiguration(String name) {
96104 super ( name );
97105 }
98106
107+ /**
108+ * Create a new empty configuration with a given {@link #rootUrl}
109+ * and with {@linkplain PersistenceSettings#SCANNER_DISCOVERY
110+ * entity discovery} via scanning enabled.
111+ *
112+ * @param name the name of the persistence unit, which may be used by
113+ * the persistence provider for logging and error reporting
114+ * @param rootURL the root URL of the persistence unit
115+ *
116+ * @since 7.1
117+ */
118+ public HibernatePersistenceConfiguration (String name , URL rootURL ) {
119+ super ( name );
120+ this .rootUrl = rootURL ;
121+ property ( PersistenceSettings .SCANNER_DISCOVERY , "class" );
122+ }
123+
124+ /**
125+ * Create a new empty configuration with the {@link #rootUrl} inferred
126+ * from the given class file and with
127+ * {@linkplain PersistenceSettings#SCANNER_DISCOVERY entity discovery}
128+ * via scanning enabled.
129+ *
130+ * @param name the name of the persistence unit, which may be used by
131+ * the persistence provider for logging and error reporting
132+ * @param classFromRootUrl a class loaded from the root URL of the
133+ * persistence unit
134+ *
135+ * @since 7.1
136+ */
137+ public HibernatePersistenceConfiguration (String name , Class <?> classFromRootUrl ) {
138+ this ( name , classFromRootUrl .getProtectionDomain ().getCodeSource ().getLocation () );
139+ }
140+
99141 /**
100142 * Create a new {@link SessionFactory} based on this configuration.
101143 */
@@ -464,7 +506,7 @@ public HibernatePersistenceConfiguration managedClasses(Collection<Class<?>> man
464506 /**
465507 * Add the specified resource names as {@linkplain #mappingFiles() mapping files}.
466508 *
467- * @see #mappingFiles
509+ * @see #mappingFiles()
468510 */
469511 public HibernatePersistenceConfiguration mappingFiles (String ... names ) {
470512 Collections .addAll ( mappingFiles (), names );
@@ -474,13 +516,43 @@ public HibernatePersistenceConfiguration mappingFiles(String... names) {
474516 /**
475517 * Add the specified resource names as {@linkplain #mappingFiles() mapping files}.
476518 *
477- * @see #mappingFiles
519+ * @see #mappingFiles()
478520 */
479521 public HibernatePersistenceConfiguration mappingFiles (Collection <String > names ) {
480522 mappingFiles ().addAll ( names );
481523 return this ;
482524 }
483525
526+ /**
527+ * Add the specified URL as a {@linkplain #jarFileUrls() JAR file}.
528+ *
529+ * @see #jarFileUrls()
530+ */
531+ public HibernatePersistenceConfiguration jarFileUrl (URL url ) {
532+ jarFileUrls .add ( url );
533+ return this ;
534+ }
535+
536+ /**
537+ * Add the specified URLs as {@linkplain #jarFileUrls() JAR files}.
538+ *
539+ * @see #jarFileUrls()
540+ */
541+ public HibernatePersistenceConfiguration jarFileUrls (URL ... urls ) {
542+ Collections .addAll ( jarFileUrls , urls );
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 (Collection <URL > urls ) {
552+ jarFileUrls .addAll ( urls );
553+ return this ;
554+ }
555+
484556 /**
485557 * Specify the {@linkplain Action action} to take in terms of automatic
486558 * database schema tooling.
@@ -549,4 +621,32 @@ public HibernatePersistenceConfiguration property(String name, Object value) {
549621 public HibernatePersistenceConfiguration properties (Map <String , ?> properties ) {
550622 return (HibernatePersistenceConfiguration ) super .properties ( properties );
551623 }
624+
625+ /**
626+ * URLs of JAR files.
627+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
628+ * entity discovery} is enabled, the JAR files will be scanned for entities.
629+ *
630+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
631+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getJarFileUrls
632+ *
633+ * @since 7.1
634+ */
635+ public List <URL > jarFileUrls () {
636+ return jarFileUrls ;
637+ }
638+
639+ /**
640+ * Root URL of the persistence unit.
641+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
642+ * entity discovery} is enabled, this root URL will be scanned for entities.
643+ *
644+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
645+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getPersistenceUnitRootUrl
646+ *
647+ * @since 7.1
648+ */
649+ public URL rootUrl () {
650+ return rootUrl ;
651+ }
552652}
0 commit comments