Hibernate - ServiceRegistryBuilder

前端 未结 9 1875
眼角桃花
眼角桃花 2020-12-28 08:30

I\'m just trying to learn Hibernate (version 4 final) but I have a problem when trying to create the session factory. Here is some code related to the problem:

hi

9条回答
  •  情话喂你
    2020-12-28 09:15

    The methods buildSessionFactory and ServiceRegistryBuilder in Hibernate 4.3.4 are deprecated.

    The right code is here.

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    
    
    .....
    
        Configuration conf = new Configuration()
                  .configure();
    
    
        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    
    
        SessionFactory sf = conf.buildSessionFactory(sr);
    
        Session session = sf.openSession();
    
        session.beginTransaction();
    
    
        YourDominClass ydc = new YourDominClass();
    
        ydc.setSomething("abcdefg");
    
        session.save(ydc);
    
        session.getTransaction().commit();
    
        session.close();
    
        sf.close();
                ........
    

提交回复
热议问题