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
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();
........
As answered in hibernate 4.0.0. CR4: org.hibernate.internal.util.config.ConfigurationException with hibernate.cfg.xml, the new way of creating SessionFactories doesn't work yet. It will be ready in Hibernate 4.1.
Here is the deprecated method from Configuration that still works. It is doing a lot of hibernate specific setup that hibernate users wouldn't really want to do. Things like veryifying properties and copying them from one object to another. I have also been looking for a working example of Hibernate configuration for Hibernate 4 that does not use the deprecated buildSessionFactory() method and have been unable to find one so far. I believe that the intent is to deprecate Configuration entirely.
public SessionFactory buildSessionFactory() throws HibernateException {
Environment.verifyProperties( properties );
ConfigurationHelper.resolvePlaceHolders( properties );
final ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings( properties )
.buildServiceRegistry();
setSessionFactoryObserver(
new SessionFactoryObserver() {
@Override
public void sessionFactoryCreated(SessionFactory factory) {
}
@Override
public void sessionFactoryClosed(SessionFactory factory) {
( (StandardServiceRegistryImpl) serviceRegistry ).destroy();
}
}
);
return buildSessionFactory( serviceRegistry );
}