没有xml文件的Hibernate4配置 - sessionFactory为空 [英] Hibernate4 configuration without xml files - sessionFactory is null

查看:190
本文介绍了没有xml文件的Hibernate4配置 - sessionFactory为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Spring3和Hibernate4的Web项目,现在我想测试DAO而不使用xml文件。为此,我创建了一个类,该类使用应用程序的xml文件中包含的数据和简单的测试类创建LocalSessionFactoryBean。然而,由 localSessionFactoryBean.getObject()返回的 sessionFactory 为空。 我一直在寻找一些例子,比如这个,当我修改它们时它们也有同样的问题没有Spring的情况下运行。你有什么想法吗?



这是准备sessionFactory的代码:

  @Configuration 
@Transactional
@EnableTransactionManagement
@ComponentScan({com.company})
public class HibernateInitializator {

public SessionFactory getSessionFactory(){

属性hibernateProperties = getHibernateProperties();
DataSource dataSource = getDatasourceConfiguration();
LocalSessionFactoryBean localSessionFactoryBean = generateSessionFactoryBean(new String [] {com.company},
dataSource,hibernateProperties);
SessionFactory sessionFactory = localSessionFactoryBean.getObject();

HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);

return sessionFactory;

$ b $ private DataSource getDatasourceConfiguration(){

DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(com.mysql.jdbc.Driver);
dataSource.setUrl(jdbc:mysql:// localhost:3306 / dbName);
dataSource.setUsername(username);
dataSource.setPassword(password);

返回dataSource;

$ b private static LocalSessionFactoryBean generateSessionFactoryBean(String [] basePackage,DataSource dataSource,
Properties hibernateProperties){

LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource);
localSessionFactoryBean.setPackagesToScan(basePackage);
localSessionFactoryBean.setHibernateProperties(hibernateProperties);

返回localSessionFactoryBean;


私有静态属性getHibernateProperties(){

属性hibernateProperties = new Properties();
hibernateProperties.put(hibernate.dialect,org.hibernate.dialect.MySQL5InnoDBDialect);
hibernateProperties.put(hibernate.show_sql,false);
hibernateProperties.put(hibernate.generate_statistics,false);
hibernateProperties.put(hibernate.hbm2ddl.auto,update);
hibernateProperties.put(hibernate.use_sql_comments,false);

返回hibernateProperties;






$ b这是一个使用它的简单测试类:

  public class GenericDAOHibernateTest {

private GenericDAOHibernate dao;

@BeforeTest
private void testInitialization(){

dao = new GenericDAO();
HibernateInitializator initializator = new HibernateInitializator();
SessionFactory sessionFactory = initializator.getSessionFactory();
dao.setSessionFactory(sessionFactory);
}

@Test(description =检查返回用户列表)
public void shouldReturnsUserList()抛出SQLException异常{

List< ;对象[]> openResultSetList = dao.doSomeOperation();
...
}
}


解决方案

尝试添加这一行

  localSessionFactoryBean.afterPropertiesSet(); 

在属性 LocalSessionFactoryInstance 已经设置好了。您的方法将如下所示:

  private static LocalSessionFactoryBean generateSessionFactoryBean(String [] basePackage,DataSource dataSource,
Properties hibernateProperties){
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(dataSource);
localSessionFactoryBean.setPackagesToScan(basePackage);
localSessionFactoryBean.setHibernateProperties(hibernateProperties);
//添加了以下行
localSessionFactoryBean.afterPropertiesSet();
返回localSessionFactoryBean;
}

链接可能会增加对问题的更多洞察。



根据文档,

public void afterPropertiesSet()throws IOException
$ b


在BeanFactory设置完所有Bean后调用它提供的属性(并满足BeanFactoryAware和ApplicationContextAware)。
此方法允许bean实例只在所有bean属性设置完成时才能执行初始化,并在发生配置错误时抛出异常。


在您的情况下,我认为您需要手动在代码中调用它。


I have a web project working with Spring3 and Hibernate4 and now I want to test the DAOs without using the xml files. To do so I have created a class that creates a LocalSessionFactoryBean with the data contained in the application's xml file and a simple test class.

However, the sessionFactory returned by localSessionFactoryBean.getObject() is null. I have been looking at some examples like this and they have the same problem when I modify them to run without Spring. Do you have any idea?

This is the code that prepares the sessionFactory:

@Configuration
@Transactional
@EnableTransactionManagement
@ComponentScan({ "com.company" })
public class HibernateInitializator {

    public SessionFactory getSessionFactory() {

        Properties hibernateProperties = getHibernateProperties();
        DataSource dataSource = getDatasourceConfiguration();
        LocalSessionFactoryBean localSessionFactoryBean = generateSessionFactoryBean(new String[] { "com.company" },
            dataSource, hibernateProperties);
        SessionFactory sessionFactory = localSessionFactoryBean.getObject();

        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return sessionFactory;
    }

    private DataSource getDatasourceConfiguration() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/dbName");
        dataSource.setUsername("username");
        dataSource.setPassword("password");

        return dataSource;
    }

    private static LocalSessionFactoryBean generateSessionFactoryBean(String[] basePackage, DataSource dataSource,
        Properties hibernateProperties) {

        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource);
        localSessionFactoryBean.setPackagesToScan(basePackage);
        localSessionFactoryBean.setHibernateProperties(hibernateProperties);

        return localSessionFactoryBean;
    }

    private static Properties getHibernateProperties() {

        Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        hibernateProperties.put("hibernate.show_sql", false);
        hibernateProperties.put("hibernate.generate_statistics", false);
        hibernateProperties.put("hibernate.hbm2ddl.auto", "update");
        hibernateProperties.put("hibernate.use_sql_comments", false);

        return hibernateProperties;
    }
}

And this is a simple test class that uses it:

public class GenericDAOHibernateTest {

    private GenericDAOHibernate dao;

    @BeforeTest
    private void testInitialization() {

        dao = new GenericDAO();
        HibernateInitializator initializator = new HibernateInitializator();
        SessionFactory sessionFactory = initializator.getSessionFactory();
        dao.setSessionFactory(sessionFactory);
    }

    @Test(description = "Checks that returns the user list ")
    public void shouldReturnsUserList() throws SQLException, Exception {

        List<Object[]> openResultSetList = dao.doSomeOperation();
        ...
    }
}

解决方案

Try adding this line

localSessionFactoryBean.afterPropertiesSet();

in the method after the properties of LocalSessionFactoryInstance has been set. Your method will be as

private static LocalSessionFactoryBean generateSessionFactoryBean(String[] basePackage, DataSource dataSource,
        Properties hibernateProperties) {
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setDataSource(dataSource);
        localSessionFactoryBean.setPackagesToScan(basePackage);
        localSessionFactoryBean.setHibernateProperties(hibernateProperties);
        // Added the below line
        localSessionFactoryBean.afterPropertiesSet();
        return localSessionFactoryBean;
    }

This link may add more insight to the problem.

As per Documentation,

public void afterPropertiesSet() throws IOException

Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware). This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

In your case, I think you need to call it in your code manually.

这篇关于没有xml文件的Hibernate4配置 - sessionFactory为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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