HHH-6703 id element has 'column' attribute, then the pk column is nullable in ddl created by schema export

This commit is contained in:
Strong Liu 2011-10-01 01:21:14 +08:00
parent 6764549784
commit 3d3b5b85f5
6 changed files with 81 additions and 18 deletions

View File

@ -25,6 +25,8 @@ package org.hibernate.engine.jdbc.internal;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import org.hibernate.internal.util.StringHelper;
/** /**
* Performs formatting of DDL SQL statements. * Performs formatting of DDL SQL statements.
* *
@ -43,6 +45,7 @@ public class DDLFormatterImpl implements Formatter {
* @param sql The statement to be fornmatted. * @param sql The statement to be fornmatted.
*/ */
public String format(String sql) { public String format(String sql) {
if ( StringHelper.isEmpty( sql ) ) return sql;
if ( sql.toLowerCase().startsWith( "create table" ) ) { if ( sql.toLowerCase().startsWith( "create table" ) ) {
return formatCreateTable( sql ); return formatCreateTable( sql );
} }

View File

@ -35,16 +35,27 @@ class ColumnAttributeSourceImpl implements ColumnSource {
private final String columnName; private final String columnName;
private boolean includedInInsert; private boolean includedInInsert;
private boolean includedInUpdate; private boolean includedInUpdate;
private boolean isForceNotNull;
ColumnAttributeSourceImpl( ColumnAttributeSourceImpl(
String tableName, String tableName,
String columnName, String columnName,
boolean includedInInsert, boolean includedInInsert,
boolean includedInUpdate) { boolean includedInUpdate) {
this(tableName, columnName, includedInInsert, includedInUpdate, false);
}
ColumnAttributeSourceImpl(
String tableName,
String columnName,
boolean includedInInsert,
boolean includedInUpdate,
boolean isForceNotNull) {
this.tableName = tableName; this.tableName = tableName;
this.columnName = columnName; this.columnName = columnName;
this.includedInInsert = includedInInsert; this.includedInInsert = includedInInsert;
this.includedInUpdate = includedInUpdate; this.includedInUpdate = includedInUpdate;
this.isForceNotNull = isForceNotNull;
} }
@Override @Override
@ -69,7 +80,7 @@ class ColumnAttributeSourceImpl implements ColumnSource {
@Override @Override
public boolean isNullable() { public boolean isNullable() {
return true; return !isForceNotNull;
} }
@Override @Override

View File

@ -36,17 +36,27 @@ class ColumnSourceImpl implements ColumnSource {
private final JaxbColumnElement columnElement; private final JaxbColumnElement columnElement;
private boolean includedInInsert; private boolean includedInInsert;
private boolean includedInUpdate; private boolean includedInUpdate;
private final boolean isForceNotNull;
ColumnSourceImpl( ColumnSourceImpl(
String tableName, String tableName,
JaxbColumnElement columnElement, JaxbColumnElement columnElement,
boolean isIncludedInInsert, boolean isIncludedInInsert,
boolean isIncludedInUpdate) { boolean isIncludedInUpdate) {
this.tableName = tableName; this(tableName, columnElement, isIncludedInInsert, isIncludedInUpdate, false);
this.columnElement = columnElement;
includedInInsert = isIncludedInInsert;
includedInUpdate = isIncludedInUpdate;
} }
ColumnSourceImpl(
String tableName,
JaxbColumnElement columnElement,
boolean isIncludedInInsert,
boolean isIncludedInUpdate,
boolean isForceNotNull) {
this.tableName = tableName;
this.columnElement = columnElement;
this.isForceNotNull = isForceNotNull;
includedInInsert = isIncludedInInsert;
includedInUpdate = isIncludedInUpdate;
}
@Override @Override
public String getName() { public String getName() {
@ -55,6 +65,7 @@ class ColumnSourceImpl implements ColumnSource {
@Override @Override
public boolean isNullable() { public boolean isNullable() {
if(isForceNotNull)return false;
return ! columnElement.isNotNull(); return ! columnElement.isNotNull();
} }

View File

@ -261,16 +261,37 @@ public class Helper {
return Identifier.toIdentifier( name ); return Identifier.toIdentifier( name );
} }
public static interface ValueSourcesAdapter { public static class ValueSourcesAdapter {
public String getContainingTableName(); public String getContainingTableName() {
public boolean isIncludedInInsertByDefault(); return null;
public boolean isIncludedInUpdateByDefault(); }
public String getColumnAttribute();
public String getFormulaAttribute();
public List getColumnOrFormulaElements();
}
public static List<RelationalValueSource> buildValueSources( public boolean isIncludedInInsertByDefault() {
return false;
}
public boolean isIncludedInUpdateByDefault() {
return false;
}
public String getColumnAttribute() {
return null;
}
public String getFormulaAttribute() {
return null;
}
public List getColumnOrFormulaElements() {
return null;
}
public boolean isForceNotNull() {
return false;
}
}
public static List<RelationalValueSource> buildValueSources(
ValueSourcesAdapter valueSourcesAdapter, ValueSourcesAdapter valueSourcesAdapter,
LocalBindingContext bindingContext) { LocalBindingContext bindingContext) {
List<RelationalValueSource> result = new ArrayList<RelationalValueSource>(); List<RelationalValueSource> result = new ArrayList<RelationalValueSource>();
@ -294,7 +315,8 @@ public class Helper {
valueSourcesAdapter.getContainingTableName(), valueSourcesAdapter.getContainingTableName(),
valueSourcesAdapter.getColumnAttribute(), valueSourcesAdapter.getColumnAttribute(),
valueSourcesAdapter.isIncludedInInsertByDefault(), valueSourcesAdapter.isIncludedInInsertByDefault(),
valueSourcesAdapter.isIncludedInUpdateByDefault() valueSourcesAdapter.isIncludedInUpdateByDefault(),
valueSourcesAdapter.isForceNotNull()
) )
); );
} }
@ -323,7 +345,8 @@ public class Helper {
valueSourcesAdapter.getContainingTableName(), valueSourcesAdapter.getContainingTableName(),
(JaxbColumnElement) columnOrFormulaElement, (JaxbColumnElement) columnOrFormulaElement,
valueSourcesAdapter.isIncludedInInsertByDefault(), valueSourcesAdapter.isIncludedInInsertByDefault(),
valueSourcesAdapter.isIncludedInUpdateByDefault() valueSourcesAdapter.isIncludedInUpdateByDefault(),
valueSourcesAdapter.isForceNotNull()
) )
); );
} }

