44 */
55package org .hibernate .jpa ;
66
7+ import java .net .URL ;
78import java .util .Collection ;
89import java .util .Collections ;
10+ import java .util .List ;
911import java .util .Map ;
1012import java .util .Objects ;
1113
1719import org .hibernate .cfg .JdbcSettings ;
1820import org .hibernate .cfg .JpaComplianceSettings ;
1921import org .hibernate .cfg .MappingSettings ;
22+ import org .hibernate .cfg .PersistenceSettings ;
2023import org .hibernate .cfg .SchemaToolingSettings ;
2124import org .hibernate .cfg .StatisticsSettings ;
2225import org .hibernate .resource .jdbc .spi .StatementInspector ;
8487 * @since 7.0
8588 */
8689public class HibernatePersistenceConfiguration extends PersistenceConfiguration {
90+
91+ private URL rootUrl ;
92+ private List <URL > jarFileUrls ;
93+
8794 /**
8895 * Create a new empty configuration. An empty configuration does not
8996 * typically hold enough information for successful invocation of
@@ -96,6 +103,36 @@ public HibernatePersistenceConfiguration(String name) {
96103 super ( name );
97104 }
98105
106+ /**
107+ * Create a new empty configuration with a given {@link #rootUrl}.
108+ *
109+ * @param name the name of the persistence unit, which may be used by
110+ * the persistence provider for logging and error reporting
111+ * @param rootURL the root URL of the persistence unit
112+ *
113+ * @since 7.1
114+ */
115+ public HibernatePersistenceConfiguration (String name , URL rootURL ) {
116+ super ( name );
117+ this .rootUrl = rootURL ;
118+ property ( PersistenceSettings .SCANNER_DISCOVERY , "class" );
119+ }
120+
121+ /**
122+ * Create a new empty configuration with the {@link #rootUrl} inferred
123+ * from the given class file.
124+ *
125+ * @param name the name of the persistence unit, which may be used by
126+ * the persistence provider for logging and error reporting
127+ * @param classFromRootUrl a class loaded from the root URL of the
128+ * persistence unit
129+ *
130+ * @since 7.1
131+ */
132+ public HibernatePersistenceConfiguration (String name , Class <?> classFromRootUrl ) {
133+ this ( name , classFromRootUrl .getProtectionDomain ().getCodeSource ().getLocation () );
134+ }
135+
99136 /**
100137 * Create a new {@link SessionFactory} based on this configuration.
101138 */
@@ -481,6 +518,36 @@ public HibernatePersistenceConfiguration mappingFiles(Collection<String> names)
481518 return this ;
482519 }
483520
521+ /**
522+ * Add the specified URL as a {@linkplain #jarFileUrls() JAR file}.
523+ *
524+ * @see #mappingFiles
525+ */
526+ public HibernatePersistenceConfiguration jarFileUrl (URL url ) {
527+ jarFileUrls .add ( url );
528+ return this ;
529+ }
530+
531+ /**
532+ * Add the specified URLs as {@linkplain #jarFileUrls() JAR files}.
533+ *
534+ * @see #mappingFiles
535+ */
536+ public HibernatePersistenceConfiguration jarFileUrls (URL ... urls ) {
537+ Collections .addAll ( jarFileUrls , urls );
538+ return this ;
539+ }
540+
541+ /**
542+ * Add the specified URLs as {@linkplain #jarFileUrls() JAR files}.
543+ *
544+ * @see #mappingFiles
545+ */
546+ public HibernatePersistenceConfiguration jarFileUrls (Collection <URL > urls ) {
547+ jarFileUrls .addAll ( urls );
548+ return this ;
549+ }
550+
484551 /**
485552 * Specify the {@linkplain Action action} to take in terms of automatic
486553 * database schema tooling.
@@ -549,4 +616,32 @@ public HibernatePersistenceConfiguration property(String name, Object value) {
549616 public HibernatePersistenceConfiguration properties (Map <String , ?> properties ) {
550617 return (HibernatePersistenceConfiguration ) super .properties ( properties );
551618 }
619+
620+ /**
621+ * URLs of JAR files.
622+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
623+ * entity discovery} is enabled, the JAR files will be scanned for entities.
624+ *
625+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
626+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getJarFileUrls
627+ *
628+ * @since 7.1
629+ */
630+ public List <URL > jarFileUrls () {
631+ return jarFileUrls ;
632+ }
633+
634+ /**
635+ * Root URL of the persistence unit.
636+ * When {@linkplain org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
637+ * entity discovery} is enabled, this root URL will be scanned for entities.
638+ *
639+ * @see org.hibernate.cfg.PersistenceSettings#SCANNER_DISCOVERY
640+ * @see jakarta.persistence.spi.PersistenceUnitInfo#getPersistenceUnitRootUrl
641+ *
642+ * @since 7.1
643+ */
644+ public URL rootUrl () {
645+ return rootUrl ;
646+ }
552647}
0 commit comments