buildSessionFactory()在hibernate 4中被弃用了吗?
当我将Hibernate的版本从3.6.8.final更新到4.0.0.final时,我在这一行中收到了有关不推荐使用的方法buildSessionFactory()
的警告:
private static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
javadocbuild议使用另一种方法
buildSessionFactory(ServiceRegistry serviceRegistry)
但在文档中我发现不推荐使用的变体:(
你能帮我解决这个小小的误解吗?
是的,它被弃用。 用下面的代替你的SessionFactory:
在Hibernate 4.0,4.1,4.2中
private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; public static SessionFactory createSessionFactory() { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()). buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; }
更新:
在Hibernate 4.3 ServiceRegistryBuilder已被弃用 。 改用以下代码。
serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build();
是的,它已被弃用。 http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/cfg/Configuration.html#buildSessionFactory ()明确告诉你使用你find的其他方法( buildSessionFactory(ServiceRegistry serviceRegistry)
) -所以使用它。
文档从发行版复制到发行版,可能还没有更新(他们不会在每个版本中重写手册) – 所以请相信Javadoc。
这个变化的细节可以在下面看到:
- 源代码: https : //github.com/hibernate/hibernate-core/commit/0b10334e403cf2b11ee60725cc5619eaafecc00b
- 门票: https : //hibernate.onjira.com/browse/HHH-5991
一些额外的参考:
要么
public class Hbutil { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; private static SessionFactory configureSessionFactory() throws HibernateException { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); return sessionFactory; } public static SessionFactory getSessionFactory() { return configureSessionFactory(); } }
就像这样简单:JBoss文档不是100%完好的维护。 去用JavaDoc所说的 : buildSessionFactory(ServiceRegistry serviceRegistry)
。
find不同版本的文档之间的差异并不罕见。 大多数开发人员将文档看作是一件杂事,而且往往会把它忽略掉。
作为一个经验法则,如果javadoc说了一件事,并且一些非javadoc文档与它相矛盾,则javadoc可能更准确。 程序员更有可能通过修改代码来保持javadoc的最新状态…因为javadoc的“源代码”与代码在同一个文件中。
在@deprecated
标签的情况下,这是一个虚拟的确定性,javadoc更准确。 开发人员在仔细考虑之后会弃用这些东西……而且(一般来说)他们并不是不赞成这些东西。
validation代码在Hibernate 4.3.0中工作。 注意你可以删除XML文件名参数,否则在那里提供你自己的path。 这是类似(但错别字纠正)在这里的其他职位,但这是正确的。
import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; Configuration configuration = new Configuration(); configuration.configure("/com/rtw/test/hiber/hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry);
在最新的hibernate 4.3.0以上版本中创buildSessionFactory对象的更好方法如下:
Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(). applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(builder.build());
如果你使用的是Hibernate 5.2或更高版本,那么你可以使用这个:
private static StandardServiceRegistry registry; private static SessionFactory sessionFactory; public static SessionFactory getSessionFactory() { if (sessionFactory == null) { try { // Creating a registry registry = new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); // Create the MetadataSources MetadataSources sources = new MetadataSources(registry); // Create the Metadata Metadata metadata = sources.getMetadataBuilder().build(); // Create SessionFactory sessionFactory = metadata.getSessionFactoryBuilder().build(); } catch (Exception e) { e.printStackTrace(); if (registry != null) { StandardServiceRegistryBuilder.destroy(registry); } } } return sessionFactory; } //To shut down public static void shutdown() { if (registry != null) { StandardServiceRegistryBuilder.destroy(registry); } }
public class HibernateSessionFactory { private static final SessionFactory sessionFactory = buildSessionFactory1(); private static SessionFactory buildSessionFactory1() { Configuration configuration = new Configuration().configure(); // configuration // settings // from // hibernate.cfg.xml StandardServiceRegistryBuilder serviceRegistryBuilder = new StandardServiceRegistryBuilder(); serviceRegistryBuilder.applySettings(configuration.getProperties()); ServiceRegistry serviceRegistry = serviceRegistryBuilder.build(); return configuration.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); }
公共无效sampleConnection()抛出exception{
Configuration cfg = new Configuration().addResource("hibernate.cfg.xml").configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); logger.debug(" connection with the database created successfuly."); }
我编辑了上面batbaatar创build的方法,所以它接受Configuration对象作为参数:
public static SessionFactory createSessionFactory(Configuration configuration) { serviceRegistry = new StandardServiceRegistryBuilder().applySettings( configuration.getProperties()).build(); factory = configuration.buildSessionFactory(serviceRegistry); return factory; }
在我做的主要课程中:
private static SessionFactory factory; private static Configuration configuration ... configuration = new Configuration(); configuration.configure().addAnnotatedClass(Employee.class); // Other configurations, then factory = createSessionFactory(configuration);
在Hibernate 4.2.2中
import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class Test { public static void main(String[] args) throws Exception { Configuration configuration = new Configuration() .configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings( configuration.getProperties()).buildServiceRegistry(); SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Users users = new Users(); ... ... session.save(users); transaction.commit(); session.close(); sessionFactory.close(); } }
Tested on 4.2.7 release package com.national.software.hibernate; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import com.national.software.dto.UserDetails; public class HibernateTest { static SessionFactory sessionFactory; public static void main(String[] args) { // TODO Auto-generated method stub UserDetails user = new UserDetails(); user.setUserId(1); user.setUserName("user1"); Configuration config = new Configuration(); config.configure(); ServiceRegistry serviceRegistry = (ServiceRegistry) new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); sessionFactory = config.buildSessionFactory(serviceRegistry); Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(user); session.getTransaction().commit(); } }
这里有许多在hibernate核心框架中弃用的API。
我们已经创build了如下的会话工厂:
SessionFactory sessionFactory = new Configuration()。configure()。buildSessionFactory();
buildSessionFactory方法从hibernate 4版本被弃用,并且被新的API取代。 如果你使用的是4.3.0及以上版本,你的代码必须是:
-
configurationconfiguration= new Configuration()。configure();
-
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder()。applySettings(configuration.getProperties());
-
SessionFactory factory = configuration.buildSessionFactory(builder.build());
ServiceRegistryBuilder类由4.3.0的StandardServiceRegistryBuilderreplace。 看起来5.0版本将会有很多变化。 仍然没有太多的清晰的弃用的API和使用合适的替代品。 每个增量版本都有更多不赞成使用的API,它们正在微调5.0版本的核心框架。
只要导入下面的包,
import org.hibernate.cfg.Configuration;