我对Spring很陌生,我可能会错过一些明显的东西。我正在开发一个可追溯到2008年左右的项目,该项目使用Spring(v4.2.5)和Hibernate(v3.5.6)。我已经从大约相同时间段的另一个项目中复制了一些代码,并且当前尝试从复制的代码中访问Hibernate Session。
我尝试过的一些事情,都为null
和HibernateTemplate
提供了HibernateSession
:
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的getter,就像我在 4尝试的那样,但仍然会返回null
。
有人能给我一个答案吗?我想要的只是我班上的Hibernate DB-Session。如果您需要查看任何其他.java或.xml文件的代码,请告诉我。
答案 0 :(得分:0)
好的,正如 @ M.Deinum 在评论中正确指出的那样,我在完全错误的地方看着我的问题。原来我忘了从其他项目的XML文件中复制一些String-beans,我从那里复制了java文件。
因此,在复制并稍微修改这些bean之后,只需使用Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);
以下是豆子:
<bean id="ourDaoTarget" class="our.dao.OurDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="ourDao" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>plus.dao.IOurDao</value>
</list>
</property>
<property name="target" ref="ourDaoTarget" />
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
</list>
</property>
</bean>
<bean id="messageProcessor" class="our.plus.MessageProcessor">
<property name="ourDao" ref="ourDao" />
</bean>
在使用Dao的MessageProcessor
类中,我必须添加一个默认构造函数和getter&amp;道的设定者。
再次感谢, @ M.Deinum