Hibernate - ServiceRegistryBuilder

前端 未结 9 1839
眼角桃花
眼角桃花 2020-12-28 08:30

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

相关标签:
9条回答
  • 2020-12-28 08:50

    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

    0 讨论(0)
  • 2020-12-28 08:53

    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

    0 讨论(0)
  • 2020-12-28 08:54

    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?

    0 讨论(0)
  • 2020-12-28 08:57

    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">
    
    0 讨论(0)
  • 2020-12-28 09:00

    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..

    0 讨论(0)
  • 2020-12-28 09:09

    // 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

    0 讨论(0)
提交回复
热议问题