Question

I'm learning hibernate in myeclipse. I tried to reverse engineer a mysql table and follow the official myeclipse tutorial. All the xml files are generated but writing a simple test programme it gives me the following error:

.

     17-dic-2012 19.18.57 org.hibernate.annotations.common.Version <clinit>
     INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
     17-dic-2012 19.18.57 org.hibernate.Version logVersion
     INFO: HHH000412: Hibernate Core {4.1.4.Final}
     17-dic-2012 19.18.57 org.hibernate.cfg.Environment <clinit>
     INFO: HHH000206: hibernate.properties not found
     17-dic-2012 19.18.57 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.57 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
    at org.hibernate.cfg.Configuration.add(Configuration.java:478)
    at org.hibernate.cfg.Configuration.add(Configuration.java:474)
    at org.hibernate.cfg.Configuration.add(Configuration.java:647)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
    at sor.hibernate.HibernateSessionFactory.<clinit>(HibernateSessionFactory.java:32)
    at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    ... 13 more
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
17-dic-2012 19.18.59 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: sor/hibernate/Hitest.hbm.xml
%%%% Error Creating SessionFactory %%%%
org.hibernate.InvalidMappingException: Unable to read XML
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:109)
    at org.hibernate.cfg.Configuration.add(Configuration.java:478)
    at org.hibernate.cfg.Configuration.add(Configuration.java:474)
    at org.hibernate.cfg.Configuration.add(Configuration.java:647)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:730)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2109)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2081)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2061)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2014)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1929)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1908)
    at sor.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:71)
    at sor.hibernate.HibernateSessionFactory.getSession(HibernateSessionFactory.java:55)
    at sor.hibernate.BaseHibernateDAO.getSession(BaseHibernateDAO.java:13)
    at sor.hibernate.HitestTest.main(HitestTest.java:15)
Caused by: org.dom4j.DocumentException: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20 Nested exception: http://www.jboss.org/dtd/hibernate/hibernate-mapping-4.0.dtd%20
    at org.dom4j.io.SAXReader.read(SAXReader.java:484)
    at org.hibernate.internal.util.xml.MappingReader.readMappingDocument(MappingReader.java:78)
    ... 14 more
Exception in thread "main" java.lang.NullPointerException
    at sor.hibernate.HitestTest.main(HitestTest.java:15)

The test programme is

package sor.hibernate;


import java.util.Date;

import org.hibernate.Session;
import org.hibernate.Transaction;


public class HitestTest {

    public static void main(String[] args) {

        HitestDAO dao = new HitestDAO();
        Transaction tx = dao.getSession().beginTransaction();

        Hitest hitest[] = new Hitest[10];
        for (int i = 0; i < hitest.length; i++) {
            hitest[i]  = new Hitest();
            hitest[i].setGreeting("saluto numero " + i*23);
            hitest[i].setRef(i+10);
            hitest[i].setDate(new Date(System.currentTimeMillis()));
            dao.save(hitest[i]);
        }
        tx.commit();
        dao.getSession().close();
    }
}

and the hibernate.cfg.xml is

<?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">
<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

    <session-factory>
        <property name="hbm2ddl.auto">update</property>
        <property name="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/efeulib
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">qwerty</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">
            mysqlJDriver
        </property>
        <mapping resource="sor/hibernate/Hitest.hbm.xml" />

    </session-factory>

</hibernate-configuration>

while Hitest.hbm.xml is

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd ">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping package="sor.hibernate">
    <class name="sor.hibernate.Hitest" table="hitest" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="greeting" type="java.lang.String">
            <column name="greeting" length="200" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="date" length="10" />
        </property>
        <property name="ref" type="java.lang.Integer">
            <column name="ref" />
        </property>
    </class>
</hibernate-mapping>

I have read the relevant questions and I have deleted the all trailing lines and spaces. But none of them has worked. The only thing I've add to these files reading the questions is package="sor.hibernate" attribute in the Hitest.hbm.xml The paths and... are all correct and they are all autogenerated.

More information. When I type from hitest in the HQL, it tells me org.hibernate.hql.ast.QuerySyntaxException: hitest is not mapped [from hitest] while from sor.hibernate.Hitest works well

Was it helpful?

Solution

Try removing the white space in

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd "
                                                       ^-- here

And in every other place where you might have done the same mistake.

OTHER TIPS

Solving the problem indicated by @JB_Nizet I faced a new error about the validation. (Note: The problem of the space was a myeclipse bug. I tried with a project and it was myeclipse which added that evil space.) I added the following line to hibernate.cfg.xml and the problem was partly solved. The new error was (%%%% Error Creating SessionFactory %%%% org.hibernate.HibernateException: Error applying BeanValidation relational constraints)

<property name="javax.persistence.validation.mode">none</property>

The new error was hibernate sql query error. The id should have been assigned so I changed Hitest.hbm.xml to

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="sor.hibernate.Hitest" table="hitest" catalog="test">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="increment" />
        </id>
        <property name="greeting" type="java.lang.String">
            <column name="greeting" length="200" />
        </property>
        <property name="date" type="java.util.Date">
            <column name="date" length="10" />
        </property>
        <property name="ref" type="java.lang.Integer">
            <column name="ref" />
        </property>
    </class>
</hibernate-mapping>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top