My entity looks like below: to simplify I am just showing two fields. We decided to do fetch all to load all properties at once.
@Entity
public class Person{
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<PhoneNumber> phoneNumbers = new HashSet<>(0);
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "AGENCY_ID")
private Agency agency;
left join:(This returns duplicate records.)
select person
from Person person left join person.agency ag left join person.phoneNumbers
where upper(person.name) LIKE '%SU%' and upper(person.status.rcode) = 'ACTIVE'
order by person.agency.name asc, person.id asc
left join with distinct : This gives "ORA-01791: not a SELECTed expression" because i am using agency field in order clause.
select distinct person
from Person person left join person.agency ag left join person.phoneNumbers
where upper(person.name) LIKE '%SU%' and upper(person.Status.rcode) = 'ACTIVE'
order by person.agency.name asc, person.id asc
left join fetch : This works fine, no duplicates . but has big performance hit for pulling 2000 person records. taking about 15 seconds vs 1 with just left join.
select person
from Person person left join fetch person.agency ag left join fetch person.phoneNumbers
where upper(person.name) LIKE '%SU%' and upper(person.Status.rcode) = 'ACTIVE'
order by person.agency.name asc, person.id asc
fetch all properties: No duplicates. performs better.But.. When I try to query a person like below (this person doesn't have any agency): it returns the person record (that means it is doing a left join).
select person
from Person person
fetch all properties
where upper(person.name) LIKE '%SU%' and upper(person.status) = 'ACTIVE'
order by person.id asc
But when I do below, I don't get the person record back. (Difference is added field of agency in order by clause, seems like it is not doing left join in this case.)
select person
from Person person
fetch all properties
where upper(person.name) LIKE '%SU%' and upper(person.status) = 'ACTIVE'
order by person.agency.name asc, person.id asc
What i am looking is to avoid duplicates, sortable by all fields of person with good performance.