HHH-6384 : hibernate.hbm2ddl.auto=create does not drop tables

This commit is contained in:
Gail Badner 2011-08-19 17:33:57 -07:00 committed by Steve Ebersole
parent 61d60d0c88
commit 09bba73a25
3 changed files with 141 additions and 2 deletions

View File

@ -270,7 +270,8 @@ public class SchemaExport {
}
/**
* Run the schema creation script.
* Run the schema creation script; drop script is automatically
* executed before running the creation script.
*
* @param script print the DDL to the console
* @param export export the script to the database
@ -279,8 +280,15 @@ public class SchemaExport {
create( Target.interpret( script, export ) );
}
/**
* Run the schema creation script; drop script is automatically
* executed before running the creation script.
*
* @param output the target of the script.
*/
public void create(Target output) {
execute( output, Type.CREATE );
// need to drop tables before creating so need to specify Type.BOTH
execute( output, Type.BOTH );
}
/**

View File

@ -0,0 +1,112 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.test.schemaexport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.testing.ServiceRegistryBuilder;
import org.hibernate.testing.junit4.BaseUnitTestCase;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import static org.junit.Assert.assertEquals;
/**
* @author Gail Badner
*/
public class SchemaExportTest extends BaseUnitTestCase {
private final String MAPPING = "org/hibernate/test/schemaexport/mapping.hbm.xml";
private ServiceRegistry serviceRegistry;
@Before
public void setUp() {
serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry( Environment.getProperties() );
}
@After
public void tearDown() {
ServiceRegistryBuilder.destroy( serviceRegistry );
serviceRegistry = null;
}
protected JdbcServices getJdbcServices() {
return serviceRegistry.getService( JdbcServices.class );
}
@Test
public void testCreateAndDropOnlyType() {
Configuration cfg = new Configuration();
cfg.addResource( MAPPING );
SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg );
// create w/o dropping first; (OK because tables don't exist yet
schemaExport.execute( false, true, false, true );
assertEquals( 0, schemaExport.getExceptions().size() );
// create w/o dropping again; should be an exception for each table
// (2 total) because the tables exist already
assertEquals( 0, schemaExport.getExceptions().size() );
schemaExport.execute( false, true, false, true );
assertEquals( 2, schemaExport.getExceptions().size() );
// drop tables only
schemaExport.execute( false, true, true, false );
assertEquals( 0, schemaExport.getExceptions().size() );
}
@Test
public void testBothType() {
Configuration cfg = new Configuration();
cfg.addResource( MAPPING );
SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg );
// drop before create (nothing to drop yeT)
schemaExport.execute( false, true, false, false );
assertEquals( 0, 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() );
// drop tables
schemaExport.execute( false, true, true, false );
assertEquals( 0, schemaExport.getExceptions().size() );
}
@Test
public void testCreateAndDrop() {
Configuration cfg = new Configuration();
cfg.addResource( MAPPING );
SchemaExport schemaExport = new SchemaExport( serviceRegistry, cfg );
// should drop before creating, but tables don't exist yet
schemaExport.create( false, true);
assertEquals( 0, schemaExport.getExceptions().size() );
// call create again; it should drop tables before re-creating
schemaExport.create( false, true );
assertEquals( 0, schemaExport.getExceptions().size() );
// drop the tables
schemaExport.drop( false, true );
assertEquals( 0, schemaExport.getExceptions().size() );
}
}

View File

@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.schemaupdate">
<class name="Version">
<id name="id">
<generator class="org.hibernate.id.TableHiLoGenerator">
<param name="table">uid_table</param>
<param name="column">next_hi_value_column</param>
</generator>
</id>
<property name="description"/>
</class>
</hibernate-mapping>