HHH-18519 - Add vararg method to HibernatePersistenceConfiguration for supplying mappings

This commit is contained in:
Steve Ebersole 2024-08-26 12:17:14 -05:00
parent 4b06bf63cd
commit 77a34e6312
2 changed files with 102 additions and 0 deletions

View File

@ -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<Class<?>> 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<String> 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

View File

@ -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();
} );
}
}
}