HHH-5337 Allow Customization of DML import file name
hibernate.hbm2ddl.import_file=[filename] git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19803 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
ffe92dacfa
commit
5aad112e03
|
@ -468,6 +468,15 @@ public final class Environment {
|
||||||
*/
|
*/
|
||||||
public static final String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
|
public static final String HBM2DDL_AUTO = "hibernate.hbm2ddl.auto";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Name of the optional file containing SQL DML statements executed during the SessionFactory creation.
|
||||||
|
* These statements are only executed if the schema is created ie if <tt>hibernate.hbm2ddl.auto</tt>
|
||||||
|
* is set to <tt>create</tt> or <tt>create-drop</tt>.
|
||||||
|
*
|
||||||
|
* The default value is <tt>/import.sql</tt>
|
||||||
|
*/
|
||||||
|
public static final String HBM2DDL_IMPORT_FILE = "hibernate.hbm2ddl.import_file";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link org.hibernate.exception.SQLExceptionConverter} to use for converting SQLExceptions
|
* The {@link org.hibernate.exception.SQLExceptionConverter} to use for converting SQLExceptions
|
||||||
* to Hibernate's JDBCException hierarchy. The default is to use the configured
|
* to Hibernate's JDBCException hierarchy. The default is to use the configured
|
||||||
|
|
|
@ -100,6 +100,7 @@ public final class Settings {
|
||||||
// private ComponentTuplizerFactory componentTuplizerFactory; todo : HHH-3517 and HHH-1907
|
// private ComponentTuplizerFactory componentTuplizerFactory; todo : HHH-3517 and HHH-1907
|
||||||
// private BytecodeProvider bytecodeProvider;
|
// private BytecodeProvider bytecodeProvider;
|
||||||
private JdbcSupport jdbcSupport;
|
private JdbcSupport jdbcSupport;
|
||||||
|
private String importFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Package protected constructor
|
* Package protected constructor
|
||||||
|
@ -117,6 +118,14 @@ public final class Settings {
|
||||||
// return formatSql;
|
// return formatSql;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
public String getImportFile() {
|
||||||
|
return importFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImportFile(String importFile) {
|
||||||
|
this.importFile = importFile;
|
||||||
|
}
|
||||||
|
|
||||||
public SQLStatementLogger getSqlStatementLogger() {
|
public SQLStatementLogger getSqlStatementLogger() {
|
||||||
return sqlStatementLogger;
|
return sqlStatementLogger;
|
||||||
}
|
}
|
||||||
|
|
|
@ -337,6 +337,7 @@ public class SettingsFactory implements Serializable {
|
||||||
settings.setAutoCreateSchema(true);
|
settings.setAutoCreateSchema(true);
|
||||||
settings.setAutoDropSchema(true);
|
settings.setAutoDropSchema(true);
|
||||||
}
|
}
|
||||||
|
settings.setImportFile( properties.getProperty( Environment.HBM2DDL_IMPORT_FILE ) );
|
||||||
|
|
||||||
EntityMode defaultEntityMode = EntityMode.parse( properties.getProperty( Environment.DEFAULT_ENTITY_MODE ) );
|
EntityMode defaultEntityMode = EntityMode.parse( properties.getProperty( Environment.DEFAULT_ENTITY_MODE ) );
|
||||||
log.info( "Default entity-mode: " + defaultEntityMode );
|
log.info( "Default entity-mode: " + defaultEntityMode );
|
||||||
|
|
|
@ -73,13 +73,14 @@ public class SchemaExport {
|
||||||
private String[] dropSQL;
|
private String[] dropSQL;
|
||||||
private String[] createSQL;
|
private String[] createSQL;
|
||||||
private String outputFile = null;
|
private String outputFile = null;
|
||||||
private String importFile = "/import.sql";
|
private String importFile;
|
||||||
private Dialect dialect;
|
private Dialect dialect;
|
||||||
private String delimiter;
|
private String delimiter;
|
||||||
private final List exceptions = new ArrayList();
|
private final List exceptions = new ArrayList();
|
||||||
private boolean haltOnError = false;
|
private boolean haltOnError = false;
|
||||||
private Formatter formatter;
|
private Formatter formatter;
|
||||||
private SQLStatementLogger sqlStatementLogger;
|
private SQLStatementLogger sqlStatementLogger;
|
||||||
|
private static final String DEFAULT_IMPORT_FILE = "/import.sql";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a schema exporter for the given Configuration
|
* Create a schema exporter for the given Configuration
|
||||||
|
@ -105,6 +106,7 @@ public class SchemaExport {
|
||||||
createSQL = cfg.generateSchemaCreationScript( dialect );
|
createSQL = cfg.generateSchemaCreationScript( dialect );
|
||||||
sqlStatementLogger = settings.getSqlStatementLogger();
|
sqlStatementLogger = settings.getSqlStatementLogger();
|
||||||
formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
formatter = ( sqlStatementLogger.isFormatSql() ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
||||||
|
importFile = settings.getImportFile() != null ? settings.getImportFile() : DEFAULT_IMPORT_FILE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -129,6 +131,8 @@ public class SchemaExport {
|
||||||
createSQL = cfg.generateSchemaCreationScript( dialect );
|
createSQL = cfg.generateSchemaCreationScript( dialect );
|
||||||
|
|
||||||
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, props ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
||||||
|
|
||||||
|
importFile = PropertiesHelper.getString( Environment.HBM2DDL_IMPORT_FILE, props, DEFAULT_IMPORT_FILE );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -144,6 +148,9 @@ public class SchemaExport {
|
||||||
dropSQL = cfg.generateDropSchemaScript( dialect );
|
dropSQL = cfg.generateDropSchemaScript( dialect );
|
||||||
createSQL = cfg.generateSchemaCreationScript( dialect );
|
createSQL = cfg.generateSchemaCreationScript( dialect );
|
||||||
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, cfg.getProperties() ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
formatter = ( PropertiesHelper.getBoolean( Environment.FORMAT_SQL, cfg.getProperties() ) ? FormatStyle.DDL : FormatStyle.NONE ).getFormatter();
|
||||||
|
importFile = PropertiesHelper.getString( Environment.HBM2DDL_IMPORT_FILE, cfg.getProperties(),
|
||||||
|
DEFAULT_IMPORT_FILE
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -162,6 +169,7 @@ public class SchemaExport {
|
||||||
*
|
*
|
||||||
* @param filename The import file name.
|
* @param filename The import file name.
|
||||||
* @return this
|
* @return this
|
||||||
|
* @deprecated use {@link org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILE}
|
||||||
*/
|
*/
|
||||||
public SchemaExport setImportFile(String filename) {
|
public SchemaExport setImportFile(String filename) {
|
||||||
importFile = filename;
|
importFile = filename;
|
||||||
|
@ -405,7 +413,7 @@ public class SchemaExport {
|
||||||
boolean halt = false;
|
boolean halt = false;
|
||||||
boolean export = true;
|
boolean export = true;
|
||||||
String outFile = null;
|
String outFile = null;
|
||||||
String importFile = "/import.sql";
|
String importFile = DEFAULT_IMPORT_FILE;
|
||||||
String propFile = null;
|
String propFile = null;
|
||||||
boolean format = false;
|
boolean format = false;
|
||||||
String delim = null;
|
String delim = null;
|
||||||
|
@ -471,10 +479,12 @@ public class SchemaExport {
|
||||||
cfg.setProperties( props );
|
cfg.setProperties( props );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (importFile != null) {
|
||||||
|
cfg.setProperty( Environment.HBM2DDL_IMPORT_FILE, importFile );
|
||||||
|
}
|
||||||
SchemaExport se = new SchemaExport( cfg )
|
SchemaExport se = new SchemaExport( cfg )
|
||||||
.setHaltOnError( halt )
|
.setHaltOnError( halt )
|
||||||
.setOutputFile( outFile )
|
.setOutputFile( outFile )
|
||||||
.setImportFile( importFile )
|
|
||||||
.setDelimiter( delim );
|
.setDelimiter( delim );
|
||||||
if ( format ) {
|
if ( format ) {
|
||||||
se.setFormat( true );
|
se.setFormat( true );
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
<!DOCTYPE hibernate-mapping PUBLIC
|
||||||
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||||
|
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||||
|
|
||||||
|
<!--
|
||||||
|
|
||||||
|
Test for import files.
|
||||||
|
|
||||||
|
-->
|
||||||
|
|
||||||
|
<hibernate-mapping package="org.hibernate.test.importfile">
|
||||||
|
<class name="Human" table="human">
|
||||||
|
<id name="id"/>
|
||||||
|
<property name="firstname" column="fname"/>
|
||||||
|
<property name="lastname" column="lname"/>
|
||||||
|
</class>
|
||||||
|
</hibernate-mapping>
|
|
@ -0,0 +1,34 @@
|
||||||
|
package org.hibernate.test.importfile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bernard
|
||||||
|
*/
|
||||||
|
public class Human {
|
||||||
|
private Integer id;
|
||||||
|
private String firstname;
|
||||||
|
private String lastname;
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstname() {
|
||||||
|
return firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstname(String firstname) {
|
||||||
|
this.firstname = firstname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastname() {
|
||||||
|
return lastname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastname(String lastname) {
|
||||||
|
this.lastname = lastname;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
package org.hibernate.test.importfile;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import org.hibernate.cfg.Configuration;
|
||||||
|
import org.hibernate.cfg.Environment;
|
||||||
|
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Emmanuel Bernard
|
||||||
|
*/
|
||||||
|
public class ImportFileTest extends FunctionalTestCase {
|
||||||
|
|
||||||
|
public void testImportFile() throws Exception {
|
||||||
|
Session s = openSession( );
|
||||||
|
final Transaction tx = s.beginTransaction();
|
||||||
|
final List<?> list = s.createQuery( "from " + Human.class.getName() ).list();
|
||||||
|
assertEquals( "database.sql not imported", 3, list.size() );
|
||||||
|
for (Object entity : list) {
|
||||||
|
s.delete( entity );
|
||||||
|
}
|
||||||
|
tx.commit();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void configure(Configuration cfg) {
|
||||||
|
cfg.setProperty( Environment.HBM2DDL_IMPORT_FILE, "/database.sql");
|
||||||
|
}
|
||||||
|
|
||||||
|
public ImportFileTest(String string) {
|
||||||
|
super( string );
|
||||||
|
}
|
||||||
|
|
||||||
|
public String[] getMappings() {
|
||||||
|
return new String[] {
|
||||||
|
"importfile/Human.hbm.xml"
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
INSERT INTO human (id, fname, lname) VALUES (1,'Emmanuel','Bernard')
|
||||||
|
INSERT INTO human (id, fname, lname) VALUES (2,'Gavin','King')
|
||||||
|
INSERT INTO human (id, fname, lname) VALUES (3,'Max','Andersen')
|
Loading…
Reference in New Issue