I have below entity relationship (1:1)
Contact <—>ContactCustomField
Contact <—> ContactCustomField2.
All the entitites have account_id as partition_key. When we do em.find(SalesContact.class, id) the query generated does not have account_id filter in where clause condition.
Query generated
select
sc1_0.id,
sc1_0.account_id,
ccf1_0.id,
ccf1_0.account_id,
ccf1_0.contact_id,
ccf2_0.id,
ccf2_0.account_id,
ccf2_0.contact_id,
sc1_0.first_name,
sc1_0.last_name
from
contacts sc1_0
left join
contact_custom_field2 ccf1_0
on sc1_0.account_id=ccf1_0.account_id
and sc1_0.id=ccf1_0.contact_id
left join
contact_custom_fields ccf2_0
on sc1_0.account_id=ccf2_0.account_id
and sc1_0.id=ccf2_0.contact_id
where
sc1_0.id=?
If i use criteria API to construct both id/account_id filter number of queries generated are more.
-
Loads contact using id/account_id
-
Loads contact custom field by explicitly fetching
-
Loads contact custom field 2 by explicitly fetching
-
Loads contact custom fields by joining contact / contact_custom_field2 / contact_custom_field
Hibernate:
select
sc1_0.id,
sc1_0.account_id,
sc1_0.first_name,
sc1_0.last_name
from
contacts sc1_0
where
sc1_0.id=?
and sc1_0.account_id=?
Hibernate:
select
ccf1_0.id,
ccf1_0.account_id,
ccf1_0.contact_id
from
contact_custom_field2 ccf1_0
where
(
ccf1_0.account_id, ccf1_0.contact_id
) in ((?, ?))
Hibernate:
select
sc1_0.id,
sc1_0.account_id,
ccf1_0.id,
ccf1_0.account_id,
ccf1_0.contact_id,
ccf2_0.id,
ccf2_0.account_id,
ccf2_0.contact_id,
sc1_0.first_name,
sc1_0.last_name
from
contacts sc1_0
left join
contact_custom_field2 ccf1_0
on sc1_0.account_id=ccf1_0.account_id
and sc1_0.id=ccf1_0.contact_id
left join
contact_custom_fields ccf2_0
on sc1_0.account_id=ccf2_0.account_id
and sc1_0.id=ccf2_0.contact_id
where
(
sc1_0.account_id, sc1_0.id
) in ((?, ?))
Hibernate:
select
ccf1_0.id,
ccf1_0.account_id,
ccf1_0.contact_id
from
contact_custom_fields ccf1_0
where
(
ccf1_0.account_id, ccf1_0.contact_id
) in ((?, ?))