code: public <T, Y> T getUniqueByAttribute(Class<T> entityCls,
              String attributeName, Y value) {
          if (entityCls == null) {
              throw new IllegalArgumentException("entityCls cannot be null");
          }
          if (attributeName == null) {
              throw new IllegalArgumentException("attributeName cannot be null");
          }
          TypedQuery<T> query = createTypedQuery(entityCls, attributeName, value);
          T result = null;
          try {
              result = query.getSingleResult();
          } catch (NonUniqueResultException nure) {
              LOGGER.warn("Result not unique for {}: {} = {}",
                      entityCls.getName(), attributeName, value);
              result = query.getResultList().get(0);
          } catch (NoResultException nre) {
              LOGGER.debug("Result not existant for {}: {} = {}",
                      entityCls.getName(), attributeName, value);
          }
          return result;
      }
docs: Executes a query for the entity with the given attribute value. This
  method assumes that at most one instance of the given entity will be a
  match. This typically is used with attributes with a uniqueness
  constraint.
  
  @param <T> the type of the entity.
  @param <Y> the type of the attribute.
  @param entityCls the entity class. Cannot be <code>null</code>.
  @param attributeName the name of the attribute. Cannot be
  <code>null</code>.
  @param value the value. If there is more than one matching instance, only
  the first will be returned, and a warning will be logged.
  
  @return the matching instance, or <code>null</code> if there is none.