Evgeni Evgeni - 1 year ago 85
Java Question

Hibernate schema migration set naming strategy

I have a Spring JPA application that uses

by default, which is very close to

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()

LocalSessionFactoryBuilder sessionFactory = new LocalSessionFactoryBuilder(dataSource);
//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.


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.