Uploaded image for project: 'Hibernate ORM'
  1. HHH-11182

HQL subquery with constraint property in superclass does not join superclass table

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 4.1.4, 4.2.21, 5.0.11, 5.2.3
    • Fix Version/s: 5.2.4, 5.1.3, 5.0.12
    • Component/s: query-hql
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

      Given the following entities:

      	@Entity(name="Person")
      	@Inheritance(strategy = InheritanceType.JOINED)
      	public static abstract class Person {
      		@Id
      		@GeneratedValue
      		@Column
      		private Long id;
      
      		@Basic(optional = false)
      		@Column(nullable = false)
      		private String firstName;
      	}
      	@Entity(name="Employee")
      	public static class Employee extends Person {
      		@Id
      		@GeneratedValue
      		@Column
      		private Long id;
      
      		private int employeeNumber;
      
      		@ManyToOne(optional = false)
      		@JoinColumn(nullable = false)
      		private InvestmentCompany company;
      	}
      

      For the query:

      String queryHQL = "from InvestmentCompany investmentCompany "
                                             + "where exists "
                                              + "(select employee "
                                              + "from investmentCompany.employees as employee "
                                              + "  where lower(employee.firstName) like :param1 )";
      

      NOTE: employee.firstName is in Person table (not Employee)

      The following SQL is generated:

          SELECT
              investment0_.id AS id1_0_,
              investment0_1_.fullName AS fullName2_0_,
              investment0_1_.shortName AS shortNam3_0_,
              investment0_.investorType AS investor1_2_
          FROM
              InvestmentCompany investment0_
          INNER JOIN
              CompanyBase investment0_1_
                  ON investment0_.id=investment0_1_.id
          WHERE
              EXISTS (
                  SELECT
                      employees1_.id
                  FROM
                      Employee employees1_
                  WHERE
                      investment0_.id=employees1_.company_id
                      AND (
                          LOWER(employees1_1_.firstName) LIKE ?
                      )
              )
      

      The alias, employees1_1_ does not correspond to any table involved in the query. Person table should have been joined with Employee because firstName is declared in Person.

        Attachments

          Issue links

            Activity

              People

              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: