gpt4 book ai didi

java - 没有 xml 文件的 Hibernate4 配置 - sessionFactory 为空

转载 作者:搜寻专家 更新时间:2023-10-31 08:14:59 25 4
gpt4 key购买 nike

我有一个使用 Spring3 和 Hibernate4 的 Web 项目,现在我想在不使用 xml 文件的情况下测试 DAO。为此,我创建了一个类,该类使用应用程序的 xml 文件中包含的数据和一个简单的测试类来创建 LocalSessionFactoryBean。

但是,localSessionFactoryBean.getObject() 返回的sessionFactory 为空。我一直在看一些例子,比如 this当我修改它们以在没有 Spring 的情况下运行时,它们也有同样的问题。你有什么想法吗?

这是准备 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;
}
}

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

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

最佳答案

尝试添加这一行

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);
// Added the below line
localSessionFactoryBean.afterPropertiesSet();
return localSessionFactoryBean;
}

link可能会增加对问题的更多见解。

根据文档,

public void afterPropertiesSet() 抛出 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.

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

关于java - 没有 xml 文件的 Hibernate4 配置 - sessionFactory 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23885579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com