2

I'm looking to convert this XML to Java Config for Spring:

<bean id="hibernateConfiguration" factory-bean="&amp;sessionFactory"
    factory-method="getConfiguration" />

And I'm thinking...

@Bean
public org.hibernate.cfg.Configuration configuration() {

    org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();

    //uhhh...

    return config;
}

How would I specify the factory-bean and factory-method params? Tried looking around on SO, but no luck.

Aside: the &amp; tells Spring to fetch the actual bean

UPDATE:

Here is how I am using the bean. It's a listener that prints out the schema.

@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

       log.debug("onApplicationEvent");

       if (isPrintSchemaEnabled()) {
          log.info("printing schema");

          SchemaExport schema = new SchemaExport(hibernateConfiguration);
          schema.setDelimiter(BaseConstants.SEMI_COLON);

          if(isCreateOutputFile()) {
             schema.setOutputFile(getSchemaOutputPath());
          }
          schema.create(true, false);
       }
    }

    public static boolean isPrintSchemaEnabled() {
       return Boolean.valueOf(getResourceBundle().getString(PRINT_SCHEMA_ENABLED));
    }

    public static boolean isCreateOutputFile() {
       return Boolean.valueOf(getResourceBundle().getString(OUTPUT_FILE_ENABLED));
    }

    public static String getSchemaOutputPath() {
       return getResourceBundle().getString(SCHEMA_OUTPUT_PATH);
    }

    @Autowired
    private Configuration hibernateConfiguration;

    public static final String PRINT_SCHEMA_ENABLED = "enablePrintSchema";
    public static final String SCHEMA_OUTPUT_PATH = "schemaOutputPath";
    public static final String OUTPUT_FILE_ENABLED = "enableSchemaOutputFile";

    private static final Logger log = LoggerFactory.getLogger(SchemaExportListener.class);

}

As for more XML, it's not all relevant, but since it was requested:

  <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
    <!--Annotated classes redacted -->
    <property name="hibernateProperties">
      <props>       
         <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>         
         <prop key="hibernate.show_sql">false</prop>
         <prop key="hibernate.format_sql">true</prop>
         <prop key="hibernate.hbm2ddl.auto">create</prop>
      </props>
    </property>
  </bean>

  <bean id="dataSource" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
    <constructor-arg ref="hikariConfig" />
  </bean>

  <bean id="hikariConfig" class="com.zaxxer.hikari.HikariConfig">
    <property name="poolName" value="springHikariConnectionPool" />
    <property name="connectionTestQuery" value="SELECT 1" />
    <property name="dataSourceClassName" value="${dataSource.dataSourceClassName}" />
    <property name="maximumPoolSize" value="10" />
    <property name="idleTimeout" value="30000" />

    <property name="dataSourceProperties">
      <props>
            <prop key="url">${dataSource.url}</prop>
            <prop key="user">${dataSource.username}</prop>
            <prop key="password">${dataSource.password}</prop>
      </props>
    </property>
  </bean>
4
  • It will be helpful if you could add more xml configuration so that it helps us to provide an equivalent java configuration. Commented Jun 25, 2015 at 5:30
  • Why aren't you simply using the LocalSessionFactoryBean or AnnotationSessionFactoryBean? Commented Jun 25, 2015 at 5:47
  • @sarfaraz Added further detail to question Commented Jun 25, 2015 at 15:11
  • @M.Deinium - I'm using the LocalSessionFactoryBean as noted in the additional XML that I posted Commented Jun 25, 2015 at 15:12

2 Answers 2

2

Don't think that you'll need to mention the factory bean or methods in the java config.In your class wire the sessionFactory in class and see if it works

@Component
public class SchemaExportListener extends AbstractEnvironment implements ApplicationListener<ContextRefreshedEvent> {

@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

       log.debug("onApplicationEvent");

       if (isPrintSchemaEnabled()) {
          log.info("printing schema");

          SchemaExport schema = new SchemaExport(localSessionFactoryBean.getConfiguration());
          schema.setDelimiter(BaseConstants.SEMI_COLON);

In Java configuration class

@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;

@Bean
public DataSource getDataSource() {
  //return the data source here
}

@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
    return localSessionFactoryBean.getConfiguration();
}

If the autowire of session object fails then try to create on Object in the config like below

@Autowired
@Bean
public LocalSessionFactoryBean getSessionFactoryBean(DataSource dataSource) {
    LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(this.getDataSource());
  //sessionFactory.setHibernateProperties(this.hibernateProperties());

    return localSessionFactoryBean;
 }

For the rest of the configuration refer to some guide or example Here are few example1 example2

Hope this will solve your issue

Sign up to request clarification or add additional context in comments.

1 Comment

It's still considered a work-around to the actual question, but I am accepting this as the solution (the last section worked for me, rather than providing the getConfig() method).
0

You can just autowire the LocalSessionFactoryBean and get the configuration

@Autowired
private LocalSessionFactoryBean localSessionFactoryBean;

@Bean(name = "hibernateConfig")
public org.hibernate.cfg.Configuration getConfig() {
    return localSessionFactoryBean.getConfiguration();
}

2 Comments

Saw an exception with this work-around: Factory method 'getHibernateConfig' threw exception; nested exception is java.lang.IllegalStateException: Configuration not initialized yet
(I renamed the method to getHibernateConfig())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.