在Native SQL Hibernate中使用addJoin的列中的异常

时间:2018-05-20 13:01:25

标签: java sql hibernate

我使用Hibernate 5.2.8,我有桌子Departamentos和Empleados。的 Departamentos.java

private int deptNo;
private String dnombre;
private String loc;
private Set<Empleados> empleadoses = new HashSet<Empleados>(0);

Empleados.java

private int empNo;
private Departamentos departamentos;
......

Departamentos HBM

<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true">
<class catalog="hibernate" dynamic-insert="false" dynamic-update="false" mutable="true" name="jdbc.Departamentos" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="departamentos">
    <id name="deptNo" type="int">
        <column name="dept_no"/>
        <generator class="assigned"/>
    </id>
   .........
    <set embed-xml="true" fetch="select" inverse="true" lazy="true" mutable="true" name="empleadoses" optimistic-lock="true" sort="unsorted" table="empleados">
        <key on-delete="noaction">
            <column name="dept_no" not-null="true"/>
        </key>

        <one-to-many class="jdbc.Empleados" embed-xml="true" not-found="exception"/>
    </set>
</class>

Empleados HBM

<hibernate-mapping auto-import="true" default-access="property" default-cascade="none" default-lazy="true">
<class catalog="hibernate" dynamic-insert="false" dynamic-update="false" mutable="true" name="jdbc.Empleados" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="empleados">
    <id name="empNo" type="int">
        <column name="emp_no"/>
        <generator class="assigned"/>
    </id>
    <many-to-one class="jdbc.Departamentos" embed-xml="true" fetch="select" insert="true" name="departamentos" not-found="exception" optimistic-lock="true" unique="false" update="true">
        <column name="dept_no" not-null="true"/>
    </many-to-one>
    ......

这是查询:

Query q = session.createSQLQuery("SELECT e.emp_no,e.apellido,e.oficio, d.dnombre FROM empleados e inner join departamentos d on e.dept_no=d.dept_no "
            + "where d.dnombre LIKE '"+dep+"'")
             .addEntity("e",jdbc.Empleados.class)
             //.addEntity("d",jdbc.Departamentos.class)
             .addJoin("d","e.departamentos");

例外:

  

错误:列&#39; dept_no&#39;未找到。线程&#34; main&#34;中的例外情况org.hibernate.exception.SQLGrammarException:无法执行查询

如何解决此问题?

0 个答案:

没有答案