Skip to content

Commit 4e2856b

Browse files
committed
HHH-16548 more flexible / robust way to specify root URL and jar file URLs for PersistenceConfiguration
1 parent 9d958d7 commit 4e2856b

File tree

2 files changed

+102
-11
lines changed

2 files changed

+102
-11
lines changed

hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceConfiguration.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
*/
55
package org.hibernate.jpa;
66

7+
import java.net.URL;
78
import java.util.Collection;
89
import java.util.Collections;
10+
import java.util.List;
911
import java.util.Map;
1012
import java.util.Objects;
1113

@@ -17,6 +19,7 @@
1719
import org.hibernate.cfg.JdbcSettings;
1820
import org.hibernate.cfg.JpaComplianceSettings;
1921
import org.hibernate.cfg.MappingSettings;
22+
import org.hibernate.cfg.PersistenceSettings;
2023
import org.hibernate.cfg.SchemaToolingSettings;
2124
import org.hibernate.cfg.StatisticsSettings;
2225
import org.hibernate.resource.jdbc.spi.StatementInspector;
@@ -84,6 +87,10 @@
8487
* @since 7.0
8588
*/
8689
public 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
}

hibernate-core/src/main/java/org/hibernate/jpa/boot/spi/PersistenceConfigurationDescriptor.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
package org.hibernate.jpa.boot.spi;
66

7-
import java.io.File;
8-
import java.net.MalformedURLException;
97
import java.net.URL;
108
import java.util.List;
119
import java.util.Properties;
@@ -14,6 +12,7 @@
1412
import org.hibernate.bytecode.spi.ClassTransformer;
1513
import org.hibernate.cfg.AvailableSettings;
1614
import org.hibernate.internal.util.collections.CollectionHelper;
15+
import org.hibernate.jpa.HibernatePersistenceConfiguration;
1716
import org.hibernate.jpa.HibernatePersistenceProvider;
1817

1918
import jakarta.persistence.PersistenceConfiguration;
@@ -127,18 +126,15 @@ public ClassTransformer getClassTransformer() {
127126

128127
@Override
129128
public URL getPersistenceUnitRootUrl() {
130-
// When hibernate.archive.autodetection=class
131-
// scan the CWD for entities (useful for tests)
132-
try {
133-
return new File( System.getProperty("user.dir") ).toURI().toURL();
134-
}
135-
catch ( MalformedURLException e ) {
136-
return null;
137-
}
129+
return persistenceConfiguration instanceof HibernatePersistenceConfiguration configuration
130+
? configuration.rootUrl()
131+
: null;
138132
}
139133

140134
@Override
141135
public List<URL> getJarFileUrls() {
142-
return null;
136+
return persistenceConfiguration instanceof HibernatePersistenceConfiguration configuration
137+
? configuration.jarFileUrls()
138+
: null;
143139
}
144140
}

0 commit comments

Comments
 (0)