Skip to content

Commit 9ea3aed

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

File tree

2 files changed

+119
-13
lines changed

2 files changed

+119
-13
lines changed

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

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

7+
import java.net.URL;
8+
import java.util.ArrayList;
79
import java.util.Collection;
810
import java.util.Collections;
11+
import java.util.List;
912
import java.util.Map;
1013
import java.util.Objects;
1114

@@ -17,6 +20,7 @@
1720
import org.hibernate.cfg.JdbcSettings;
1821
import org.hibernate.cfg.JpaComplianceSettings;
1922
import org.hibernate.cfg.MappingSettings;
23+
import org.hibernate.cfg.PersistenceSettings;
2024
import org.hibernate.cfg.SchemaToolingSettings;
2125
import org.hibernate.cfg.StatisticsSettings;
2226
import org.hibernate.resource.jdbc.spi.StatementInspector;
@@ -84,6 +88,10 @@
8488
* @since 7.0
8589
*/
8690
public 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
}

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)