欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Hibernate4 buildSessionFactory过时解决方案  

程序员文章站 2024-03-20 15:47:16
...
Hibernate4 buildSessionFactory过时解决方案(Annotation也是一样解决) .
今天想用Hibernate4时候发现原先的buildSessionFactory已经过时,查了一下文档得到如下解决方案~

[java] view plaincopyprint?
01.public void test() { 
02.        Configuration cfg = new Configuration(); 
03.        cfg.configure();         
04.        ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();          
05.        SessionFactory sf = cfg.buildSessionFactory(sr); 
06.                 
07.         
08.        Session session = sf.openSession(); 
09.        Transaction tx = session.beginTransaction(); 
10.        User user = new User(); 
11.        user.setBirthday(new Date()); 
12.        user.setName("neal"); 
13.         
14.        session.persist(user); 
15.        tx.commit(); 
16.        session.close(); 
17.        System.out.println("end"); 
18.    } 
public void test() {
Configuration cfg = new Configuration();
cfg.configure();
ServiceRegistry  sr = new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();        
SessionFactory sf = cfg.buildSessionFactory(sr);


Session session = sf.openSession();
Transaction tx = session.beginTransaction();
User user = new User();
user.setBirthday(new Date());
user.setName("neal");

session.persist(user);
tx.commit();
session.close();
System.out.println("end");
}