Hibernate sessionFactory的编程配置导致了' sessionFactory'或者' hibernateTemplate'是必须的

时间:2014-04-25 11:23:05

标签: java spring hibernate

我正在关注spring doc entry关于在独立应用程序中以编程方式设置我的应用程序上下文。

主:

public class Reporter {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SessionConfig.class, ReportConfig.class);
        ReportDao reportDao = ctx.getBean(ReportDao.class);
        Report report = reportDao.getReport("an_id");
        System.out.println(report.getReportName());
    }
}

SessionConfig:

@Configuration
public class SessionConfig {

    @Bean
    public HibernateTransactionManager getTransationManager() {
        return new HibernateTransactionManager(getSessionFactory());
    }

    @Bean
    public SessionFactory getSessionFactory() {
        LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder(new MyDataSource());
        builder.addAnnotatedClass(Report.class);
        builder.scanPackages("com.mystuff.dao");
        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        hibernateProperties.put("hibernate.connection.provider_class", "com.mystuff.dao.MyConnectionProvider");
        builder.addProperties(hibernateProperties);
        return builder.buildSessionFactory();
    }
}

ReportConfig:

@Configuration
public class ReportConfig {

    @Bean
    public ReportDao reportDao(){
        return new ReportDaoImpl();
    }
}

ReportDao:

public interface ReportDao {
    /** Add or update the given report. */
    void updateReport(Report report);
    /** @return all reports. */
    List<Report> getReports();
    /** @return report for the given identifier */
    Report getReport(String id);
}

ReportDaoImpl:

@Repository
public class ReportDaoImpl extends HibernateDaoSupport implements ReportDao {
    @Override
    public void updateReport(Report report) {
        getHibernateTemplate().saveOrUpdate(report);
    }

    @Override
    public List<Report> getReports() {
        return (List<Report>)getHibernateTemplate().find("from Report");
    }

    @Override
    public Report getReport(String id) {
        return (Report) getHibernateTemplate().get(Report.class, id);
    }
}

结果:

    2014-04-25 11:39:22,215 INFO AbstractApplicationContext.java{1}:513> Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3527c4a: startup date [Fri Apr 25 11:39:22 BST 2014]; root of context hierarchy
2014-04-25 11:39:22,435 INFO AutowiredAnnotationBeanPostProcessor.java{1}:141> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2014-04-25 11:39:36,806 INFO JavaReflectionManager.java{1}:66> HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
2014-04-25 11:39:36,817 INFO Version.java{1}:54> HHH000412: Hibernate Core {4.3.5.Final}
2014-04-25 11:39:36,822 INFO Environment.java{1}:239> HHH000206: hibernate.properties not found
2014-04-25 11:39:36,824 INFO Environment.java{1}:346> HHH000021: Bytecode provider name : javassist
2014-04-25 11:39:36,993 INFO ConnectionProviderInitiator.java{1}:190> HHH000130: Instantiating explicit connection provider: com.mystuff.dao.MyConnectionProvider
2014-04-25 11:39:37,549 INFO Dialect.java{1}:145> HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2014-04-25 11:39:37,567 INFO LobCreatorBuilder.java{1}:123> HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
2014-04-25 11:39:37,789 INFO TransactionFactoryInitiator.java{1}:62> HHH000399: Using default transaction strategy (direct JDBC transactions)
2014-04-25 11:39:37,796 INFO ASTQueryTranslatorFactory.java{1}:47> HHH000397: Using ASTQueryTranslatorFactory
2014-04-25 11:39:43,436 INFO HibernateTransactionManager.java{1}:339> Using DataSource [com.mystuff.dao.MyDataSource@33162991] of Hibernate SessionFactory for HibernateTransactionManagerException in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'reportDao' defined in class com.mystuff.reporting.ReportConfig: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at com.mystuff.reporting.Reporter.main(Reporter.java:14)
Caused by: java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    at org.springframework.orm.hibernate4.support.HibernateDaoSupport.checkDaoConfig(HibernateDaoSupport.java:117)
    at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
    ... 11 more

我期待SessionConfig Bean满足sessionFactory要求,但也许我误解了它是如何工作的。

1 个答案:

答案 0 :(得分:2)

ReportConfig更改为:

@Configuration
public class ReportConfig {  

    @Autowired
    private SessionFactory sessionFactory;

    @Bean
    public ReportDao reportDao(){
        final ReportDaoImpl reportDao = new ReportDaoImpl();
        reportDao.setSessionFactory(sessionFactory);
        return reportDao; 
    }
}