|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html. |
| 6 | + */ |
| 7 | +package org.hibernate.jpa; |
| 8 | + |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +import org.hibernate.SessionFactory; |
| 13 | +import org.hibernate.cache.spi.access.AccessType; |
| 14 | +import org.hibernate.cfg.CacheSettings; |
| 15 | +import org.hibernate.cfg.JdbcSettings; |
| 16 | +import org.hibernate.cfg.JpaComplianceSettings; |
| 17 | +import org.hibernate.cfg.MappingSettings; |
| 18 | +import org.hibernate.cfg.StatisticsSettings; |
| 19 | +import org.hibernate.resource.jdbc.spi.StatementInspector; |
| 20 | + |
| 21 | +import jakarta.persistence.EntityManager; |
| 22 | +import jakarta.persistence.EntityManagerFactory; |
| 23 | +import jakarta.persistence.PersistenceConfiguration; |
| 24 | +import jakarta.persistence.PersistenceUnitTransactionType; |
| 25 | +import jakarta.persistence.SharedCacheMode; |
| 26 | +import jakarta.persistence.ValidationMode; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Steve Ebersole |
| 30 | + */ |
| 31 | +public class HibernatePersistenceConfiguration extends PersistenceConfiguration { |
| 32 | + /** |
| 33 | + * Create a new empty configuration. An empty configuration does not |
| 34 | + * typically hold enough information for successful invocation of |
| 35 | + * {@link #createEntityManagerFactory()}. |
| 36 | + * |
| 37 | + * @param name the name of the persistence unit, which may be used by |
| 38 | + * the persistence provider for logging and error reporting |
| 39 | + */ |
| 40 | + public HibernatePersistenceConfiguration(String name) { |
| 41 | + super( name ); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public SessionFactory createEntityManagerFactory() { |
| 46 | + return (SessionFactory) super.createEntityManagerFactory(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Name of the JDBC driver to use for non-Datasource connection |
| 51 | + * |
| 52 | + * @see #JDBC_DRIVER |
| 53 | + */ |
| 54 | + public HibernatePersistenceConfiguration jdbcDriver(String driverName) { |
| 55 | + property( JDBC_DRIVER, driverName ); |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * URL to use for non-Datasource JDBC connection |
| 61 | + * |
| 62 | + * @see #JDBC_URL |
| 63 | + */ |
| 64 | + public HibernatePersistenceConfiguration jdbcUrl(String url) { |
| 65 | + property( JDBC_URL, url ); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * User-name to use for non-Datasource JDBC connection |
| 71 | + * |
| 72 | + * @see #JDBC_USER |
| 73 | + * @see #jdbcPassword |
| 74 | + */ |
| 75 | + public HibernatePersistenceConfiguration jdbcUsername(String username) { |
| 76 | + property( JDBC_USER, username ); |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * User-name to use for non-Datasource JDBC connection |
| 82 | + * |
| 83 | + * @see #JDBC_PASSWORD |
| 84 | + * @see #jdbcUsername |
| 85 | + */ |
| 86 | + public HibernatePersistenceConfiguration jdbcPassword(String password) { |
| 87 | + property( JDBC_PASSWORD, password ); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 93 | + * all aspects of {@linkplain jakarta.persistence.Query} handling. |
| 94 | + * |
| 95 | + * @see JpaComplianceSettings#JPA_QUERY_COMPLIANCE |
| 96 | + */ |
| 97 | + public HibernatePersistenceConfiguration queryCompliance(boolean enabled) { |
| 98 | + property( JpaComplianceSettings.JPA_QUERY_COMPLIANCE, enabled ); |
| 99 | + return this; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 104 | + * all aspects of transaction handling. |
| 105 | + * |
| 106 | + * @see JpaComplianceSettings#JPA_TRANSACTION_COMPLIANCE |
| 107 | + */ |
| 108 | + public HibernatePersistenceConfiguration transactionCompliance(boolean enabled) { |
| 109 | + property( JpaComplianceSettings.JPA_TRANSACTION_COMPLIANCE, enabled ); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 115 | + * handling around calls to {@linkplain EntityManager#close()}, |
| 116 | + * {@linkplain EntityManager#isOpen()}, |
| 117 | + * {@linkplain EntityManagerFactory#close()} and |
| 118 | + * {@linkplain EntityManagerFactory#isOpen()} |
| 119 | + * |
| 120 | + * @see JpaComplianceSettings#JPA_CLOSED_COMPLIANCE |
| 121 | + */ |
| 122 | + public HibernatePersistenceConfiguration closedCompliance(boolean enabled) { |
| 123 | + property( JpaComplianceSettings.JPA_CLOSED_COMPLIANCE, enabled ); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 129 | + * handling of proxies. |
| 130 | + * |
| 131 | + * @see JpaComplianceSettings#JPA_PROXY_COMPLIANCE |
| 132 | + */ |
| 133 | + public HibernatePersistenceConfiguration proxyCompliance(boolean enabled) { |
| 134 | + property( JpaComplianceSettings.JPA_PROXY_COMPLIANCE, enabled ); |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 140 | + * handling of proxies. |
| 141 | + * |
| 142 | + * @see JpaComplianceSettings#JPA_PROXY_COMPLIANCE |
| 143 | + */ |
| 144 | + public HibernatePersistenceConfiguration cachingCompliance(boolean enabled) { |
| 145 | + property( JpaComplianceSettings.JPA_PROXY_COMPLIANCE, enabled ); |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 151 | + * in terms of collecting all named value generators globally, regardless of location. |
| 152 | + * |
| 153 | + * @see JpaComplianceSettings#JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE |
| 154 | + */ |
| 155 | + public HibernatePersistenceConfiguration globalGeneratorCompliance(boolean enabled) { |
| 156 | + property( JpaComplianceSettings.JPA_ID_GENERATOR_GLOBAL_SCOPE_COMPLIANCE, enabled ); |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 162 | + * the interpretation of {@link jakarta.persistence.OrderBy}. |
| 163 | + * |
| 164 | + * @see JpaComplianceSettings#JPA_ORDER_BY_MAPPING_COMPLIANCE |
| 165 | + */ |
| 166 | + public HibernatePersistenceConfiguration orderByMappingCompliance(boolean enabled) { |
| 167 | + property( JpaComplianceSettings.JPA_ORDER_BY_MAPPING_COMPLIANCE, enabled ); |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Defines whether Hibernate will strictly adhere to compliance with Jakarta Persistence for |
| 173 | + * the allowed type of identifier value passed to |
| 174 | + * {@link jakarta.persistence.EntityManager#getReference} and |
| 175 | + * {@link jakarta.persistence.EntityManager#find} |
| 176 | + * |
| 177 | + * @see JpaComplianceSettings#JPA_LOAD_BY_ID_COMPLIANCE |
| 178 | + */ |
| 179 | + public HibernatePersistenceConfiguration loadByIdCompliance(boolean enabled) { |
| 180 | + property( JpaComplianceSettings.JPA_LOAD_BY_ID_COMPLIANCE, enabled ); |
| 181 | + return this; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Enable/disable Hibernate's caching support |
| 186 | + */ |
| 187 | + public HibernatePersistenceConfiguration caching(CachingType type) { |
| 188 | + assert Objects.nonNull( type ); |
| 189 | + if ( type == CachingType.NONE || type == CachingType.AUTO ) { |
| 190 | + property( CacheSettings.USE_SECOND_LEVEL_CACHE, false ); |
| 191 | + property( CacheSettings.USE_QUERY_CACHE, false ); |
| 192 | + } |
| 193 | + else if ( type == CachingType.BOTH ) { |
| 194 | + property( CacheSettings.USE_SECOND_LEVEL_CACHE, true ); |
| 195 | + property( CacheSettings.USE_QUERY_CACHE, true ); |
| 196 | + } |
| 197 | + else if ( type == CachingType.DATA ) { |
| 198 | + property( CacheSettings.USE_SECOND_LEVEL_CACHE, true ); |
| 199 | + property( CacheSettings.USE_QUERY_CACHE, false ); |
| 200 | + } |
| 201 | + else if ( type == CachingType.QUERY ) { |
| 202 | + property( CacheSettings.USE_SECOND_LEVEL_CACHE, false ); |
| 203 | + property( CacheSettings.USE_QUERY_CACHE, true ); |
| 204 | + } |
| 205 | + |
| 206 | + return this; |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * If {@linkplain CachingType#DATA data caching} is enabled, configure the |
| 211 | + * type of concurrency access that should be applied if not explicitly specified |
| 212 | + * on a cache region. |
| 213 | + * |
| 214 | + * @see org.hibernate.annotations.Cache#usage |
| 215 | + * @see CacheSettings#DEFAULT_CACHE_CONCURRENCY_STRATEGY |
| 216 | + */ |
| 217 | + public HibernatePersistenceConfiguration cachingAccessType(AccessType type) { |
| 218 | + // todo (7.0) : should this enable second-level cache if not? |
| 219 | + property( CacheSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, type ); |
| 220 | + return this; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Specify a {@linkplain StatementInspector} to be applied to all Sessions/EntityManagers |
| 225 | + * |
| 226 | + * @see JdbcSettings#STATEMENT_INSPECTOR |
| 227 | + */ |
| 228 | + public HibernatePersistenceConfiguration statementInspector(Class<? extends StatementInspector> inspectorImpl) { |
| 229 | + property( JdbcSettings.STATEMENT_INSPECTOR, inspectorImpl ); |
| 230 | + return this; |
| 231 | + } |
| 232 | + |
| 233 | + /** |
| 234 | + * Specify a {@linkplain StatementInspector} to be applied to all Sessions/EntityManagers |
| 235 | + * |
| 236 | + * @see JdbcSettings#STATEMENT_INSPECTOR |
| 237 | + */ |
| 238 | + public HibernatePersistenceConfiguration statementInspector(StatementInspector inspector) { |
| 239 | + property( JdbcSettings.STATEMENT_INSPECTOR, inspector ); |
| 240 | + return this; |
| 241 | + } |
| 242 | + |
| 243 | + /** |
| 244 | + * Configure a default catalog name to be used for database objects (tables, sequences, etc) which do not |
| 245 | + * explicitly specify one. |
| 246 | + * |
| 247 | + * @see MappingSettings#DEFAULT_CATALOG |
| 248 | + */ |
| 249 | + public HibernatePersistenceConfiguration defaultCatalog(String catalogName) { |
| 250 | + property( MappingSettings.DEFAULT_CATALOG, catalogName ); |
| 251 | + return this; |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Configure a default schema name to be used for database objects (tables, sequences, etc) which do not |
| 256 | + * explicitly specify one. |
| 257 | + * |
| 258 | + * @see MappingSettings#DEFAULT_SCHEMA |
| 259 | + */ |
| 260 | + public HibernatePersistenceConfiguration defaultSchema(String schemaName) { |
| 261 | + property( MappingSettings.DEFAULT_SCHEMA, schemaName ); |
| 262 | + return this; |
| 263 | + } |
| 264 | + |
| 265 | + /** |
| 266 | + * Configure a default schema name to be used for database objects (tables, sequences, etc) which do not |
| 267 | + * explicitly specify one. |
| 268 | + * |
| 269 | + * @see MappingSettings#USE_NATIONALIZED_CHARACTER_DATA |
| 270 | + */ |
| 271 | + public HibernatePersistenceConfiguration nationalizedCharacterData(boolean enabled) { |
| 272 | + property( MappingSettings.USE_NATIONALIZED_CHARACTER_DATA, enabled ); |
| 273 | + return this; |
| 274 | + } |
| 275 | + |
| 276 | + /** |
| 277 | + * Configures whether Hibernate should process XML mappings ({@code orm.xml} files). |
| 278 | + * |
| 279 | + * @see MappingSettings#XML_MAPPING_ENABLED |
| 280 | + */ |
| 281 | + public HibernatePersistenceConfiguration xmlMappings(boolean enabled) { |
| 282 | + property( MappingSettings.XML_MAPPING_ENABLED, enabled ); |
| 283 | + return this; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * Configures whether Hibernate should validate (via schema descriptor) XML files. |
| 288 | + * |
| 289 | + * @see MappingSettings#VALIDATE_XML |
| 290 | + */ |
| 291 | + public HibernatePersistenceConfiguration xmlValidation(boolean enabled) { |
| 292 | + property( MappingSettings.VALIDATE_XML, enabled ); |
| 293 | + return this; |
| 294 | + } |
| 295 | + |
| 296 | + /** |
| 297 | + * Configures whether Hibernate should collect {@linkplain org.hibernate.stat.Statistics}. |
| 298 | + * |
| 299 | + * @see StatisticsSettings#GENERATE_STATISTICS |
| 300 | + */ |
| 301 | + public HibernatePersistenceConfiguration collectStatistics(boolean enabled) { |
| 302 | + property( StatisticsSettings.GENERATE_STATISTICS, enabled ); |
| 303 | + return this; |
| 304 | + } |
| 305 | + |
| 306 | + |
| 307 | + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 308 | + // covariant overrides |
| 309 | + |
| 310 | + @Override |
| 311 | + public HibernatePersistenceConfiguration provider(String providerClassName) { |
| 312 | + return (HibernatePersistenceConfiguration) super.provider( providerClassName ); |
| 313 | + } |
| 314 | + |
| 315 | + @Override |
| 316 | + public HibernatePersistenceConfiguration jtaDataSource(String dataSourceJndiName) { |
| 317 | + return (HibernatePersistenceConfiguration) super.jtaDataSource( dataSourceJndiName ); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public HibernatePersistenceConfiguration nonJtaDataSource(String dataSourceJndiName) { |
| 322 | + return (HibernatePersistenceConfiguration) super.nonJtaDataSource( dataSourceJndiName ); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public HibernatePersistenceConfiguration managedClass(Class<?> managedClass) { |
| 327 | + return (HibernatePersistenceConfiguration) super.managedClass( managedClass ); |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public HibernatePersistenceConfiguration mappingFile(String name) { |
| 332 | + return (HibernatePersistenceConfiguration) super.mappingFile( name ); |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public HibernatePersistenceConfiguration transactionType(PersistenceUnitTransactionType transactionType) { |
| 337 | + return (HibernatePersistenceConfiguration) super.transactionType( transactionType ); |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public HibernatePersistenceConfiguration sharedCacheMode(SharedCacheMode sharedCacheMode) { |
| 342 | + return (HibernatePersistenceConfiguration) super.sharedCacheMode( sharedCacheMode ); |
| 343 | + } |
| 344 | + |
| 345 | + @Override |
| 346 | + public HibernatePersistenceConfiguration validationMode(ValidationMode validationMode) { |
| 347 | + return (HibernatePersistenceConfiguration) super.validationMode( validationMode ); |
| 348 | + } |
| 349 | + |
| 350 | + @Override |
| 351 | + public HibernatePersistenceConfiguration property(String name, Object value) { |
| 352 | + return (HibernatePersistenceConfiguration) super.property( name, value ); |
| 353 | + } |
| 354 | + |
| 355 | + @Override |
| 356 | + public HibernatePersistenceConfiguration properties(Map<String, ?> properties) { |
| 357 | + return (HibernatePersistenceConfiguration) super.properties( properties ); |
| 358 | + } |
| 359 | +} |
0 commit comments