学习hibernate 谁能给我解释一段下面的代码 很基础
public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}
}
尤其是下面这段------------------------------------------------
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
加上了个static 是什么意思 有什么用 所谓的静态代码 在什么情况下执行啊
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s=(Session)session.get();
//Open a new Session, if this Thread has none yet
if (s==null) {
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s=(Session) session.get();
session.set(null);
if (s !=null)
s.close();
}
}
尤其是下面这段------------------------------------------------
static {
try{
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
log.error("Initial SessionFactory creation failed.",ex);
throw new ExceptionInInitializerError(ex);
}
}
加上了个static 是什么意思 有什么用 所谓的静态代码 在什么情况下执行啊
...全文
相关推荐
Hibernate基础学习源码
本资源是 学习Hibernate 的时候写的源码 ,有兴趣的可以下载下来看看
Hibernate学习代码示例
示例中包括:基于底层的JDBC对数据库的操作和利用hibernate对数据库操作的对比,其中hibernate的实体关系映射包括(一对多,多对多)还有与Spring的整合例子。
Hibernate基础学习
1. 熟悉Hibernate框架和相关配置文件; 2. 熟悉DAO模式,以及Hibernate实现的DAO代码; 3. Hibernate实现分页查询。
Hibernate基础代码
一个 hibernate基础小程序,希望能帮到刚开始学习入门的同学们!
J2EE学习资料,java基础,spring,hibernate,struts等资料
J2EE学习资料,java基础,spring,hibernate,struts等资料,个人收藏的,分享出来


Web 开发

