2

Hey guys I am beginner for the hibernate and I know there are so many similar questions here. I tried to solve from them but I could not. I also tried to change SYSTEM from PUBLIC in dtd but its not working. I googled for it but everywhere it showing for the dtd statement error.

This is my configuration file.

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
   <hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver/property> 
<property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
<property name="username">root</property> 
<property name="password"></property>
<property name="connection.pool_size">1</property>  
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hbm2ddl.auto">update</property>
<mapping class="org.hibernate.src.userDetails" /> 
</session-factory>
</hibernate-configuration>

I tried to change the version 3.0 to 4.0 as I am using hibernate version 4.3.6 but still its not working. please help me out.

This is my userDetails class..

package org.hibernate.src;

@Entity
public class userDetails {
@Id
private int userId ;
private String userName;
@Embedded
private Address address;


public Address getAddress() {
    return address;
}

public void setAddress(Address address) {
    this.address = address;
}

public int getUserId() {
    return userId;
}

public String getUserName() {
    return userName;
}
public void setUserName(String userName) {
    this.userName = userName;
}
public void setUserId(int userId) {
    this.userId = userId;

}


}

These jar files I have added to my project:

lib\jpa\hibernate-entitymanager-4.3.6.Final.jar
lib\required\antlr-2.7.7.jar
lib\required\dom4j-1.6.1.jar
lib\required\hibernate-commons-annotations-4.0.5.Final.jar
lib\required\hibernate-core-4.3.6.Final.jar
lib\required\hibernate-jpa-2.1-api-1.0.0.Final.jar
lib\required\jandex-1.1.0.Final.jar
lib\required\javassist-3.18.1-GA.jar
lib\required\jboss-logging-3.1.3.GA.jar
lib\required\jboss-logging-annotations-1.2.0.Beta1.jar
lib\required\jboss-transaction-api_1.2_spec-1.0.0.Final.jar

This is what I am getting while running the application

Oct 13, 2014 4:24:47 PM      
 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
 INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
 Oct 13, 2014 4:24:47 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.6.Final}
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Environment buildBytecodeProvider  
INFO: HHH000021: Bytecode provider name : javassist
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Oct 13, 2014 4:24:47 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
 Exception in thread "main" org.hibernate.HibernateException: Could not parse     
configuration: /hibernate.cfg.xml
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2163)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2075)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2054)
at org.hibernate.test.userTest.main(userTest.java:18)
Caused by: org.dom4j.DocumentException: Error on line 4 of document  : Content is not    
allowed in prolog. Nested exception: Content is not allowed in prolog.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2155)
... 3 more
user3678528
  • 1,741
  • 2
  • 18
  • 24
Dipesh Raichana
  • 1,008
  • 3
  • 18
  • 36

9 Answers9

4

I updated my dtd as u mentioned. thank you so much.

I changed my tag. then it was showing an error for white space bfore the tag.

i solved it then it was showing me an error for accessible database hibernate. i tried a lot even i reinstall my wamp server, though it didnt work.

finally i create a new database and changed my hibernate.cfg.xml file.

this is my file which is working cool...

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="connection.url">jdbc:mysql://localhost/test</property> 
<property name="username">root</property> 
<property name="password" /> 
<property name="connection.pool_size">1</property>  
<property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
<property name="hbm2ddl.auto">update</property>
<mapping class="org.hibernet.src.userDetails" ></mapping> 
</session-factory>
</hibernate-configuration>
Dipesh Raichana
  • 1,008
  • 3
  • 18
  • 36
3

Require internet connection to run your example:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-configuration PUBLIC  
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/db</property>
        <property name="connection_pool_size">1</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>

        <mapping resource="employee.hbm.xml" />
    </session-factory>

</hibernate-configuration>

Don't require internet connection to run your example:

<?xml version='1.0' encoding='UTF-8'?>  

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

<hibernate-configuration>

    <session-factory>

        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/springdb</property>
        <property name="connection_pool_size">1</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="show_sql">true</property>

        <mapping resource="employee.hbm.xml" />
    </session-factory>

</hibernate-configuration>

Only difference between these two config file is having different DTD.

Nilesh Jadav
  • 890
  • 9
  • 8
1

The problem is in following line.

"-//Hibernate/Hibernate Configuration DTD .0//EN"

It should be as following:

"-//Hibernate/Hibernate Configuration DTD 4.0//EN"

Note: You are missing 4 in 4.0, you just have .0

If it does not work change your DTD to following:

<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • its showing following error... Exception in thread "main" org.hibernate.MappingException: invalid configuration at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2158) at org.hibernate.cfg.Configuration.configure(Configuration.java:2075) at org.hibernate.cfg.Configuration.configure(Configuration.java:2054) at org.firstHibernatepro.main.hibernateTest.main(hibernateTest.java:29) – Dipesh Raichana Oct 10 '14 at 05:32
  • The exception is regarding the mapping of the class `userDetails`, make sure you have class mappings properly done. Also this exception is probably getting raised while you are trying to save or get the data from database. – Darshan Lila Oct 10 '14 at 05:37
1

The DTD declaration should be like this:

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

