1

I need to let Hibernate auto generate the database starting from the entities, however I want them all UPPERCASE.

This used to work in the past now I have messed up column names with Upper and Lower case letters.

I enabled the

.setProperty("hibernate.hbm2ddl.auto", "create") 

to let Hibernate auto generate the database, and I created an UppercaseNamingStrategy.java extending org.hibernate.cfg.ImprovedNamingStrategy.

According to https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html_single/#configuration-namingstrategy

Now I should

You can specify a different strategy by calling Configuration.setNamingStrategy() before adding mappings:

SessionFactory sf = new Configuration()
.setNamingStrategy(ImprovedNamingStrategy.INSTANCE)
.addFile("Item.hbm.xml")
.addFile("Bid.hbm.xml")
.buildSessionFactory();

org.hibernate.cfg.ImprovedNamingStrategy is a built-in strategy that might be a useful starting point for some applications.

Configuration.setNamingStrategy() however seems to be no more in Hibernate 5.0.6.

I would of course like to do it programatically (I don't have .xml configuration files and don't want them).

NOTE:

Using

.setProperty("hibernate.ejb.naming_strategy", "my.project.hibernate.UppercaseNamingStrategy")

doesn't work as well, seems to be ignored altogether...

sarah.ferguson
  • 3,167
  • 2
  • 23
  • 31
  • Try to override this https://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/boot/model/naming/ImplicitNamingStrategyLegacyHbmImpl.html. – Bilal BBB Jan 22 '16 at 19:13
  • This is another solution http://stackoverflow.com/questions/32165694/spring-hibernate-5-naming-strategy-configuration – Bilal BBB Jan 22 '16 at 19:13

1 Answers1

2

Hibernate 5 uses two new interfaces for name strategies PhysicalNamingStrategy and ImplicitNamingStrategy. You need just implement PhysicalNamingStrategy. It is called by Hibernate after all column names are created for model. So you can just make it uppercase. Hibernate uses by default PhysicalNamingStrategyStandardImpl, that do nothing. You can just extend it

public class UpperCaseNamingStrategy extends PhysicalNamingStrategyStandardImpl {

    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        return context.getIdentifierHelper().toIdentifier(
            StringUtils.upperCase(name.getText(), Locale.ENGLISH));
    }

}

You can build session factory with UpperCaseNamingStrategy by this way

    Configuration configuration = new Configuration();
    configuration.setPhysicalNamingStrategy(new UpperCaseNamingStrategy());
    SessionFactory sessionFactory = configuration.configure().buildSessionFactory(); 

I am working on a more complex name strategy now. You can refer Hibernate5NamingStrategy if you are interested.

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Thankyou, upgrading to Hibernate 5 and extending PhysicalNamingStrategyStandardImpl fixed it.Just a quick rant: why do they have to change the name of methods and classes and leave the documentation unchanged?? Please, keep updating code and leave naming as is. – sarah.ferguson Jan 25 '16 at 11:04
  • @sarah.ferguson You are welcome. Looks like, Hibernate always had a poor documentation. It is interesting that there is the `ImprovedNamingStrategy` class in Hibernate 5, but you can't use it anymore. – v.ladynev Jan 25 '16 at 14:20
  • 1
    Hey @v.ladynev thanks for this information. Just a quick question, what if I wanted to configure my app via persistence.xml? – Kikin-Sama May 13 '16 at 23:09
  • @Kikin-Sama Sorry, I don't still investigate using the naming strategies with JPA. – v.ladynev May 14 '16 at 09:14