有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate会话/模板返回null

我对春天很陌生,我可能错过了一些明显的东西。我正在做一个可以追溯到2008年左右的项目,它使用Spring(v4.2.5)和Hibernate(v3.5.6)。我从大约同一时间段的另一个项目中复制了一些代码,目前正在尝试从复制的代码访问Hibernate会话

我尝试过一些东西,它们都为HibernateTemplateHibernateSession提供了null

一,

org.hibernate.Session session = org.springframework.orm.hibernate3.SessionFactoryUtils
  .getSession(getSessionFactory(), true);
// getSessionFactory comes from the Parent class 
//   org.springframework.orm.hibernate3.support.HibernateDaoSupport

二,

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

三,

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

四,

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>

五,

我也尝试过this SO answer,结果也一样:

java.lang.IllegalArgumentException: No SessionFactory specified at org.springframework.util.Assert.notNull(Assert.java:115) ...


在代码中的大多数其他地方,它是在1中完成的,在其他项目中复制的代码中也是这样做的

我的猜测是,我需要为HibernateTemplate或Session的Spring bean提供@InjectObject@AutoWired一个getter,就像我在4中尝试过的那样,但它仍然返回null

谁能给我指个答案吗?我只想在我的课堂上进行Hibernate DB会话。如果你需要查看任何其他的代码。java或。xml文件让我知道


共 (1) 个答案

  1. # 1 楼答案

    好吧,正如@M.Deinum在评论中正确指出的那样,我在完全错误的地方看待我的问题。事实证明,我忘了从另一个项目的XML文件中复制一些String Bean,我从中复制了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&;刀的设定者

    再次感谢,@M.Deinum