you have to use 3.0 for DTD declaration but not 4.0 because as per this link from hibernate there is no separate DTD with 4.0 version.

Index of /dtd

Name                                    Last modified               Size

hibernate-mapping.dtd                   Fri Apr 4 13:56:57 2014     15K
hibernate-configuration-2.0.dtd         Fri Apr 4 13:56:57 2014     1K
hibernate-configuration-3.0.dtd         Fri Apr 4 13:56:57 2014     2K
hibernate-mapping-3.0.dtd               Fri Apr 4 13:56:57 2014     44K
hibernate-mapping-1.1.dtd               Fri Apr 4 13:56:57 2014     16K
hibernate-configuration.dtd             Fri Apr 4 13:56:57 2014     830
hibernate-mapping-2.0.dtd               Fri Apr 4 13:56:57 2014     25K
hibernate-reverse-engineering-3.0.dtd   Fri Apr 4 13:56:57 2014     5K
hibernate-generic.dtd                   Fri Apr 4 13:56:57 2014     3K

Also you can refer to this SO post for similar information - Hibernate error, possibly with DTD declaration

Update:

The DTD declaration is not correct in your file, you have it as:

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

but it should be like this:

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

In your file you are saying hibernate mapping but it should be hibernate configuration

Community
  • 1
  • 1
Chaitanya
  • 15,403
  • 35
  • 96
  • 137
  • thanx buddy... but i have tried this thing but still i am facing the problem ... i just want to know that which jar files are necessary to be added for hibernate application? – Dipesh Raichana Oct 10 '14 at 08:25
  • @Geek, can you update your question and add the complete stacktrace of the exception. Also add the name of hibernate Jars to question that you have included in classpath. – Chaitanya Oct 10 '14 at 09:08
  • Hey bro I have added all the details..p – Dipesh Raichana Oct 11 '14 at 09:53
  • Hey bro ...i have added all the details ..please let me know where i am making the mistake?? it will screwed me up.... – Dipesh Raichana Oct 11 '14 at 09:55
  • @Geek, I don't see any changes you made to your question after my comment, so what have you added? Please add these --> `can you update your question and add the complete stacktrace of the exception. Also add the name of hibernate Jars to question that you have included in classpath` also add your latest hibernate.cfg.xml file which you are using now. With out knowing these details it is difficult to guess where the issue is. – Chaitanya Oct 11 '14 at 12:08
  • hey bro please scroll down the cursor... i updated my hibernate.cfg.xml file and added the list of jar files with the exception list..just after the line "This is my userDetails class.." scroll down the cursor... – Dipesh Raichana Oct 12 '14 at 04:57
  • @Geek, the error says `The document type declaration for root element type "hibernate-configuration" must end with '>'.` so it seems your updated file is missing the closing bracket - '>', so please check it once. If you see that the file is correct then update your question with your latest hibernate.cfg.xml file content. – Chaitanya Oct 12 '14 at 10:27
  • hey i saw it but there was not any unclosed '<>' brackets. and i have updated my file also...please tell me where i have been stuck? – Dipesh Raichana Oct 13 '14 at 10:59
  • @Geek, the DTD declaration is not correct in your file, please use the one mentioned in my answer. I am adding details in the update section of my answer, please check. – Chaitanya Oct 13 '14 at 12:04
0

Try using this.

<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

OR

<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-4.0.dtd">

Reference

Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
0
<!DOCTYPE root-node PUBLIC/SYSTEM "path to your DTD" >

Try this one : Extract 'hibernate-configuration-3.0.dtd' from your Hibernate jar and put it in some directory (in this case, I have added to Project root directory).

<!DOCTYPE hibernate-configuration SYSTEM
"hibernate-configuration-3.0.dtd">

Instead of fetching DTD from the web, it will fetch the dtd from the system.

Sample Configuration file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.xa.client.OracleXADataSource
</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:9999:srvc</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="\hibernate\products.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
0

Hi it seems your previous error was pointing you to line 4 of your configuration file:

<property name="connection.driver_class">com.mysql.jdbc.Driver/property>

it should have been

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>

< in the closing tag was missing

Andrew Quartey
  • 101
  • 2
  • 7
0

It may be "http://www.hibernate.org" is blocked on your machine or your system is not connected to internet so the solution is to provide the DTD file location in the system using classpath. DTD file is present in the hibernate jars and it should load it from there. So the DocType that worked offline would be:

<!DOCTYPE hibernate-configuration SYSTEM 
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
alok
  • 2,718
  • 21
  • 17
0

In my case I had the same error when I had empty password for database connection with closing tag like below:

<property name="hibernate.connection.password"/></property>

Once I removed the closing brackets of property this one "</property>" like in below line, its working:

<property name="hibernate.connection.password"/>

So if you also have empty password for database connection try to remove "</property>" brackets. May this will solve your problem too.

A.Aleem11
  • 1,844
  • 17
  • 12
  • The issue was not your "" tag. But if you see you have already closed your tag by adding "/" in tag(self closing tag). So you can use either ways here.. "" or "". I hope it is clear – Dipesh Raichana Oct 23 '17 at 09:41