日期:2014-05-17  浏览次数:20723 次

buildSessionFactory被废弃了,该怎么写呢?
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().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;
  }

}

但是那个buildSessionFactory方法,在hibernate4.1中被废弃了,该怎样修改方法呢?谢谢

------解决方案--------------------
Java code

Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory(new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry());