I'm encountering an issue with Hibernate where I'm getting the following SQL error:
Please help me to resolve error
SQL Error: 0, SQLState: 42P01 ERROR: missing FROM-clause entry for table "th1_1" Position: 14
I'm working on a Spring Boot application where I'm using Hibernate for ORM mapping. I have entities defined for dx_entity and dx_temporary_hazard, and I'm using a join strategy inheritance between them. I'm attempting to retrieve data using Hibernate's findAll method.
@Getter
@Setter
@Entity
@Table(name = "dx_entity")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "table_name", discriminatorType =
DiscriminatorType.STRING)
@Where(clause = "deleted_at is null")
@NoArgsConstructor
public class DxEntity extends MultiTenantEntity implements Serializable
{
// Entity fields...
}
@Entity
@Table(name = "dx_temporary_hazard")
@Setter
@Getter
@DiscriminatorValue("dx_temporary_hazard")
public class TemporaryHazard extends DxEntity {
// Entity fields...
}
@RestController
public class TController {
private final TemporaryHazardRepository
temporaryHazardRepository;
public TController(TemporaryHazardRepository
temporaryHazardRepository) {
this.temporaryHazardRepository = temporaryHazardRepository;
}
@GetMapping("/test")
public Page<TemporaryHazard> test() {
return
temporaryHazardRepository.findAll(PageRequest.of(0,20));
}
}
When we trigger /test controller, Quires performed in console:
`SELECT th1_1.pk_id,
th1_0.changed_at,
th1_0.changed_by,
th1_0.created_at,
th1_0.created_by,
th1_0.created_layout,
th1_0.deleted_at,
th1_0.module_name,
th1_0.status,
th1_0.tag,
th1_0.tenant_id,
th1_0.updated_layout,
th1_1.abc,
th1_1.xyz,
th1_1.abc1,
th1_1.control_measures_required,
th1_1.xyz1,
th1_1.type
FROM PUBLIC.dx_entity th1_0
JOIN PUBLIC.dx_temporary_hazard th1_1
ON th1_0.pk_id=th1_1.pk_id
WHERE th1_0.tenant_id = ?
AND (
th1_0.deleted_at IS NULL)
AND th1_0.table_name='dx_temporary_hazard' offset ? rowsFETCH first ? rows only`
SELECT count(th1_1.pk_id) FROM PUBLIC.dx_entity th1_0 WHERE th1_0.tenant_id = ? AND ( th1_0.deleted_at IS NULL) AND th1_0.table_name='dx_temporary_hazard'
Error: SQL Error: 0, SQLState: 42P01
ERROR: missing FROM-clause entry for table
We can see that "th1_1.pk_id" is wrong alias name in count query. Hence query failing to return result. I tried with hibernate 6.2.x and 6.4.x version, No luck