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 org.hibernate.internal.util.StringHelper;
/**
* Performs formatting of DDL SQL statements.
*
@ -43,6 +45,7 @@ public class DDLFormatterImpl implements Formatter {
* @param sql The statement to be fornmatted.
*/
public String format(String sql) {
if ( StringHelper.isEmpty( sql ) ) return sql;
if ( sql.toLowerCase().startsWith( "create table" ) ) {
return formatCreateTable( sql );
}

View File

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

View File

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

View File

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

View File

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

View File

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