我一直在尝试启动一个 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();
}
}