Missing join when using sub-type in a predicate and InheritanceType.JOINED
See original GitHub issueSummary:
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:
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?
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
InheritanceType.JOINED, @PrimaryKeyJoinColumn and ...
This annotation specifies a primary key column that is used as a foreign key to join to another table. It is used to...
Read more >Limit Joins on Inheritance when querying Collections
I have been playing with collections for quite a while now and found 1 ... My current workaround is to use EMF and...
Read more >Hibernate ORM 6.1.6.Final User Guide - Red Hat on GitHub
Explicit association joins with join conditions; 16.8.4. join fetch for ... Basic collections (only subtypes of Collection ), which are not annotated with...
Read more >Java Back-End Web App Tutorial Part 6: Inheritance in Class ...
Case Study 2: Implementing a Class Hierarchy with Joined Table ... The object type Book with two subtypes: TextBook and Biography; 1.2.
Read more >Joined Subclass Inheritance Strategy - JPA - LogicBig
This strategy has the disadvantage of using one or more join queries to instantiate instances of a subclass. In deep class hierarchies, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I went ahead and tried this out with straight up querydsl:
Here is the rendered JPQL:
This causes postgres to error
missing FROM-clause entry for table "person1_1_"
Adding in an innerJoin fixes it:
query.innerJoin(qPersonContainer.person, qEmployee._super);
Also, Joined inheritance has been plagued by regressions in Hibernate in both 5.3 and 5.4 until recent patch versions of these releases, so keep that in mind when encountering this issue.