@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.entity=:entity ");
  }
  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("entity",entity);
  }
  if (hostName != null && !hostName.isEmpty()) {
    query.setParameter("hostName",hostName);
  }
  if (message != null && !message.isEmpty()) {
    query.setParameter("message","%" + message + "%");
  }
  return query.getResultList();
}

@ 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.entity=:entity " ) ; } 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 ( "entity" , entity ) ; } if ( hostName != null && ! hostName . isEmpty ( ) ) { query . setParameter ( "hostName" , hostName ) ; } if ( message != null && ! message . isEmpty ( ) ) { query . setParameter ( "message" , "%" + message + "%" ) ; } return query . getResultList ( ) ; }
finds audit history based on the entity , host name and message fragment .
