Create a factory session in Hibernate 4

I had a problem creating a factory session in Hibernate 4. In Hibernate 3, I just did:

org.hibernate.cfg.Configuration conf= HibernateUtil .getLimsInitializedConfiguration(systemConfiguration .getHibernateconfFile()); SessionFactory sf = conf.configure().buildSessionFactory(); 

Now I need to pass the ServiceRegistry class to buildSessionFactory, but the Javadocs are extremely vague on how to do this. Any tips?

+61
java hibernate
Nov 02 '11 at 20:13
source share
8 answers

Yes, they have deprecated the previous buildSessionFactory API, and it's pretty easy to do well .. you can do something like this.

EDIT : ServiceRegistryBuilder is deprecated. you should use StandardServiceRegistryBuilder

 public void testConnection() throws Exception { logger.info("Trying to create a test connection with the database."); Configuration configuration = new Configuration(); configuration.configure("hibernate_sp.cfg.xml"); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build()); Session session = sessionFactory.openSession(); logger.info("Test connection with the database created successfuly."); } 

For more detailed and detailed information, you can check the official test case of sleep mode at https://github.com/hibernate/hibernate-orm/blob/master/hibernate-testing/src/main/java/org/hibernate/testing/junit4 /BaseCoreFunctionalTestCase.java function (buildSessionFactory ()).

+68
May 28 '12 at 12:32
source share

Try it!

 package your.package; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtil { private static SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; static { try { // Configuration configuration = new Configuration(); Configuration configuration = new Configuration().configure(); serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (HibernateException he) { System.err.println("Error creating Session: " + he); throw new ExceptionInInitializerError(he); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 
+16
Sep 24 '12 at 14:56
source share
 Configuration hibConfiguration = new Configuration() .addResource("wp4core/hibernate/config/table.hbm.xml") .configure(); serviceRegistry = new ServiceRegistryBuilder() .applySettings(hibConfiguration.getProperties()) .buildServiceRegistry(); sessionFactory = hibConfiguration.buildSessionFactory(serviceRegistry); session = sessionFactory.withOptions().openSession(); 
+11
May 23 '12 at 19:10
source share

The following expresses the experience I had with hibernate 4.0.0.Final.

The javadoc (distributed under the LGPL license) of the org.hibernate.cfg.Configuration class states that:

NOTE. This will be replaced using ServiceRegistryBuilder and org.hibernate.metamodel.MetadataSources instead of version 4.0, after which this class will become obsolete and scheduled to be removed in 5.0. See HHH-6183 , HHH-2578, and HHH-6586 for details.

After considering question 2578, I used something like this:

 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().configure().buildServiceRegistry(); MetadataSources metadataSources = new MetadataSources(serviceRegistry); metadataSources.addResource("some_mapping.hbm.xml") SessionFactory sessionFactory = metadataSources.buildMetadata().buildSessionFactory(); 

To start reading the configuration, I had to modify the hibernate 3.2.6 configuration and mapping files to use xmlns="http://www.hibernate.org/xsd/hibernate-configuration" and xmlns="http://www.hibernate.org/xsd/hibernate-mapping" , as well as remove the dtd specifications.

I could not find a way to check the mappings defined in hibernate.cfg.xml and sleep mode. the prefix for hibernation-related properties in hibernate.cfg.xml is no longer required.

This might work for some.

For example, I encountered some error because the mapping files contained <cache usage="read-write" /> and ended up using an outdated configuration method:

 Configuration configuration = new Configuration().configure(); SessionFactoryImpl sessionFactory = (SessionFactoryImpl) configuration.buildSessionFactory(); EventListenerRegistry listenerRegistry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class); SolrIndexEventListener indexListener = new SolrIndexEventListener(); // a SaveOrUpdateEventListener i wanted to attach listenerRegistry.appendListeners(EventType.SAVE_UPDATE, indexListener); 

I had to add event listeners programmatically because Configuration is no longer looking for them in hibernate.cfg.xml

+6
Dec 15 '11 at 16:59
source share

Even in version 4.3.0 there is an update. ServiceRegistryBuilder is also deprecated in 4.3.0 and replaced with the standard ServiceRegistryBuilder. Now, the actual code for creating a factory session will look like this when creating a factory session .

+5
Dec 25 '13 at 6:10
source share

[quote from http://www.javabeat.net/session- factory -hibernate-4 /]

There are many APIs deprecated in the hibernate kernel structure. Currently, one of the frustrating things is that the hibernate official documentation does not give clear instructions on how to use the new API, and indicates that the documentation is complete. Also, each incremental version receives changes in some important APIs. One of these things is the new API for creating a factory session in Hibernate 4.3.0 on computers. In our previous versions, we created a factory session as shown below:

 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

The buildSessionFactory method has been deprecated from hibernate 4 and is being replaced by a new API. If you are using hibernation 4.3.0 or higher, your code should be:

 Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(builder.build()); 

The ServiceRegistryBuilder class has been replaced by the standard ServiceRegistryBuilder from 4.3.0. It looks like there will be a lot of changes in version 5.0. However, there is no clarity regarding legacy APIs and suitable alternatives for use. Each incremental release contains a more obsolete API, they are able to fine-tune the basic structure for release 5.0.

+2
Feb 03 '14 at 5:15
source share

The buildSessionFactory method has been deprecated from Hibernate 4 and replaced with a new API. If you use Hibernate 4.3.0 and higher, your code should be:

 Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(builder.build()); 
+1
Jan 22 '16 at 14:44
source share

In earlier versions, the factory session was created as follows:

 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

The buildSessionFactory method has been deprecated from hibernate 4 and replaced with a new API. If you are using hibernate mode 4.3.0 or higher, your code should look like this:

 Configuration configuration = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder(). applySettings(configuration.getProperties()); SessionFactory factory = configuration.buildSessionFactory(builder.build()); 
0
Jul 27 '14 at 17:01
source share



All Articles