2

I am new to hibernate .. and I am facing some basic problems :

My xml is

<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<?xml version='1.0' encoding='utf-8'?>
<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>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/database</property>
<property name="connection.username">user</property>
<property name="connection.password">user123</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache  -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="hibernateDTO.StudentDetails"/>
</session-factory>
</hibernate-configuration>

I have written 2 classes one which is entity class and other with a main method to store / read operations. I am using hibernate 4 and once I run my class I get exception

Nov 13, 2014 10:58:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.7.Final}
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Nov 13, 2014 10:58:58 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Nov 13, 2014 10:58:58 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:2165)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2077)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2056)
at hibernateTest.Hirbernate.main(Hirbernate.java:20)
Caused by: org.dom4j.DocumentException: Error on line 5 of document  : The processing instruction target matching "[xX][mM][lL]" is not allowed. Nested exception: The processing instruction target matching "[xX][mM][lL]" is not allowed.
    at org.dom4j.io.SAXReader.read(SAXReader.java:482)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2157)
    ... 3 more

My jars are

hibernate-entitymanager-4.3.7.Final

antlr-2.7.7

dom4j-1.6.1

hibernate-commons-annotations-4.0.5.Final

hibernate-core-4.3.7.Final

hibernate-jpa-2.1-api-1.0.0.Final

jandex-1.1.0.Final

javassist-3.18.1-GA

jboss-logging-3.1.3.GA

jboss-logging-annotations-1.2.0.Beta1

jboss-transaction-api_1.2_spec-1.0.0.Final

and mysql-connector-java-5.1.33-bin

am I missing out in jars or have wrong jars?

3 Answers 3

2

You need to put your XML processing instruction above your DOCTYPE declaration, so to get rid of the XML parsing error, the start of your file should look 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">

Second problem: you're referencing both a DTD file (for Hibernate version 3) and an XML schema (for version 4). You should probably get rid of the DOCTYPE declaration altogether.

Sign up to request clarification or add additional context in comments.

1 Comment

You still get an error about an invalid XML processing instruction error on the same line? Sounds like your file wasn't updated.
1

First, the first line in all XML file, must be something similar to:

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

So, you can try the next:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/database</property>
    <property name="connection.username">user</property>
    <property name="connection.password">user123</property>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>
    <!-- Drop and re-create the database schema on startup -->
    <property name="hbm2ddl.auto">create</property>
    <mapping class="hibernateDTO.StudentDetails"/>
  </session-factory>
</hibernate-configuration>

See also XML Validator online and Hibernate Configuration in www.tutorialspoint.com

1 Comment

Hey, @AndyHayden! -- What? ... Aaaaaah! Ha, ha. I think I was thinking in freeformatter.com/xml-validator-xsd.html -- Thanks!
0

do it like

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

the problem you have done is you have again defined namespaced in hibernate-configuration tag.

    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<?xml version='1.0' encoding='utf-8'?>
<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>

1 Comment

yes you are right i have typed wrong. thanks for informing. I am editing it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.