官方很简单的HibernateUtil.java,可以针对自己的需求改进。
对了,工具类大多都是静态的,为什么呢,留下这个问题来思考。
package org.bbjava.funny.test;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new Configuration().configure("org/bbjava/funny/test/cfg/hibernate.cfg.xml").buildSessionFactory();
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
对了,工具类大多都是静态的,为什么呢,留下这个问题来思考。