functional hibernate.cfg.xml for hibernate 4

10,818

Solution 1

I had the same problem and I was able to get it to work by just removing all the attributes from hibernate-configuration, despite what the documentation and error messages say :) So in the end, I have this for my DOCTYPE:

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

and then I just have:

<hibernate-configuration>
....
</hibernate-configuration>

and that works for me.

Solution 2

... for what it's worth, I ended up having IntelliJ created the stub file, just to get a start on it (for hibernate 4.1):

<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
Share:
10,818
amphibient
Author by

amphibient

Software Engineer with table manners

Updated on June 12, 2022

Comments

  • amphibient
    amphibient almost 2 years

    does anybody have a functional cfg file example for hibernate 4 ? All the reference i can find online is for less than v4 and that doesn't work. i tried pasting the contents of my file here but this site removes the hibernate-configuration tag.

    so here is what comes out:

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/">
    
    <hibernate-configuration xmlns="http://www.hibernate.org/xsd/hibernate-configuration">
    
      <session-factory> 
    
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
        <!-- Assume test is the database name --> 
        <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>
    

    once I change to

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

    i get this exception:

    Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 63; Attribute "xmlns" must be declared for element type "hibernate-configuration".
    

    BUT xmlns IS specified (xmlns="http://www.hibernate.org/xsd/hibernate-configuration")

    IS THIS A BUG IN HIBERNATE 4.1 ???