简体   繁体   中英

How do I setNamingStrategy on Dropwizard HibernateBundle class without xml?

I am working on a project that uses dropwizard's hibernate bundle to get a session factory per the below docs:

http://www.dropwizard.io/0.7.1/docs/manual/hibernate.html

The project doesn't use any xml and only uses annotated classes for the configuration of the bundle just like in the example.

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();
    }
};

However, we have a use case where I need to prepend the environment to the table name of the DAO objects such that the @Table annotation gets overwritten.

I have a class which implements ImprovedNamingStrategy, per the below docs

http://docs.jboss.org/hibernate/orm/5.0/javadocs/org/hibernate/cfg/ImprovedNamingStrategy.html

But how do I hook the naming strategy into my dropwizard hibernate bundle. I would like to be able to do something like this...

hibernateBundle.setNamingStrategy(ImprovedNamingStrategy.Instance)

or

hibernateBundle.addAnnotatedClass(someHibernateNamingPropertyConfig)

https://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/cfg/Configuration.html#setNamingStrategy(org.hibernate.cfg.NamingStrategy)

However, the hibernateBundle API doesn't allow for any of this.

Looking through the source code of HibernateBundle

https://github.com/dropwizard/dropwizard/blob/master/dropwizard-hibernate/src/main/java/io/dropwizard/hibernate/HibernateBundle.java

You can see that it uses "import io.dropwizard.Configuration;" as opposed to org.hibernate.cfg which does expose all of these methods. I'm trying to avoid a major refactor so if there is a "hacky" way to force set the naming property on the bundle, then I'm okay with that.

Any ideas of where to go from here would be much appreciated.

Found a solution for this that works for me in Dropwizard 0.8.4 . Not sure if it can help you with Dropwizard 0.7.1, but it will definitely be helpful for readers coming from Google with the same question.

You can extend HibernateBundle and override configure() . This method is called just before SessionFactoryFactory is going to be built, with the Configuration object for it. You can then override the method to add any special configurations you might need.

Example:

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);
  }
}

This example uses ScanningHibernateBundle because it's the one I was using, but you can use HibernateBundle directly too.

我刚决定不使用Hibernate Bundle并创建第二个配置对象,它从Hibernate Bundle中读取值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM