I cannot get hibernate.cfg.xml to be parsed and create a session using Hibernate 4.1.6.
Here is how I am instantiating the session:
Code:
private static Session openHibernateSession()
{
SessionFactory hbrntFctry;
Session rslt = null;
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().configure().buildServiceRegistry();
try
{
hbrntFctry = new Configuration().configure().buildSessionFactory(serviceRegistry);
rslt = hbrntFctry.openSession();
}
catch (Throwable ex)
{
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
}
return rslt;
}
Here is the file I am using:
Code:
<?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 xmlns="http://www.hibernate.org/xsd/hibernate-configuration"
xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-configuration-4.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/foampile</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></property>
<!-- List of XML mapping files -->
<mapping resource="SiteRecord.hbm.xml"/>
</session-factory>
</hibernate-configuration>
I modeled it after the example on the Hibernate site (http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch01.html) but their example didn't specify DOCTYPE. Some Google results I got suggested I add that so I added what was above.
What happens when I do that is this:
Code:
Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 63; Attribute "xmlns" must be declared for element type "hibernate-configuration".
and the exception is thrown on line
Code:
hbrntFctry = new Configuration().configure().buildSessionFactory(serviceRegistry);
The weird thing is that hibernate-configuration DOES in fact declare attribute "xmlns". Go figure...
When I know DOCTYPE out altogether, here is what I get:
Code:
Caused by: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 25; Document is invalid: no grammar found.
This time, the exception is thrown at the ServiceRegistry line (one line above).
Please help. I am exasperated as I have spun wheels on this last 3 hrs trying this and that, to no avail. If someone could actually post a copy of a functional cfg xml file, that would be great. As well as perhaps how the session is being created.
Thanks