Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

functional hibernate.cfg.xml for hibernate 4

Tags:

hibernate

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

like image 877
amphibient Avatar asked Aug 30 '12 22:08

amphibient


People also ask

Is it necessary to use hibernate cfg XML for configuration in hibernate?

Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate. cfg. xml file which is exactly what the configure() method does.

How do I fix hibernate cfg XML not found?

Open Module setting of your project, select "Modules" from left and in the "sources" tab select the newly created "resources" folder and mark it as "Resources". Create the hibernate. cfg. xml file inside this newly created "resources" folder.

What is the use of Hibernate cfg XML?

These configurations contain the mapping information that provides different functionalities to Java classes. Generally, we provide database related mappings in the configuration file. Hibernate facilitates to provide the configurations either in an XML file (like hibernate. cfg.


2 Answers

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.

like image 147
user990522 Avatar answered Jan 03 '23 22:01

user990522


... 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">
like image 41
vector Avatar answered Jan 03 '23 23:01

vector