究竟该如何创建SessionFactory?(hibernate5.0.x以上版本)
试过目前几乎所有看到的代码。仍然报错,都指向SessionFactory创建的这行。。
官方那个给的
package test; import java.io.IOException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.Test; import bean.Message; public class MessageTest { private SessionFactory sessionfactory; private org.hibernate.Session session; private Transaction transaction; @Test public void test() throws IOException { // 创建一个SessionFactory对象 SessionFactory sessionFactory = null; final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() .configure().build(); try { sessionFactory = new MetadataSources(registry).buildMetadata() .buildSessionFactory(); } catch (Exception e) { // 出现异常需要销毁StandardServiceRegistry注册表 StandardServiceRegistryBuilder.destroy(registry); System.out.println("创建会化工厂失败"); } //2.创建一个Session对象 org.hibernate.Session session = sessionFactory.openSession(); session.beginTransaction(); // Configuration configuration = new Configuration().configure(); // sessionfactory = new Configuration().configure().buildSessionFactory(); // session = sessionfactory.getCurrentSession(); // transaction = session.beginTransaction(); // Configuration configuration = new Configuration(); // SessionFactory sf = configuration.buildSessionFactory(); // org.hibernate.Session session = sf.openSession(); // Transaction tx = session.beginTransaction(); // tx.begin(); // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); // SessionFactory sessionFactory = new MetadataSources(serviceRegistry) // .buildMetadata().buildSessionFactory(); // org.hibernate.Session session = sessionFactory.openSession(); // Transaction transaction = session.beginTransaction(); // // 创建配置对象 // Configuration configuration = new Configuration().configure(); // // 创建服务注册对象 // StandardServiceRegistryBuilder serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); // //创建会话工厂对象 // SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); // // //创建会话对象 // session = sessionfactory.openSession(); // //开启事务 // transaction = session.beginTransaction(); Message message = new Message(); message.setId("1"); message.setCommand("我是命令"); message.setContent("呢荣"); message.setDescription("我是描述"); session.save(message); session.getTransaction().commit(); session.close(); } }
2.版本2
package test; import java.io.IOException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.Test; import bean.Message; public class MessageTest { private SessionFactory sessionfactory; private org.hibernate.Session session; private Transaction transaction; @Test public void test() throws IOException { // // 创建一个SessionFactory对象 // SessionFactory sessionFactory = null; // final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() // .configure().build(); // try { // sessionFactory = new MetadataSources(registry).buildMetadata() // .buildSessionFactory(); // } catch (Exception e) { // // 出现异常需要销毁StandardServiceRegistry注册表 // StandardServiceRegistryBuilder.destroy(registry); // System.out.println("创建会化工厂失败"); // } // //2.创建一个Session对象 // // org.hibernate.Session session = sessionFactory.openSession(); // session.beginTransaction(); // Configuration configuration = new Configuration().configure(); // sessionfactory = new Configuration().configure().buildSessionFactory(); // session = sessionfactory.getCurrentSession(); // transaction = session.beginTransaction(); Configuration configuration = new Configuration(); SessionFactory sf = configuration.buildSessionFactory(); org.hibernate.Session session = sf.openSession(); Transaction tx = session.beginTransaction(); tx.begin(); // ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); // SessionFactory sessionFactory = new MetadataSources(serviceRegistry) // .buildMetadata().buildSessionFactory(); // org.hibernate.Session session = sessionFactory.openSession(); // Transaction transaction = session.beginTransaction(); // // 创建配置对象 // Configuration configuration = new Configuration().configure(); // // 创建服务注册对象 // StandardServiceRegistryBuilder serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); // //创建会话工厂对象 // SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); // // //创建会话对象 // session = sessionfactory.openSession(); // //开启事务 // transaction = session.beginTransaction(); Message message = new Message(); message.setId("1"); message.setCommand("我是命令"); message.setContent("呢荣"); message.setDescription("我是描述"); session.save(message); session.getTransaction().commit(); session.close(); } }
3.版本3
package test; import java.io.IOException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.Test; import bean.Message; public class MessageTest { private SessionFactory sessionfactory; private org.hibernate.Session session; private Transaction transaction; @Test public void test() throws IOException { // // 创建一个SessionFactory对象 // SessionFactory sessionFactory = null; // final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() // .configure().build(); // try { // sessionFactory = new MetadataSources(registry).buildMetadata() // .buildSessionFactory(); // } catch (Exception e) { // // 出现异常需要销毁StandardServiceRegistry注册表 // StandardServiceRegistryBuilder.destroy(registry); // System.out.println("创建会化工厂失败"); // } // //2.创建一个Session对象 // // org.hibernate.Session session = sessionFactory.openSession(); // session.beginTransaction(); // Configuration configuration = new Configuration().configure(); // sessionfactory = new Configuration().configure().buildSessionFactory(); // session = sessionfactory.getCurrentSession(); // transaction = session.beginTransaction(); // Configuration configuration = new Configuration(); // SessionFactory sf = configuration.buildSessionFactory(); // org.hibernate.Session session = sf.openSession(); // Transaction tx = session.beginTransaction(); // tx.begin(); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); SessionFactory sessionFactory = new MetadataSources(serviceRegistry) .buildMetadata().buildSessionFactory(); org.hibernate.Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); // // 创建配置对象 // Configuration configuration = new Configuration().configure(); // // 创建服务注册对象 // StandardServiceRegistryBuilder serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); // //创建会话工厂对象 // SessionFactory sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); // // //创建会话对象 // session = sessionfactory.openSession(); // //开启事务 // transaction = session.beginTransaction(); Message message = new Message(); message.setId("1"); message.setCommand("我是命令"); message.setContent("呢荣"); message.setDescription("我是描述"); session.save(message); session.getTransaction().commit(); session.close(); } }