Issue
I have the following mappings:
ProductProviderDetail.class
@Entity
@Table
public class ProductProviderDetail {
// other fields
@Embedded
private AuditInformation auditInformation;
// getters and setters
}
AuditInformation.class
@Embeddable
public class AuditInformation {
// other fields
@ManyToOne(fetch = FetchType.LAZY, optional = true) // default is true but I want to make a point here
@JoinColumn(name = "head", referencedColumnName = "id")
private Head head;
// getters and setters
}
As you can see there's an association inside of a ComponentType
such that it's optional
.
Querying this property would look like
CriteriaBuilder builder = session.getCriteriaBuilder();
CriteriaQuery<Tuple> query = builder.createTupleQuery();
Root<ProductProviderDetail > root = query.from(ProductProviderDetail .class);
query.multiselect(root.get("auditInformation").get("head"));
Since the association is optional, I'd like to make a LEFT_JOIN which, in this case, would be something like
root.get("auditInformation").join("head", JoinType.LEFT);
The thing is root.get("auditInformation")
would return an instance of SingularAttributePath
which is not capable of doing a .join()
operation.
Is there any other ways that I can achieve this type of query with CriteriaQuery API? Thank you for any help.
Solution
I found a solution after browsing the site. Apparently, we're supposed to do .join()
on both the component and attribute level. Like so.
root.join("auditInformation").join("head", JoinType.LEFT);
Answered By - Ngọc Hy