cacaopie 2015-08-11 00:55 采纳率: 14.3%
浏览 1253

关于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();
}

}


  • 写回答

1条回答 默认 最新

  • 毕小宝 博客专家认证 2015-08-29 07:29
    关注

    检查下import导入的类是不是Hibernate的,可能项目中有其他同名类被import了。

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算