Hibernate - ServiceRegistryBuilder

时间秒杀一切 提交于 2019-11-30 01:55:16

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 of 4.3.0, even ServiceRegistryBuilder has been deprecated. This is how you would want to go about it

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

And also you would have to import org.hibernate.boot.registry.StandardServiceRegistryBuilder instead of org.hibernate.service.ServiceRegistryBuilder

Here is how it works with hibernate 4.x

<?xml version='1.0' encoding='utf-8'?>
<hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
    <session-factory name="java:hibernate/SessionFactory"><!-- Name is Optional -->
......
</session-factory>
</hibernate-configuration>

rest of configuration remains same

I encountered similar problems trying to use Hibernate 4.1.6.

Building on an example from RoseIndia.net, I got ServiceRegistryBuilder working like this:

Configuration config = new Configuration();
config.configure();
ServiceRegistryBuilder srBuilder = new ServiceRegistryBuilder();
srBuilder.applySettings(config.getProperties());
ServiceRegistry serviceRegistry = srBuilder.buildServiceRegistry();
SessionFactory factory = config.buildSessionFactory(serviceRegistry);

My hibernate.cfg.xml file uses the old DTD file; I have not been able to get the new XSD file to be recognized by Hibernate 4.1.6:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        ...
    </session-factory>
</hibernate-configuration>

Perhaps you can adapt this to work for you?

perky

for the below error :

Oct 09, 2015 12:29:53 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.2.Final}
Oct 09, 2015 12:29:53 AM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 09, 2015 12:29:53 AM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Exception in thread "main" org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml]
    at org.hibernate.boot.cfgxml.internal.ConfigLoader.loadConfigXmlResource(ConfigLoader.java:53)
    at org.hibernate.boot.registry.StandardServiceRegistryBuilder.configure(StandardServiceRegistryBuilder.java:163)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:259)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:245)
    at org.hibernate.ClientResource.ClientTest.main(ClientTest.java:17)

we should place the hibernate.cfg.xml in src folder..

It might be just nitpicking, but please try replacing the DOCTYPE declaration in hibernate.cfg.xml with this:

<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

Also, declare a name for the session-factory:

<session-factory name="java:hibernate/SessionFactory">

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 );
}
Thiago Chaves

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.

user2473488

// hibernate 4构建sessionFactory方式 email 563143188@qq.com

    Configuration cfg = new Configuration()
            .configure("hibernate.cfg.xml");

    SessionFactory sf = cfg.buildSessionFactory(new ServiceRegistryBuilder()
            .applySettings(cfg.getProperties()).build());
    //build()   return buildServiceRegistry
    Session s = sf.openSession();

// test is ok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!