【发布时间】:2017-02-18 15:56:38
【问题描述】:
我对 Spring 很陌生,我可能在这里错过了一些明显的东西。我正在开发一个可以追溯到 2008 年左右的项目,该项目使用 Spring (v4.2.5) 和 Hibernate (v3.5.6)。我从大约同一时间段的另一个项目中复制了一些代码,目前尝试从复制的代码中访问 Hibernate Session。
我尝试过的一些事情,对于HibernateTemplate 和HibernateSession 都返回了null:
1.
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
.getSession(getSessionFactory(), true);
// getSessionFactory comes from the Parent class
// org.springframework.orm.hibernate3.support.HibernateDaoSupport
2.
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
.getSession(HibernateUtil.getSessionFactory(), true);
// Where HibernateUtil is our own factory-class:
import java.io.PrintStream;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
static {
try {
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
}
3.
org.springframework.orm.hibernate3.HibernateTemplate hibernateTemplate = getHibernateTemplate();
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
.getSession(hibernateTemplate.getSessionFactory(), true);
// getHibernateTemplate comes from the Parent class
// org.springframework.orm.hibernate3.support.HibernateDaoSupport
4.
org.springframework.orm.hibernate3.HibernateTemplate hibernateTemplate = getSpringHibernateTemplate();
org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
.getSession(hibernateTemplate.getSessionFactory(), true);
// Where getSpringHibernateTemplate is:
@InjectObject("spring:hibernateTemplate")
public abstract HibernateTemplate getSpringHibernateTemplate();
// With the following bean in our ourprojectname-general.xml:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingDirectoryLocations">
<!-- Add all Hibernate mappings to the list below -->
<list>
<value>/WEB-INF/hbm</value>
</list>
</property>
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="net.sf.ehcache.configurationResourceName">spinoff/dao/ehcache.xml</prop>
<!-- Determines the size of the JDBC fetch, that is the maximum number
of rows that are exchanged between Hibernate and the database in one go.
It's been increased to reduce overhead when searching for all entiteits -->
<prop key="hibernate.jdbc.fetch_size">500</prop>
<prop key="hibernate.dialect">spinoff.objects.spatial.oracle.OracleSpatialDialect</prop>
<prop key="hibernate.default_batch_fetch_size">25</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="use_sql_comments">false</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<prop key="hibernate.connection.release_mode">after_transaction</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
5.
我也试过this SO answer,结果相同:
java.lang.IllegalArgumentException:未指定 SessionFactory 在 org.springframework.util.Assert.notNull(Assert.java:115) ...
在代码中的大多数其他地方,它就像在 1 处一样,并且在其他项目的复制代码中也是如此。
我的猜测是我需要 @InjectObject 或 @AutoWired 获取 HibernateTemplate 或 Session 的 Spring-bean,就像我在 4 尝试过的一样,但它仍然存在返回null。
谁能指出我的答案?我想要的只是我班上的一个 Hibernate DB-Session。如果您需要查看任何其他 .java 或 .xml 文件的代码,请告诉我。
【问题讨论】:
-
我强烈希望你不要做这两件事……尤其是选项 2!你不应该自己管理你的会话,让 spring 来做。你唯一应该做的就是
sessionFactory.getCurrentSession(),忘记HibernateTemplate。此外,如果您真的想使用HibernateTemplate和普通会话,请使用SessionCallback而不是自己获取会话(这也意味着您需要自己关闭它并自己停止/启动 tx,如果您不这样做,您将最终会耗尽数据库连接!)。 -
也不知道
@InjectObject应该是什么和/或你的daos是如何配置/使用的。您发布了很多没有提供所需洞察力的代码。SessionFactory和/或HibernateTemplate不能是null,因为如果这样,您甚至无法启动您的应用程序,它会引发异常。所以我怀疑你正在做类似new WhateverYourDaoIsCalled()之类的事情,而不是使用 Spring 托管和配置的实例。 -
@M.Deinum 啊,你的最后一句话确实是正确的:“所以我怀疑你正在做类似 new WhatYourDaoIsCalled() 之类的事情,而不是使用 Spring 托管和配置的实例。".. 我们有代码
Dao getDao(){ if(this.dao == null) this.dao = new Dao(); return this.dao; },它很可能创建了道,因为它是null。我将把这个 Dao 作为 Spring-bean 添加到 XML 中,看看它是否不再为空。 (然后阅读更多关于 Spring 的一般信息。>.>) -
只添加它可能没有帮助,您还必须注入它。
标签: java spring hibernate session properties