protected TypedQuery<SamlIdPMetadataDocument> buildQuery(final Optional<SamlRegisteredService> registeredService) {
        var sql = "SELECT r FROM SamlIdPMetadataDocument r ";
        if (registeredService.isPresent()) {
            sql += " WHERE r.appliesTo = :appliesTo";
        }
        val query = getEntityManager().createQuery(sql, SamlIdPMetadataDocument.class);
        if (registeredService.isPresent()) {
            query.setParameter("appliesTo", getAppliesToFor(registeredService));
        }
        return query.setMaxResults(1);
    }
Build query.

@param registeredService the registered service
@return the typed query
support/cas-server-support-saml-idp-metadata-jpa/src/main/java/org/apereo/cas/support/saml/idp/metadata/JpaSamlIdPMetadataLocator.java