在Hibernate 5中实现NamingStrategy(使自动生成的列名称为UPPERCASE) [英] Implementing a NamingStrategy in Hibernate 5 (make autogenerated column names UPPERCASE)

查看:1626
本文介绍了在Hibernate 5中实现NamingStrategy(使自动生成的列名称为UPPERCASE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让Hibernate从实体开始自动生成数据库,但是我希望它们全部大写。



这个过去的工作现在我已经搞砸了用大写和小写字母填充列名。



我启用了

  .setProperty(hibernate.hbm2ddl.auto,create)

让Hibernate auto生成数据库,并且我创建了一个可扩展 org.hibernate.cfg.ImprovedNamingStrategy 的UppercaseNamingStrategy.java。 根据 https://docs.jboss.org/hibernate/orm/5.0/ manual / en-US / html_single /#configuration-namingstrategy



现在我应该


在添加映射之前,您可以通过调用
Configuration.setNamingStrategy()来指定不同的策略:

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

org.hibernate.cfg.ImprovedNamingStrategy是一种内置策略,
可能是对于某些应用程序来说,这是一个有用的起点。

然而,Configuration.setNamingStrategy()在Hibernate 5.0.6中似乎没有更多。



我当然希望以编程方式进行编程(我没有配置文件,不需要它们)。

注意: 使用

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

不起作用,似乎完全被忽略...

$ hibernate 5为名称策略使用了两个新接口 PhysicalNamingStrategy
ImplicitNamingStrategy 。你只需要实现 PhysicalNamingStrategy 。在为模型创建所有列名后,由Hibernate调用它。所以你可以使它大写。 Hibernate默认使用 PhysicalNamingStrategyStandardImpl ,它什么都不做。您可以扩展它。

  public class UpperCaseNamingStrategy extends PhysicalNamingStrategyStandardImpl {

@Override
public标识符toPhysicalColumnName(标识符名称,JdbcEnvironment上下文){
return context.getIdentifierHelper()。toIdentifier(
StringUtils.upperCase(name.getText(),Locale.ENGLISH));
}

}

您可以使用 UpperCaseNamingStrategy 通过这种方式

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

现在我正在研究更复杂的名称策略。你可以参考 Hibernate5NamingStrategy ,如果你有兴趣。

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...

解决方案

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.

这篇关于在Hibernate 5中实现NamingStrategy(使自动生成的列名称为UPPERCASE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