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
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
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
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?
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">
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..
// 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