org.hibernate.MappingException:关联引用未映射的类:empresa.Empleados

Posted

技术标签:

【中文标题】org.hibernate.MappingException:关联引用未映射的类:empresa.Empleados【英文标题】:org.hibernate.MappingException: Association references unmapped class: empresa.Empleados 【发布时间】:2022-01-15 01:05:34 【问题描述】:

我在mysql中有两个表的hibernate配置,Pojo的配置文件如下:

<hibernate-mapping>
    <class name="empresa.Departamentos" table="departamentos" catalog="empresa2" optimistic-lock="version">
        <id name="deptno" type="int">
            <column name="deptno" />
            <generator class="assigned" />
        </id>
        <property name="dnombre" type="string">
            <column name="dnombre" length="15" />
        </property>
        <property name="loc" type="string">
            <column name="loc" length="15" />
        </property>
        <set name="empleadoses" table="empleados" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="deptno" not-null="true" />
            </key>
            <one-to-many class="empresa.Empleados" />
        </set>
    </class>
</hibernate-mapping>
<hibernate-mapping>
    <class name="empresa.Empleados" table="empleados" catalog="empresa2" optimistic-lock="version">
        <id name="empno" type="short">
            <column name="empno" />
            <generator class="assigned" />
        </id>
        <many-to-one name="departamentos" class="empresa.Departamentos" fetch="select">
            <column name="deptno" not-null="true" />
        </many-to-one>
        <property name="apellido" type="string">
            <column name="apellido" length="10" />
        </property>
        <property name="oficio" type="string">
            <column name="oficio" length="10" />
        </property>
        <property name="dir" type="java.lang.Short">
            <column name="dir" />
        </property>
        <property name="fechaalt" type="date">
            <column name="fechaalt" length="10" />
        </property>
        <property name="salario" type="java.lang.Float">
            <column name="salario" precision="6" />
        </property>
        <property name="comision" type="java.lang.Float">
            <column name="comision" precision="6" />
        </property>
    </class>
</hibernate-mapping>

接下来,当我在hql中执行以下语句时: select e from Empleados e where e.departamentos.deptno in (select d.deptno from Departamentos d where d.loc='London') 它给了我以下错误: 在 org.hibernate.cfg.HbmBinder.bindCollectionSecondPass(HbmBinder.java:2557) 在 org.hibernate.cfg.HbmBinder$CollectionSecondPass.secondPass(HbmBinder.java:2808) 在 org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:70) 在 org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695) 在 org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844) 在 org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928) 问候, 哈维尔

【问题讨论】:

【参考方案1】:

解决办法是:

select e.apellido 
from Empleados e 
where e.departamentos.deptno in 
  (select d.deptno 
   from Departamentos d 
   where d.loc='Sevilla')

【讨论】:

以上是关于org.hibernate.MappingException:关联引用未映射的类:empresa.Empleados的主要内容,如果未能解决你的问题,请参考以下文章