Search code examples
javahibernateormhql

HQL:Implicit Association joining not working


I am using Hibernate 3.2.5 for my application.

I have a Dept table and an Employees table.

Dept.java

private int deptId;
private String deptName;
private Map empMap = new HashMap();
//Getters and Setters

Employees.java

private int empId;
private String empName;
private int deptId;
private int age;
private String sex;
private Dept dept;
//Getters and Setters

Association between these two:

<map name="empMap" inverse="false" cascade="all">
      <key column="DEPT_ID"></key>
      <map-key formula="EMP_ID" type="integer"></map-key>
      <one-to-many class="com.jdbc.Employees"/>
  </map>

When I try the below statement:

Query hqlQuery = session.createQuery("from Dept dept where dept.empMap.empName = 'XYZ'");

I am getting the below exception:

org.hibernate.QueryException: illegal attempt to dereference collection [dept0_.DEPT_ID.empMap] with element property reference [empName] [from com.jdbc.Dept dept where dept.empMap.empName = 'XYZ']

Kindly let me know how to use the implicit join here. Reading the doc, I am not able to figure out what I am missing.


Solution

  • You are trying to access a collection like a property. You can do this instead:

    from Dept dept inner join dept.empMap emp where emp.empName = 'XYZ'