Evgeni Evgeni - 6 months ago 39
Java Question

Hibernate schema migration set naming strategy

I have a Spring JPA application that uses

SpringNamingStrategy
by default, which is very close to
ImprovedNamingStrategy
.

I'm writing a helper that uses the hibernate classes to generate scripts for schema update.

public class MigrationHelper {

public static void main(String[] args) throws SQLException {
DataSource dataSource = DataSourceBuilder.create()
.driverClassName("oracle.jdbc.driver.OracleDriver")
.url("jdbc:oracle:thin:@localhost:1521:ORCL")
.username("mqcompare")
.password("mqcompare")
.build();

LocalSessionFactoryBuilder sessionFactory = new LocalSessionFactoryBuilder(dataSource);
sessionFactory.scanPackages("com.company.mqmsgcom.entity");
//this doesen't work
sessionFactory.setProperty("hibernate.ejb.naming_strategy", "org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy");
Dialect dialect = new Oracle10gDialect();
DatabaseMetadata metadata = new DatabaseMetadata(dataSource.getConnection(), dialect, sessionFactory);
List<SchemaUpdateScript> scripts = sessionFactory.generateSchemaUpdateScriptList(dialect, metadata);

Formatter formatter = FormatStyle.DDL.getFormatter();
for (SchemaUpdateScript script : scripts) {
System.err.println(formatter.format(script.getScript()) + ";");
}
}
}


Script reference: here

How can I set the naming strategy in this code?

Edit: Hibernate version is 4.3.11 Final.

Answer

hibernate.ejb.naming_strategy is used for JPA. You can set a naming strategy by LocalSessionFactoryBuilder#setNamingStrategy().

Looks like, there is not a property in Hibernate 4 to set a naming strategy to Configuration as a property.