View File

@ -101,7 +101,12 @@ class SingularIdentifierAttributeSourceImpl implements SingularAttributeSource {
public boolean isIncludedInUpdateByDefault() { public boolean isIncludedInUpdateByDefault() {
return false; return false;
} }
},
@Override
public boolean isForceNotNull() {
return true;
}
},
bindingContext bindingContext
); );
} }

View File

@ -28,6 +28,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.hibernate.cfg.Configuration; import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.testing.junit4.BaseUnitTestCase; import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.tool.hbm2ddl.SchemaExport; import org.hibernate.tool.hbm2ddl.SchemaExport;
@ -41,6 +42,10 @@ public abstract class SchemaExportTest extends BaseUnitTestCase {
protected abstract SchemaExport createSchemaExport(Configuration cfg); protected abstract SchemaExport createSchemaExport(Configuration cfg);
private boolean doesDialectSupportDropTableIfExist(){
return Dialect.getDialect().supportsIfExistsAfterTableName() || Dialect.getDialect().supportsIfExistsBeforeTableName();
}
@Test @Test
public void testCreateAndDropOnlyType() { public void testCreateAndDropOnlyType() {
Configuration cfg = new Configuration(); Configuration cfg = new Configuration();
@ -66,7 +71,12 @@ public abstract class SchemaExportTest extends BaseUnitTestCase {
SchemaExport schemaExport = createSchemaExport( cfg ); SchemaExport schemaExport = createSchemaExport( cfg );
// drop before create (nothing to drop yeT) // drop before create (nothing to drop yeT)
schemaExport.execute( false, true, false, false ); schemaExport.execute( false, true, false, false );
assertEquals( 0, schemaExport.getExceptions().size() ); if ( doesDialectSupportDropTableIfExist() ) {
assertEquals( 0, schemaExport.getExceptions().size() );
}
else {
assertEquals( 2, schemaExport.getExceptions().size() );
}
// drop before crete again (this time drops the tables before re-creating) // drop before crete again (this time drops the tables before re-creating)
schemaExport.execute( false, true, false, false ); schemaExport.execute( false, true, false, false );
assertEquals( 0, schemaExport.getExceptions().size() ); assertEquals( 0, schemaExport.getExceptions().size() );