2013-02-14 13 views
10

Cześć mam to duże oracle hibernacji aplikacji internetowych i wydaje się, aby dać ten błądHibernate kryterium klauzuli 1000 zerwać

ORA-01795: maximum number of expressions in a list is 1000

i muszę przetestowany przez kogoś jako zdefiniowany użytkownik hibernacji składnik kodu java dodać do mojego wyszukiwania klas java na moim ekranie tak łatwo, jak to możliwe, czy ktoś może mieć tak przetestowany komponent?

Odpowiedz

20

Próbowałem to poniżej kodu z link i wydaje się, że działa pięknie, wkleję kod w przypadku, gdy link został złamany w przyszłości.

Keep It Simple Keep it Smile :)

/** 
    * An utility method to build the Criterion Query IN clause if the number of parameter 
    * values passed has a size more than 1000. Oracle does not allow more than 
    * 1000 parameter values in a IN clause. Doing so a {@link SQLException} is 
    * thrown with error code, 'ORA-01795: maximum number of expressions in a list is 1000'. 
    * @param propertyName 
    * @param values 
    * @return 
    */ 
import java.util.List; 
import org.hibernate.criterion.Restrictions; 

/** 
* 
* @author 2796 
*/ 
public class SplitHibernateIn { 

    private static int PARAMETER_LIMIT = 999; 

    public static org.hibernate.criterion.Criterion buildInCriterion(String propertyName, List values) { 
     org.hibernate.criterion.Criterion criterion = null; 

     int listSize = values.size(); 
     for (int i = 0; i < listSize; i += PARAMETER_LIMIT) { 
      List subList; 
      if (listSize > i + PARAMETER_LIMIT) { 
       subList = values.subList(i, (i + PARAMETER_LIMIT)); 
      } else { 
       subList = values.subList(i, listSize); 
      } 
      if (criterion != null) { 
       criterion = Restrictions.or(criterion, Restrictions.in(propertyName, subList)); 
      } else { 
       criterion = Restrictions.in(propertyName, subList); 
      } 
     } 
     return criterion; 
    } 
} 
+0

FYI; Ten kod przetestowano na hibernacji 3 i oracle 10g – shareef

0

ten sam pomysł, ale przy użyciu javax predykatu.

private static int PARAMETER_LIMIT = 999; 

private static Predicate createInStatement(CriteriaBuilder cb, Path fieldName, List values) { 
    int listSize = values.size(); 
    Predicate predicate = null; 
    for (int i = 0; i < listSize; i += PARAMETER_LIMIT) { 
     List subList; 
     if (listSize > i + PARAMETER_LIMIT) { 
      subList = values.subList(i, (i + PARAMETER_LIMIT)); 
     } else { 
      subList = values.subList(i, listSize); 
     } 
     if (predicate == null) { 
      predicate = fieldName.in(subList); 
     } else { 
      predicate = cb.or(predicate, fieldName.in(subList)); 
     } 
    } 
    return predicate; 
} 

A Wykorzystanie

public List<Bean> getBeanList(List<Long> pkList) { 
    CriteriaBuilder cb = entityManager.getCriteriaBuilder(); 
    CriteriaQuery<Bean> query = cb.createQuery(Bean.class); 
    Root<Bean> root = query.from(Bean.class); 

    // "Bean_" is a @StaticMetamodel(Bean.class) 
    Predicate inStatement = createInStatement(cb, root.get(Bean_.pk), pkList); 

    query.select(root).where(inStatement); 
    return entityManager.createQuery(query).getResultList(); 
} 
Powiązane problemy