From 77a34e63126a2e8d372f5485d1c911c60e7c7e33 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Mon, 26 Aug 2024 12:17:14 -0500 Subject: [PATCH] HHH-18519 - Add vararg method to HibernatePersistenceConfiguration for supplying mappings --- .../HibernatePersistenceConfiguration.java | 61 +++++++++++++++++++ .../boot/PersistenceConfigurationTests.java | 41 +++++++++++++ 2 files changed, 102 insertions(+) diff --git a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceConfiguration.java b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceConfiguration.java index 9da09fbc97..23167948e5 100644 --- a/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceConfiguration.java +++ b/hibernate-core/src/main/java/org/hibernate/jpa/HibernatePersistenceConfiguration.java @@ -6,6 +6,8 @@ */ package org.hibernate.jpa; +import java.util.Collection; +import java.util.Collections; import java.util.Map; import java.util.Objects; @@ -15,8 +17,10 @@ import org.hibernate.cfg.CacheSettings; import org.hibernate.cfg.JdbcSettings; import org.hibernate.cfg.JpaComplianceSettings; import org.hibernate.cfg.MappingSettings; +import org.hibernate.cfg.SchemaToolingSettings; import org.hibernate.cfg.StatisticsSettings; import org.hibernate.resource.jdbc.spi.StatementInspector; +import org.hibernate.tool.schema.Action; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; @@ -26,6 +30,8 @@ import jakarta.persistence.SharedCacheMode; import jakarta.persistence.ValidationMode; /** + * Hibernate extension to the Jakarta Persistence PersistenceConfiguration contract. + * * @author Steve Ebersole */ public class HibernatePersistenceConfiguration extends PersistenceConfiguration { @@ -303,6 +309,61 @@ public class HibernatePersistenceConfiguration extends PersistenceConfiguration return this; } + /** + * Add the specified classes as {@linkplain #managedClasses() managed classes}. + * + * @see #managedClass + */ + public HibernatePersistenceConfiguration managedClasses(Class... managedClasses) { + Collections.addAll( managedClasses(), managedClasses ); + return this; + } + + /** + * Add the specified classes as {@linkplain #managedClasses() managed classes}. + * + * @see #managedClass + */ + public HibernatePersistenceConfiguration managedClasses(Collection> managedClasses) { + managedClasses().addAll( managedClasses ); + return this; + } + + /** + * Add the specified resource names as {@linkplain #mappingFiles() mapping files}. + * + * @see #mappingFiles + */ + public HibernatePersistenceConfiguration mappingFiles(String... names) { + Collections.addAll( mappingFiles(), names ); + return this; + } + + /** + * Add the specified resource names as {@linkplain #mappingFiles() mapping files}. + * + * @see #mappingFiles + */ + public HibernatePersistenceConfiguration mappingFiles(Collection names) { + mappingFiles().addAll( names ); + return this; + } + + /** + * Specify the {@linkplain Action action} to take in terms of automatic + * database schema tooling. + * + * @apiNote This only controls tooling as exported directly to the database. To + * output tooling commands to scripts, use {@linkplain #properties(Map) config properties} + * instead with appropriate {@linkplain SchemaToolingSettings settings}. + * + * @see SchemaToolingSettings#HBM2DDL_AUTO + */ + public HibernatePersistenceConfiguration schemaToolingAction(Action action) { + property( SchemaToolingSettings.HBM2DDL_AUTO, action ); + return this; + } + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // covariant overrides diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/PersistenceConfigurationTests.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/PersistenceConfigurationTests.java index 0fc3f71a2f..e3a5115390 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/PersistenceConfigurationTests.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/boot/PersistenceConfigurationTests.java @@ -6,11 +6,18 @@ */ package org.hibernate.orm.test.jpa.boot; +import java.util.List; + import org.hibernate.dialect.H2Dialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; import org.hibernate.jpa.HibernatePersistenceConfiguration; +import org.hibernate.tool.schema.Action; import org.hibernate.testing.env.TestingDatabaseInfo; +import org.hibernate.testing.orm.domain.library.Book; +import org.hibernate.testing.orm.domain.library.Person; import org.hibernate.testing.orm.junit.RequiresDialect; +import org.hibernate.testing.transaction.TransactionUtil2; import org.junit.jupiter.api.Test; import jakarta.persistence.EntityManagerFactory; @@ -110,4 +117,38 @@ public class PersistenceConfigurationTests { //end::example-bootstrap-standard-HibernatePersistenceConfiguration[] } } + + @Test + @RequiresDialect( H2Dialect.class ) + public void testVarargs() { + final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration( "emf" ) + .jdbcUrl( "jdbc:h2:mem:db1" ) + .jdbcUsername( "sa" ) + .jdbcPassword( "" ) + .schemaToolingAction( Action.CREATE_DROP ) + .managedClasses( Book.class, Person.class ); + try (EntityManagerFactory emf = cfg.createEntityManagerFactory()) { + assert emf.isOpen(); + TransactionUtil2.inTransaction( emf.unwrap( SessionFactoryImplementor.class ), (em) -> { + em.createSelectionQuery( "from Book", Book.class ).list(); + } ); + } + } + + @Test + @RequiresDialect( H2Dialect.class ) + public void testVarargs2() { + final PersistenceConfiguration cfg = new HibernatePersistenceConfiguration( "emf" ) + .jdbcUrl( "jdbc:h2:mem:db1" ) + .jdbcUsername( "sa" ) + .jdbcPassword( "" ) + .schemaToolingAction( Action.CREATE_DROP ) + .managedClasses( List.of( Book.class, Person.class ) ); + try (EntityManagerFactory emf = cfg.createEntityManagerFactory()) { + assert emf.isOpen(); + TransactionUtil2.inTransaction( emf.unwrap( SessionFactoryImplementor.class ), (em) -> { + em.createSelectionQuery( "from Book", Book.class ).list(); + } ); + } + } }