code: @SuppressWarnings("unchecked")
      public static List<Audit> findByEntityHostnameMessage(EntityManager em, JPAEntity entity, String hostName, String message) {
          boolean isAndrequired = false;
          StringBuilder queryString = new StringBuilder("SELECT a FROM Audit a ");
  
          if (entity != null) {
              isAndrequired = true;
              queryString.append(" WHERE a.entityId=:entityId ");
          }
          if (hostName != null && !hostName.isEmpty()) {
              if (isAndrequired) {
                  queryString.append(" AND ");
              } else {
                  isAndrequired = true;
                  queryString.append(" WHERE ");
              }
              queryString.append(" a.hostName = :hostName ");
          }
          if (message != null && !message.isEmpty()) {
              if (isAndrequired) {
                  queryString.append(" AND ");
              } else {
                  isAndrequired = true;
                  queryString.append(" WHERE ");
              }
              queryString.append(" a.message LIKE :message ");
          }
  
          Query query = em.createQuery(queryString.toString(), Audit.class);
  
          if (entity != null) {
              query.setParameter("entityId", entity.getId());
          }
          if (hostName != null && !hostName.isEmpty()) {
              query.setParameter("hostName", hostName);
          }
          if (message != null && !message.isEmpty()) {
              query.setParameter("message", "%" + message + "%");
          }
          return query.getResultList();
      }
docs: Finds audit history based on the entity, host name and message fragment.
  
  @param   em        The entity manager. Cannot be null.
  @param   entity    The entity. If null, result will not be filtered by entity.
  @param   hostName  The host name. If null, result will not be filtered by host name.
  @param   message   The message fragment. If not null, result will be filtered by messages containing the fragment.
  
  @return  The audit history. Will not be null, but may be empty.