简体   繁体   English

Hibernate Session / Template返回null

[英]Hibernate Session/Template returns null

I'm pretty new to Spring, and I probably miss something obvious here. 我是Spring的新手,我可能想念一些显而易见的东西。 I'm working on a project that dates back to 2008 or so, which uses Spring (v4.2.5) and Hibernate (v3.5.6). 我正在研究一个可以追溯到2008年左右的项目,该项目使用Spring(v4.2.5)和Hibernate(v3.5.6)。 I've copied some code from another project of around the same time-period, and currently try to access the Hibernate Session from that copied code. 我已经从大约相同时间段的另一个项目中复制了一些代码,并且当前尝试从该复制的代码访问Hibernate Session。

Some things I've tried, which all gave back null for both the HibernateTemplate and HibernateSession : 我尝试过的一些事情,都为HibernateTemplateHibernateSession都返回了null

1. 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. 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. 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. 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. 5,

I've also tried this SO answer with the same result: 我也尝试过用相同的结果回答这个问题

java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:115) ... java.lang.IllegalArgumentException:在org.springframework.util.Assert.notNull(Assert.java:115)处未指定SessionFactory ...


On most other places in the code it's done like at 1 , and it was also done like this in the copied code in the other project. 在代码中的大多数其他地方,它都像在1处那样完成,并且在其他项目的复制代码中也这样做。

My guess is that I need to @InjectObject or @AutoWired a getter for the Spring-bean of either the HibernateTemplate or Session, something like I've tried at 4 , but it remains returning null . 我的猜测是,我需要@InjectObject@AutoWired@InjectObject HibernateTemplate或Session的Spring bean的吸气剂,就像我在4尝试过的那样,但它仍然返回null

Could anyone point me to an answer? 谁能指出我的答案? All I want is a Hibernate DB-Session in my class. 我想要的只是我班上的一个Hibernate DB-Session。 If you need to see the code of any other .java or .xml files let me know. 如果您需要查看其他任何.java或.xml文件的代码,请告诉我。

Ok, as correctly pointed out by @M.Deinum in the comments, I was looking at my problem at the completely wrong spot. 好的,正如@ M.Deinum在评论中正确指出的那样,我正在完全错误的位置查看我的问题。 It turned out I forgot to copy a few String-beans from the other project's XML-files where I copied the java files from. 原来,我忘了从另一个项目的XML文件中复制一些String-beans,而我从中复制了Java文件。
So, after copying and slightly modifying those beans it worked by just using Session session = SessionFactoryUtils.getSession(getSessionFactory(), true); 因此,在复制并稍微修改了这些bean之后,仅使用Session session = SessionFactoryUtils.getSession(getSessionFactory(), true);

Here are the beans: 这是豆子:

<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>

And in the MessageProcessor class where the Dao was used I had to add a default constructor and getter & setter for the Dao. 在使用Dao的MessageProcessor类中,我必须为Dao添加默认的构造函数以及getter和setter。

Again thanks, @M.Deinum 再次感谢@ M.Deinum

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM