Skip to content

Commit b8ed5ad

Browse files
committed
add some typesafe + varargs operations to Configuration
to improve getting started experience
1 parent 10935e1 commit b8ed5ad

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

hibernate-core/src/main/java/org/hibernate/cfg/Configuration.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Map;
1414
import java.util.Properties;
1515

16+
import jakarta.persistence.PersistenceUnitTransactionType;
1617
import org.hibernate.CustomEntityDirtinessStrategy;
1718
import org.hibernate.EntityNameResolver;
1819
import org.hibernate.HibernateException;
@@ -55,6 +56,7 @@
5556
import org.hibernate.query.sqm.function.SqmFunctionDescriptor;
5657
import org.hibernate.resource.jdbc.spi.StatementInspector;
5758
import org.hibernate.service.ServiceRegistry;
59+
import org.hibernate.tool.schema.Action;
5860
import org.hibernate.type.BasicType;
5961
import org.hibernate.type.SerializationException;
6062
import org.hibernate.usertype.UserType;
@@ -441,6 +443,75 @@ public Configuration configure(File configFile) throws HibernateException {
441443
return this;
442444
}
443445

446+
// New typed property setters ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
447+
448+
/**
449+
* Set {@value AvailableSettings#SHOW_SQL}, {@value AvailableSettings#FORMAT_SQL},
450+
* and {@value AvailableSettings#HIGHLIGHT_SQL}.
451+
*
452+
* @param showSql should SQL be logged to console?
453+
* @param formatSql should logged SQL be formatted
454+
* @param highlightSql should logged SQL be highlighted with pretty colors
455+
*/
456+
public Configuration showSql(boolean showSql, boolean formatSql, boolean highlightSql) {
457+
setProperty( AvailableSettings.SHOW_SQL, Boolean.toString(showSql) );
458+
setProperty( AvailableSettings.FORMAT_SQL, Boolean.toString(formatSql) );
459+
setProperty( AvailableSettings.HIGHLIGHT_SQL, Boolean.toString(highlightSql) );
460+
return this;
461+
}
462+
463+
/**
464+
* Set {@value AvailableSettings#HBM2DDL_AUTO}.
465+
*
466+
* @param action the {@link Action}
467+
*/
468+
public Configuration setSchemaExportAction(Action action) {
469+
setProperty( AvailableSettings.HBM2DDL_AUTO, action.getExternalHbm2ddlName() );
470+
return this;
471+
}
472+
473+
/**
474+
* Set {@value AvailableSettings#USER} and {@value AvailableSettings#PASS}.
475+
*
476+
* @param user the user id
477+
* @param pass the password
478+
*/
479+
public Configuration setCredentials(String user, String pass) {
480+
setProperty( AvailableSettings.USER, user );
481+
setProperty( AvailableSettings.PASS, pass );
482+
return this;
483+
}
484+
485+
/**
486+
* Set {@value AvailableSettings#URL}.
487+
*
488+
* @param url the JDBC URL
489+
*/
490+
public Configuration setJdbcUrl(String url) {
491+
setProperty( AvailableSettings.URL, url );
492+
return this;
493+
}
494+
495+
/**
496+
* Set {@value AvailableSettings#DATASOURCE}.
497+
*
498+
* @param jndiName the JNDI name of the datasource
499+
*/
500+
public Configuration setDatasource(String jndiName) {
501+
setProperty( AvailableSettings.DATASOURCE, jndiName );
502+
return this;
503+
}
504+
505+
/**
506+
* Set {@value AvailableSettings#JAKARTA_TRANSACTION_TYPE}.
507+
*
508+
* @param transactionType the {@link PersistenceUnitTransactionType}
509+
*/
510+
public Configuration setTransactionType(PersistenceUnitTransactionType transactionType) {
511+
setProperty( AvailableSettings.JAKARTA_TRANSACTION_TYPE, transactionType.toString() );
512+
return this;
513+
}
514+
444515
// MetadataSources ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
445516

446517
/**
@@ -694,6 +765,20 @@ public Configuration addAnnotatedClass(Class<?> annotatedClass) {
694765
return this;
695766
}
696767

768+
/**
769+
* Read metadata from the annotations associated with the given classes.
770+
*
771+
* @param annotatedClasses The classes containing annotations
772+
*
773+
* @return this (for method chaining)
774+
*/
775+
public Configuration addAnnotatedClasses(Class... annotatedClasses) {
776+
for (Class annotatedClass : annotatedClasses) {
777+
addAnnotatedClass( annotatedClass );
778+
}
779+
return this;
780+
}
781+
697782
/**
698783
* Read package-level metadata.
699784
*
@@ -708,6 +793,22 @@ public Configuration addPackage(String packageName) throws MappingException {
708793
return this;
709794
}
710795

796+
/**
797+
* Read package-level metadata.
798+
*
799+
* @param packageNames java package names
800+
*
801+
* @return this (for method chaining)
802+
*
803+
* @throws MappingException in case there is an error in the mapping data
804+
*/
805+
public Configuration addPackages(String... packageNames) throws MappingException {
806+
for (String packageName : packageNames) {
807+
addPackage( packageName );
808+
}
809+
return this;
810+
}
811+
711812
/**
712813
* Read all {@code .hbm.xml} mappings from a {@code .jar} file.
713814
* <p>

0 commit comments

Comments
 (0)