Home > Software engineering >  Spring specification avoid unexpected CROSS JOIN
Spring specification avoid unexpected CROSS JOIN

Time:02-14

I have 2 entities:

  • 1 is the Org Structure (with a self-join refers to its parent org structure)
  • 2 is the Device (has FK from OrgStructure)
public class OrgStructure{

   @Id
   private Long id;

   @ManyToOne
   private OrgStructure parentOrgStructure;

}
public class Device {

   @Id
   private Long id;

   @ManyToOne
   private OrgStructure hospitalRoom
}

I have 1 specification which generates a CROSS JOIN query. My question is how to avoid unnecessary CROSS JOIN like this

criteriaBuilder.equal(root.get(Device_.orgStructure).get(OrgStructure_.parentOrgStructure).get(OrganizationStructure_.id), *param goes here*)
The generated query:
SELECT
    *
FROM
    devices device0_
CROSS JOIN org_structures organizati1_
WHERE
    device0_.hospital_room_id = organizati1_.id_organization_structure
    AND organizati1_.organization_structure_id =?

CodePudding user response:

please change :

root.get(Device_.orgStructure).get(OrgStructure_.parentOrgStructure)

to

root.join(Device_.orgStructure).join(OrgStructure_.parentOrgStructure)
  • Related