Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在没有xml的Dropwizard HibernateBundle类上设置NamingStrategy?_Java_Hibernate_Dropwizard - Fatal编程技术网

Java 如何在没有xml的Dropwizard HibernateBundle类上设置NamingStrategy?

Java 如何在没有xml的Dropwizard HibernateBundle类上设置NamingStrategy?,java,hibernate,dropwizard,Java,Hibernate,Dropwizard,我正在从事一个项目,该项目使用dropwizard的hibernate捆绑包根据以下文档获取会话工厂: 项目不使用任何xml,只使用带注释的类来配置包,就像示例中一样 public class ExampleConfiguration extends Configuration { @Valid @NotNull @JsonProperty("database") private DataSourceFactory database = new Data

我正在从事一个项目,该项目使用dropwizard的hibernate捆绑包根据以下文档获取会话工厂:

项目不使用任何xml,只使用带注释的类来配置包,就像示例中一样

public class ExampleConfiguration extends Configuration {
     @Valid
     @NotNull
     @JsonProperty("database")
     private DataSourceFactory database = new DataSourceFactory();

     public DataSourceFactory getDataSourceFactory() {
         return database;
     }
}

private final HibernateBundle<ExampleConfiguration> hibernate = 
new HibernateBundle<ExampleConfiguration>(
    some.class
    ) {
    @Override
    public DataSourceFactory getDataSourceFactory(ExampleConfiguration configuration) {
    return configuration.getDataSourceFactory();
    }
};

但是,hibernateBundle API不允许这样做

浏览HibernateBundle的源代码

您可以看到,它使用“import io.dropwizard.Configuration;”而不是org.hibernate.cfg,后者公开了所有这些方法。我正试图避免一次大的重构,所以如果有一种“黑客”方式来强制设置bundle的命名属性,那么我可以接受


任何关于从这里开始的想法都将不胜感激。

我刚刚决定不使用Hibernate捆绑包,而是创建第二个从Hibernate捆绑包中读取值的配置对象。

在Dropwizard 0.8.4中找到了一个适合我的解决方案。不确定它是否能帮助您使用Dropwizard 0.7.1,但它肯定会对来自谷歌的有同样问题的读者有所帮助

您可以扩展
HibernateBundle
并覆盖
configure()
。这个方法是在即将构建SessionFactoryFactory之前调用的,它带有
Configuration
对象。然后可以重写该方法以添加可能需要的任何特殊配置

例如:

public abstract class DatabaseWithImprovedNamingStrategyBundle extends ScanningHibernateBundle {
  public DatabaseWithImprovedNamingStrategyBundle(String pckg) {
    super(pckg);
  }

  @Override
  protected void configure(Configuration configuration) {
    super.configure(configuration);
    configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
  }
}

本例使用扫描HibernateBundle,因为它是我正在使用的,但您也可以直接使用HibernateBundle。

对不起,您如何使用它?我尝试将其应用于OPs版本,但它不再接受构造函数中的ClassFile.class列表。你能告诉我你将如何在配置文件中实现这一点吗?你能用你的答案修改你的问题吗?或者把它作为答案发布吗?
hibernateBundle.addAnnotatedClass(someHibernateNamingPropertyConfig)
public abstract class DatabaseWithImprovedNamingStrategyBundle extends ScanningHibernateBundle {
  public DatabaseWithImprovedNamingStrategyBundle(String pckg) {
    super(pckg);
  }

  @Override
  protected void configure(Configuration configuration) {
    super.configure(configuration);
    configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
  }
}