Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring specification avoid unexpected CROSS JOIN

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 =?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

please change :

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

to

root.join(Device_.orgStructure).join(OrgStructure_.parentOrgStructure)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading