Our project use\'s Hibernate\'s programmatic Configuration
to set up our SessionFactory and such. I just migrated us from version 3 to version 4 of Hibernate. N
I also migrate from hibernate 3 to 4 ,
For hibernate.cfg.xml file i use following DTD
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
For mapping file i use follwing DTD:
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
If i change dtd it gives exception because hibernate 4 use xsd instead of dtd. Hibrenate Jira of migrating dtd to xsd
So you have to use xsd file instead of dtd.
Hibernate hbm example
I also just migrated from 3.0 to 4.0, I assume 3 causes I use the following DTD's
THE ACTUAL FIX IN THIS CASE
Make sure that you dont have any old 3.0 jar's in the path, else you can see this exception.
Possible Cause 1
For hibernate.cfg.xml
<?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">
And for the hbm files
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Works well for me.
Possible Cause 2
You have misspelt <hibernate-mapping>
in your hbm file.
Edit :
I am using mixed configuration both programmatic and cfg files. When I tried to use all programmatic, it did not work for me. Nor did I get much help from SO. But the below worked for me.
try {
String connection = "jdbc:mysql://"
+ Globals.DBSERVER.trim()
+ "/myDB?autoReconnect=true&failOverReadOnly=false&maxReconnects=10";
log.debug("Connection URL "+connection) ;
Configuration configuration = new Configuration();
configuration
.setProperty("hibernate.connection.url", connection)
.setProperty("hibernate.connection.username", Globals.DB_USER_NAME.trim())
.setProperty("hibernate.connection.password", Globals.DB_PASSWORD.trim())
;
configuration.configure();
sessionFactory = configuration
.buildSessionFactory(new ServiceRegistryBuilder()
.applySettings(configuration.getProperties()).buildServiceRegistry());
} catch (Exception e) {
log.fatal("Unable to create SessionFactory for Hibernate");
log.fatal(e.getMessage());
log.fatal(e);
e.printStackTrace();
}
My question that helped me fix it.
Overall Advice
Going all programmatic is a bad idea. Since there is a lot of programmatic stuff you need to add from column to variable mapping to variable type. It will be a debugging nightmare. I suggest doing non programmatic stuff for things that you can do without programmatic. For me I just needed to get the username password from cmd line, so that I can deploy the product on any server. So I just made that programmatic.