java在Hibernate 5中实现NamingStrategy(使自动生成的列名大写)
我需要让Hibernate从实体开始自动生成数据库,但是我希望它们都是大写的
这在过去很管用,现在我把列名和大写字母和小写字母弄乱了
我启用了
.setProperty("hibernate.hbm2ddl.auto", "create")
为了让Hibernate自动生成数据库,我创建了一个UppercaseNamingStrategy。java扩展org。冬眠cfg。改进的Naming策略
根据https://docs.jboss.org/hibernate/orm/5.0/manual/en-US/html_single/#configuration-namingstrategy
现在我应该
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.
配置。然而,setNamingStrategy()在Hibernate 5.0.6中似乎不再存在
我当然希望通过编程来实现(我没有.xml配置文件,也不想要它们)
注意:
使用
。setProperty(“hibernate.ejb.naming_strategy”、“my.project.hibernate.UppercaseNamingStrategy”)
也不管用,似乎完全被忽视了
# 1 楼答案
Hibernate 5为命名策略} ,它什么都不做。你可以扩展它
PhysicalNamingStrategy
和ImplicitNamingStrategy
使用了两个新接口。你只需要实现PhysicalNamingStrategy
。在为模型创建所有列名后,Hibernate会调用它。所以你可以把它改成大写。Hibernate默认使用^{通过这种方式,您可以使用
UpperCaseNamingStrategy
构建会话工厂我现在正在研究一个更复杂的命名策略。如果你感兴趣,可以参考Hibernate5NamingStrategy