Missing join when using sub-type in a predicate and InheritanceType.JOINED

41 views
Skip to first unread message

Casey

unread,
Nov 23, 2016, 1:23:55 PM11/23/16
to Querydsl
Summary:

Given a base `Person` class, and an `Employee subtype` specified with InheritanceType.JOINED, along with a `PersonContainer` that has a relationship to a person, invalid sql is generated given the predicate:
```java
QPersonContainer.personContainer.person.as(QEmployee.class).name.likeIgnoreCase(name)
```

Details:


// Base type
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
class Person {
  @Id
  Long id;
}

// Sub type
@Entity
@DiscriminatorValue("employee")
class Employee extends Person {
 
  @Column
  String name;
}

// The object I'm querying by
@Entity
class PersonContainer {
  @OneToOne
  private Person person;
}


I create a predicate with:


BooleanBuilder bb = new BooleanBuilder();
bb.and(QPersonContainer.personContainer.person.as(QEmployee.class).name.likeIgnoreCase(name))
personContainerRepository.findAll(bb);


The generated SQL looks like this:

SELECT count(personcont0_.id) AS col_0_0_
FROM db.person_container personcont0_
  CROSS JOIN db.person person1_
WHERE personcont0_.person_id = person1_.id
      AND (lower(person1_1_.name) LIKE '%mary%')


This fails with the root cause:

org.postgresql.util.PSQLException: ERROR: missing FROM-clause entry for table \"person1_1_\"\


You can see in the generated sql the error: person1_1_ is referenced but is not defined in a join anywhere. The query would work fine if this line was added after the cross join:


INNER JOIN db.employee as person1_1_ on person1_1_.id = person1_.id


Since I'm using a spring-data repository I don't have access to the JPQLQuery and can't specify the join myself. But anyways, it should be automatically created no?

Am I generated the predicate incorrectly? Or is this a bug?

(as I suspect this is a bug, I also cross-posted to https://github.com/querydsl/querydsl/issues/2055)

Cheers,
Casey

Reply all
Reply to author
Forward
0 new messages