Tag Archives: hibernate

Strange Hibernate behaviour

At work I decided to implement some DAO files in Hibernate. I started by making the smallest DAO first, and began with the work.
After making two HibernateDAO classes and beginning the third (file2DAO), which does nearly the same as second one (file1DAO). I simply copied all the data from the file1.hbm.xml file to the final file2.hbm.xml and edited the file to adjust it to the file2’s class.

File1.hbm.xml:

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

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

<hibernate-mappingdefault-lazy="false">
      <!—- omitted -->
      </class>

      <query name="xxx"><![CDATA[
            omitted
      ]]></query>
</hibernate-mapping>

File2.hbm.xml:

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

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

<hibernate-mappingdefault-lazy="false">
      <!—- omitted -->
      <query name="xxx1"><![CDATA[
            omitted
      ]]></query>
</hibernate-mapping>

This however, resulted in a 20mins debugging session, because the hiberbate.cfg file could not read the mapping… I started to uncomment the changes in the file2.hbm.xml file and found out that when I uncommented the query at the bottom, the junit tests could run. After a little extra debugging I found out, that hibernate cannot have two hbm.xml files, which have the same NamedQuery name. The contents of the NamedQueries where different, only the name where identical.

So… I’ve now changed the file2.hbm.xml file’s namedQuery to xxx1 instead of xxx as in file1.hbm.xml

Why is this?

Why can’t I have two namedQueries, that share the name, but have different content? Aren’t they in their own namespace?