问题描述
- 关于hibernate的一个问题
-
代码别写完以后总是显示:SessionFactory/Configuration cannot be resolved错误。为什么啊?是hibernate没配置正确吗?可我已经配置了Hibernate.cfg.xml文件了啊,怎么解决呢?package org.hibernate.entity;
import org.hibernate.*;
import org hibernate.cfg.*;public class HibernateUtil {
private static SessionFactory sessionFactory;
private static final ThreadLocal threadLocal=new ThreadLocal();static{ try { Configuration cfg=new Configuration().configure(); SessionFactory=cfg.buildSessionFactory(); } catch (Throwable ex) { throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory(){ return sessionFactory; } public static Session getSession() throws HibernateException{ Session session=(session)threadLocal.get(); if(session==null||session.isOpen()){ if(sessionFactory==null){ rebuildSessionFactory(); } session=(sessionFactory!=null)?sessionFactory.openSession():null; threadLocal.set(session); } return session; } public static void closeSession() throws HibernateException{ Session session=(session)threadLocal.get(); threadLocal.set(null); if(session!=null){ session.close(); } } public static void rebuildSessionFactory(){ try { configuration.configure(/hibernate.cfg.xml); sessionFactory=cofiguration.buildSessionFactory(); } catch (Exception e) { System.err.println("Error Creating SessionFactory!"); e.printStackTrace(); } } public static void shutdown(){ getSessionFactory.close(); }
}
解决方案
struts1 + hibernate3 做个一个网购项目的问题
解决方案二:
检查下import导入的类是不是Hibernate的,可能项目中有其他同名类被import了。
时间: 2024-11-03 02:59:52