Skip to content

Commit f2026b6

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

File tree

2 files changed

+109
-13
lines changed

2 files changed

+109
-13
lines changed

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

Lines changed: 102 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,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
}

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)