gpt4 book ai didi

java - Hibernate sessionFactory 的编程配置导致 'sessionFactory' 或 'hibernateTemplate' 是必需的

转载 作者:行者123 更新时间:2023-11-30 09:08:55 26 4
gpt4 key购买 nike

我正在关注 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());
}
}

session 配置:

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

报告配置:

@Configuration
public class ReportConfig {

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

报告道:

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 要求,但也许我误解了它的工作原理。

最佳答案

ReportConfig 更改为:

@Configuration
public class ReportConfig {

@Autowired
private SessionFactory sessionFactory;

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

关于java - Hibernate sessionFactory 的编程配置导致 'sessionFactory' 或 'hibernateTemplate' 是必需的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23291657/

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