0

我一直在尝试启动一个 Spring / Hibernate 项目,但似乎无法让它运行。我收到以下错误。我想我已经接近让它工作了,但我显然错过了一些东西。有人可以告诉我我的代码有什么问题吗?我很想知道,因为我花了 4 天的时间试图让它发挥作用,但一无所获。

这是我得到的错误:

Exception in thread "main" org.hibernate.HibernateException: No CurrentSessionContext configured!
    at org.hibernate.impl.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:542)
    at cmsutil.dao.ContentComponentDAOImpl.getContentComponents(ContentComponentDAOImpl.java:37)
    at cmsutil.util.Driver.main(Driver.java:22)

应用上下文

  <context:property-placeholder location="jdbc.properties"/>

  <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.url}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}"/>

  <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:configurationClass="org.hibernate.cfg.AnnotationConfiguration"
        p:packagesToScan="cmsutil.dao">
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
        <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
        <prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
      </props>
    </property>
    <property name="eventListeners">
      <map>
        <entry key="merge">
          <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
        </entry>
      </map>
    </property>

  </bean>

  <tx:annotation-driven transaction-manager="txnManager"/>

  <bean id="txnManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager"
        p:sessionFactory-ref="sessionFactory"/>
</beans>

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping resource="contentcomponent.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository("contentComponentDAO")
public class ContentComponentDAOImpl implements ContentComponentDAO {

    @Autowired
    private SessionFactory sessionFactory;


    public ContentComponentDAOImpl(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }


    public ContentComponentDAOImpl() {
    }

    @Override
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }


    @Override
    public List<ContentComponent> getContentComponents() throws DAOException {
        Session session = sessionFactory.getCurrentSession();
        List<ContentComponent> documents;

        Query query = session.getNamedQuery("ContentComponent.findAllContentComponents");
        documents = query.list();
        return documents;
    }

最后,测试驱动程序:

package cmsutil.util;

import cmsutil.dao.ContentComponentDAOImpl;
import cmsutil.exceptions.DAOException;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Driver {

    public static void main(String[] args) throws DAOException {
        AbstractApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
        Session session = sessionFactory.openSession();
        ContentComponentDAOImpl c = new ContentComponentDAOImpl();
        c.setSessionFactory(sessionFactory);
        List l = c.getContentComponents();
    }
}
4

2 回答 2

0

您的测试驱动程序没有任何意义。如果要确保 Spring 应用程序上下文正常工作,请使用从上下文中获取的 bean:

public static void main(String[] args) throws DAOException {
    AbstractApplicationContext factory = new ClassPathXmlApplicationContext("applicationContext.xml");        
    ContentComponentDAO c = factory.getBean(ContentComponentDAO.class);
    List l = c.getContentComponents();  
}

您也不需要hibernate.cfg.xml,因为在您的情况下,Hibernate 是在 Spring 应用程序上下文中配置的。

于 2012-04-16T16:13:32.857 回答
0

您的 dao 不是由 spring 管理的,您只是手动创建了一个 dao 实例。因此,不会自动装配任何内容。

您有两个选择 - 允许将休眠会话注入 dao(作为构造函数中的参数传入)。或者从加载的应用上下文中调用你的 bean。

如果是为了单元测试,你可以传入一个模拟对象。

于 2012-04-16T16:09:19.570 回答