Implementing a NamingStrategy in Hibernate 5 (make autogenerated column names UPPERCASE)

前端 未结 1 1366
挽巷
挽巷 2021-01-24 21:44

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 colu

相关标签:
1条回答
  • 2021-01-24 22:11

    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.

    0 讨论(0)
提交回复
热议问题