下面列出了怎么用org.hibernate.tool.schema.spi.CommandAcceptanceException的API类实例代码及写法,或者点击链接到github查看源代码。
protected PersistenceException persistenceException(String message, Exception cause) {
// Provide a comprehensible message if there is an issue with SSL support
Throwable t = cause;
while (t != null) {
if (t instanceof NoSuchAlgorithmException) {
message += "Unable to enable SSL support. You might be in the case where you used the `quarkus.ssl.native=false` configuration"
+ " and SSL was not disabled automatically for your driver.";
break;
}
if (t instanceof CommandAcceptanceException) {
message = "Invalid import file. Make sure your statements are valid and properly separated by a semi-colon.";
break;
}
t = t.getCause();
}
return new PersistenceException(getExceptionHeader() + message, cause);
}
private static void applySqlString(
String sqlString,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
if ( StringHelper.isEmpty( sqlString ) ) {
return;
}
try {
String sqlStringFormatted = formatter.format( sqlString );
for ( GenerationTarget target : targets ) {
target.accept( sqlStringFormatted );
}
}
catch (CommandAcceptanceException e) {
options.getExceptionHandler().handleException( e );
}
}
private static void applySqlString(
boolean quiet,
String sqlString,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
if ( !StringHelper.isEmpty( sqlString ) ) {
String sqlStringFormatted = formatter.format( sqlString );
for ( GenerationTarget target : targets ) {
try {
target.accept( sqlStringFormatted );
}
catch (CommandAcceptanceException e) {
if ( !quiet ) {
options.getExceptionHandler().handleException( e );
}
// otherwise ignore the exception
}
}
}
}
private static void applySqlString(
String sqlString,
Formatter formatter,
ExecutionOptions options,
GenerationTarget... targets) {
if ( StringHelper.isEmpty( sqlString ) ) {
return;
}
String sqlStringFormatted = formatter.format( sqlString );
for ( GenerationTarget target : targets ) {
try {
target.accept( sqlStringFormatted );
}
catch (CommandAcceptanceException e) {
options.getExceptionHandler().handleException( e );
}
}
}
@Test
public void whenWrongDialectSpecified_thenCommandAcceptanceException() {
thrown.expect(SchemaManagementException.class);
thrown.expectCause(isA(CommandAcceptanceException.class));
thrown.expectMessage("Halting on error : Error executing DDL");
Configuration cfg = getConfiguration();
cfg.setProperty(AvailableSettings.DIALECT,
"org.hibernate.dialect.MySQLDialect");
cfg.setProperty(AvailableSettings.HBM2DDL_AUTO, "update");
// This does not work due to hibernate bug
// cfg.setProperty(AvailableSettings.HBM2DDL_HALT_ON_ERROR,"true");
cfg.getProperties()
.put(AvailableSettings.HBM2DDL_HALT_ON_ERROR, true);
cfg.addAnnotatedClass(Product.class);
cfg.buildSessionFactory();
}
@Override
public void handleException(CommandAcceptanceException exception) {
throw new SchemaManagementException(
String.format(
Locale.ROOT,
"Halting on error : %s",
exception.getMessage()
),
exception
);
}
@Override
public void accept(String command) {
try {
writer().write( command );
writer().write( System.lineSeparator() );
writer().flush();
}
catch (IOException e) {
throw new CommandAcceptanceException( "Could not write \"" + command + "\" to target script file", e );
}
}
@Override
public void handleException(CommandAcceptanceException exception) {
log.warnf(
exception,
"GenerationTarget encountered exception accepting command : %s",
exception.getMessage()
);
}
@Override
public void perform(ServiceRegistry serviceRegistry) {
log.startingDelayedSchemaDrop();
final JdbcContext jdbcContext = new JdbcContextDelayedDropImpl( serviceRegistry );
final GenerationTargetToDatabase target = new GenerationTargetToDatabase(
serviceRegistry.getService( TransactionCoordinatorBuilder.class ).buildDdlTransactionIsolator( jdbcContext ),
true
);
target.prepare();
try {
for ( String command : commands ) {
try {
target.accept( command );
}
catch (CommandAcceptanceException e) {
// implicitly we do not "halt on error", but we do want to
// report the problem
log.unsuccessfulSchemaManagementCommand( command );
log.debugf( e, "Error performing delayed DROP command [%s]", command );
}
}
}
finally {
target.release();
}
}
@Override
public void handleException(CommandAcceptanceException exception) {
exceptions.add( exception );
}
public List<CommandAcceptanceException> getExceptions() {
return exceptions;
}