com.bemis.portal.order.service.persistence.impl.OrderPersistenceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.bemis.portal.order.service.persistence.impl.OrderPersistenceImpl.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.bemis.portal.order.service.persistence.impl;

import aQute.bnd.annotation.ProviderType;

import com.bemis.portal.order.exception.NoSuchOrderException;
import com.bemis.portal.order.model.Order;
import com.bemis.portal.order.model.impl.OrderImpl;
import com.bemis.portal.order.model.impl.OrderModelImpl;
import com.bemis.portal.order.service.persistence.OrderPersistence;

import com.liferay.portal.kernel.dao.orm.EntityCache;
import com.liferay.portal.kernel.dao.orm.FinderCache;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
import com.liferay.portal.kernel.service.persistence.CompanyProvider;
import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.spring.extender.service.ServiceReference;

import java.io.Serializable;

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * The persistence implementation for the order service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Prathima Shreenath
 * @see OrderPersistence
 * @see com.bemis.portal.order.service.persistence.OrderUtil
 * @generated
 */
@ProviderType
public class OrderPersistenceImpl extends BasePersistenceImpl<Order> implements OrderPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link OrderUtil} to access the order persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = OrderImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY + ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(OrderModelImpl.ENTITY_CACHE_ENABLED,
            OrderModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll",
            new String[0]);
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByBemisCustId", new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByBemisCustId", new String[] { String.class.getName() },
            OrderModelImpl.BEMISCUSTID_COLUMN_BITMASK | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByBemisCustId",
            new String[] { String.class.getName() });

    /**
     * Returns all the orders where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @return the matching orders
     */
    @Override
    public List<Order> findByBemisCustId(String bemisCustId) {
        return findByBemisCustId(bemisCustId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where bemisCustId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByBemisCustId(String bemisCustId, int start, int end) {
        return findByBemisCustId(bemisCustId, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where bemisCustId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByBemisCustId(String bemisCustId, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByBemisCustId(bemisCustId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where bemisCustId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByBemisCustId(String bemisCustId, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISCUSTID;
            finderArgs = new Object[] { bemisCustId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_BEMISCUSTID;
            finderArgs = new Object[] { bemisCustId, start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(bemisCustId, order.getBemisCustId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindBemisCustId = false;

            if (bemisCustId == null) {
                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_1);
            } else if (bemisCustId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_3);
            } else {
                bindBemisCustId = true;

                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustId) {
                    qPos.add(bemisCustId);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByBemisCustId_First(String bemisCustId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByBemisCustId_First(bemisCustId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisCustId=");
        msg.append(bemisCustId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByBemisCustId_First(String bemisCustId, OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByBemisCustId(bemisCustId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByBemisCustId_Last(String bemisCustId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByBemisCustId_Last(bemisCustId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisCustId=");
        msg.append(bemisCustId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByBemisCustId_Last(String bemisCustId, OrderByComparator<Order> orderByComparator) {
        int count = countByBemisCustId(bemisCustId);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByBemisCustId(bemisCustId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where bemisCustId = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param bemisCustId the bemis cust ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByBemisCustId_PrevAndNext(long orderId, String bemisCustId,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByBemisCustId_PrevAndNext(session, order, bemisCustId, orderByComparator, true);

            array[1] = order;

            array[2] = getByBemisCustId_PrevAndNext(session, order, bemisCustId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByBemisCustId_PrevAndNext(Session session, Order order, String bemisCustId,
            OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindBemisCustId = false;

        if (bemisCustId == null) {
            query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_1);
        } else if (bemisCustId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_3);
        } else {
            bindBemisCustId = true;

            query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindBemisCustId) {
            qPos.add(bemisCustId);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where bemisCustId = &#63; from the database.
     *
     * @param bemisCustId the bemis cust ID
     */
    @Override
    public void removeByBemisCustId(String bemisCustId) {
        for (Order order : findByBemisCustId(bemisCustId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where bemisCustId = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @return the number of matching orders
     */
    @Override
    public int countByBemisCustId(String bemisCustId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_BEMISCUSTID;

        Object[] finderArgs = new Object[] { bemisCustId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindBemisCustId = false;

            if (bemisCustId == null) {
                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_1);
            } else if (bemisCustId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_3);
            } else {
                bindBemisCustId = true;

                query.append(_FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustId) {
                    qPos.add(bemisCustId);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_1 = "order_.bemisCustId IS NULL";
    private static final String _FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_2 = "order_.bemisCustId = ?";
    private static final String _FINDER_COLUMN_BEMISCUSTID_BEMISCUSTID_3 = "(order_.bemisCustId IS NULL OR order_.bemisCustId = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ITEMNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByItemNo", new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByItemNo", new String[] { String.class.getName() },
            OrderModelImpl.ITEMNO_COLUMN_BITMASK | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_ITEMNO = new FinderPath(OrderModelImpl.ENTITY_CACHE_ENABLED,
            OrderModelImpl.FINDER_CACHE_ENABLED, Long.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByItemNo", new String[] { String.class.getName() });

    /**
     * Returns all the orders where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @return the matching orders
     */
    @Override
    public List<Order> findByItemNo(String itemNo) {
        return findByItemNo(itemNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByItemNo(String itemNo, int start, int end) {
        return findByItemNo(itemNo, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByItemNo(String itemNo, int start, int end, OrderByComparator<Order> orderByComparator) {
        return findByItemNo(itemNo, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByItemNo(String itemNo, int start, int end, OrderByComparator<Order> orderByComparator,
            boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO;
            finderArgs = new Object[] { itemNo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ITEMNO;
            finderArgs = new Object[] { itemNo, start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(itemNo, order.getItemNo())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByItemNo_First(String itemNo, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByItemNo_First(itemNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("itemNo=");
        msg.append(itemNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_First(String itemNo, OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByItemNo(itemNo, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByItemNo_Last(String itemNo, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByItemNo_Last(itemNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("itemNo=");
        msg.append(itemNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_Last(String itemNo, OrderByComparator<Order> orderByComparator) {
        int count = countByItemNo(itemNo);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByItemNo(itemNo, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where itemNo = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByItemNo_PrevAndNext(long orderId, String itemNo, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByItemNo_PrevAndNext(session, order, itemNo, orderByComparator, true);

            array[1] = order;

            array[2] = getByItemNo_PrevAndNext(session, order, itemNo, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByItemNo_PrevAndNext(Session session, Order order, String itemNo,
            OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindItemNo = false;

        if (itemNo == null) {
            query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_1);
        } else if (itemNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_3);
        } else {
            bindItemNo = true;

            query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindItemNo) {
            qPos.add(itemNo);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where itemNo = &#63; from the database.
     *
     * @param itemNo the item no
     */
    @Override
    public void removeByItemNo(String itemNo) {
        for (Order order : findByItemNo(itemNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where itemNo = &#63;.
     *
     * @param itemNo the item no
     * @return the number of matching orders
     */
    @Override
    public int countByItemNo(String itemNo) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_ITEMNO;

        Object[] finderArgs = new Object[] { itemNo };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_ITEMNO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_ITEMNO_ITEMNO_1 = "order_.itemNo IS NULL";
    private static final String _FINDER_COLUMN_ITEMNO_ITEMNO_2 = "order_.itemNo = ?";
    private static final String _FINDER_COLUMN_ITEMNO_ITEMNO_3 = "(order_.itemNo IS NULL OR order_.itemNo = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByProductId", new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByProductId", new String[] { String.class.getName() },
            OrderModelImpl.BEMISPRODUCTID_COLUMN_BITMASK | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByProductId", new String[] { String.class.getName() });

    /**
     * Returns all the orders where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @return the matching orders
     */
    @Override
    public List<Order> findByProductId(String bemisProductId) {
        return findByProductId(bemisProductId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where bemisProductId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByProductId(String bemisProductId, int start, int end) {
        return findByProductId(bemisProductId, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where bemisProductId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByProductId(String bemisProductId, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByProductId(bemisProductId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where bemisProductId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByProductId(String bemisProductId, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PRODUCTID;
            finderArgs = new Object[] { bemisProductId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PRODUCTID;
            finderArgs = new Object[] { bemisProductId,

                    start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(bemisProductId, order.getBemisProductId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByProductId_First(String bemisProductId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByProductId_First(bemisProductId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisProductId=");
        msg.append(bemisProductId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByProductId_First(String bemisProductId, OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByProductId(bemisProductId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByProductId_Last(String bemisProductId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByProductId_Last(bemisProductId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisProductId=");
        msg.append(bemisProductId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByProductId_Last(String bemisProductId, OrderByComparator<Order> orderByComparator) {
        int count = countByProductId(bemisProductId);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByProductId(bemisProductId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where bemisProductId = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param bemisProductId the bemis product ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByProductId_PrevAndNext(long orderId, String bemisProductId,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByProductId_PrevAndNext(session, order, bemisProductId, orderByComparator, true);

            array[1] = order;

            array[2] = getByProductId_PrevAndNext(session, order, bemisProductId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByProductId_PrevAndNext(Session session, Order order, String bemisProductId,
            OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindBemisProductId = false;

        if (bemisProductId == null) {
            query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_1);
        } else if (bemisProductId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_3);
        } else {
            bindBemisProductId = true;

            query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindBemisProductId) {
            qPos.add(bemisProductId);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where bemisProductId = &#63; from the database.
     *
     * @param bemisProductId the bemis product ID
     */
    @Override
    public void removeByProductId(String bemisProductId) {
        for (Order order : findByProductId(bemisProductId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where bemisProductId = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @return the number of matching orders
     */
    @Override
    public int countByProductId(String bemisProductId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_PRODUCTID;

        Object[] finderArgs = new Object[] { bemisProductId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_1 = "order_.bemisProductId IS NULL";
    private static final String _FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_2 = "order_.bemisProductId = ?";
    private static final String _FINDER_COLUMN_PRODUCTID_BEMISPRODUCTID_3 = "(order_.bemisProductId IS NULL OR order_.bemisProductId = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PURCHASEORDERNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPurchaseOrderNo", new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPurchaseOrderNo",
            new String[] { String.class.getName() },
            OrderModelImpl.PURCHASEORDERNO_COLUMN_BITMASK | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_PURCHASEORDERNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPurchaseOrderNo",
            new String[] { String.class.getName() });

    /**
     * Returns all the orders where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @return the matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo(String purchaseOrderNo) {
        return findByPurchaseOrderNo(purchaseOrderNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo(String purchaseOrderNo, int start, int end) {
        return findByPurchaseOrderNo(purchaseOrderNo, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo(String purchaseOrderNo, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByPurchaseOrderNo(purchaseOrderNo, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo(String purchaseOrderNo, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO;
            finderArgs = new Object[] { purchaseOrderNo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PURCHASEORDERNO;
            finderArgs = new Object[] { purchaseOrderNo,

                    start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(purchaseOrderNo, order.getPurchaseOrderNo())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPurchaseOrderNo_First(String purchaseOrderNo, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByPurchaseOrderNo_First(purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPurchaseOrderNo_First(String purchaseOrderNo, OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByPurchaseOrderNo(purchaseOrderNo, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPurchaseOrderNo_Last(String purchaseOrderNo, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByPurchaseOrderNo_Last(purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPurchaseOrderNo_Last(String purchaseOrderNo, OrderByComparator<Order> orderByComparator) {
        int count = countByPurchaseOrderNo(purchaseOrderNo);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByPurchaseOrderNo(purchaseOrderNo, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where purchaseOrderNo = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByPurchaseOrderNo_PrevAndNext(long orderId, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByPurchaseOrderNo_PrevAndNext(session, order, purchaseOrderNo, orderByComparator, true);

            array[1] = order;

            array[2] = getByPurchaseOrderNo_PrevAndNext(session, order, purchaseOrderNo, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByPurchaseOrderNo_PrevAndNext(Session session, Order order, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindPurchaseOrderNo = false;

        if (purchaseOrderNo == null) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_1);
        } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_3);
        } else {
            bindPurchaseOrderNo = true;

            query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindPurchaseOrderNo) {
            qPos.add(purchaseOrderNo);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where purchaseOrderNo = &#63; from the database.
     *
     * @param purchaseOrderNo the purchase order no
     */
    @Override
    public void removeByPurchaseOrderNo(String purchaseOrderNo) {
        for (Order order : findByPurchaseOrderNo(purchaseOrderNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where purchaseOrderNo = &#63;.
     *
     * @param purchaseOrderNo the purchase order no
     * @return the number of matching orders
     */
    @Override
    public int countByPurchaseOrderNo(String purchaseOrderNo) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_PURCHASEORDERNO;

        Object[] finderArgs = new Object[] { purchaseOrderNo };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_1 = "order_.purchaseOrderNo IS NULL";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_2 = "order_.purchaseOrderNo = ?";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_PURCHASEORDERNO_3 = "(order_.purchaseOrderNo IS NULL OR order_.purchaseOrderNo = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SHIPTOID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByShipToId", new String[] { String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SHIPTOID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByShipToId", new String[] { String.class.getName() },
            OrderModelImpl.SHIPTOID_COLUMN_BITMASK | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_SHIPTOID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByShipToId", new String[] { String.class.getName() });

    /**
     * Returns all the orders where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @return the matching orders
     */
    @Override
    public List<Order> findByShipToId(String shipToId) {
        return findByShipToId(shipToId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where shipToId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param shipToId the ship to ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByShipToId(String shipToId, int start, int end) {
        return findByShipToId(shipToId, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where shipToId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param shipToId the ship to ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByShipToId(String shipToId, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByShipToId(shipToId, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where shipToId = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param shipToId the ship to ID
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByShipToId(String shipToId, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SHIPTOID;
            finderArgs = new Object[] { shipToId };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SHIPTOID;
            finderArgs = new Object[] { shipToId, start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(shipToId, order.getShipToId())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(3 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(3);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindShipToId = false;

            if (shipToId == null) {
                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_1);
            } else if (shipToId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_3);
            } else {
                bindShipToId = true;

                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindShipToId) {
                    qPos.add(shipToId);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByShipToId_First(String shipToId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByShipToId_First(shipToId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("shipToId=");
        msg.append(shipToId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByShipToId_First(String shipToId, OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByShipToId(shipToId, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByShipToId_Last(String shipToId, OrderByComparator<Order> orderByComparator)
            throws NoSuchOrderException {
        Order order = fetchByShipToId_Last(shipToId, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(4);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("shipToId=");
        msg.append(shipToId);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByShipToId_Last(String shipToId, OrderByComparator<Order> orderByComparator) {
        int count = countByShipToId(shipToId);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByShipToId(shipToId, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where shipToId = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param shipToId the ship to ID
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByShipToId_PrevAndNext(long orderId, String shipToId,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByShipToId_PrevAndNext(session, order, shipToId, orderByComparator, true);

            array[1] = order;

            array[2] = getByShipToId_PrevAndNext(session, order, shipToId, orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByShipToId_PrevAndNext(Session session, Order order, String shipToId,
            OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(4 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(3);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindShipToId = false;

        if (shipToId == null) {
            query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_1);
        } else if (shipToId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_3);
        } else {
            bindShipToId = true;

            query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindShipToId) {
            qPos.add(shipToId);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where shipToId = &#63; from the database.
     *
     * @param shipToId the ship to ID
     */
    @Override
    public void removeByShipToId(String shipToId) {
        for (Order order : findByShipToId(shipToId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where shipToId = &#63;.
     *
     * @param shipToId the ship to ID
     * @return the number of matching orders
     */
    @Override
    public int countByShipToId(String shipToId) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_SHIPTOID;

        Object[] finderArgs = new Object[] { shipToId };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindShipToId = false;

            if (shipToId == null) {
                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_1);
            } else if (shipToId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_3);
            } else {
                bindShipToId = true;

                query.append(_FINDER_COLUMN_SHIPTOID_SHIPTOID_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindShipToId) {
                    qPos.add(shipToId);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_SHIPTOID_SHIPTOID_1 = "order_.shipToId IS NULL";
    private static final String _FINDER_COLUMN_SHIPTOID_SHIPTOID_2 = "order_.shipToId = ?";
    private static final String _FINDER_COLUMN_SHIPTOID_SHIPTOID_3 = "(order_.shipToId IS NULL OR order_.shipToId = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ITEMNO_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByItemNo_ProductId",
            new String[] { String.class.getName(), String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByItemNo_ProductId",
            new String[] { String.class.getName(), String.class.getName() },
            OrderModelImpl.BEMISPRODUCTID_COLUMN_BITMASK | OrderModelImpl.ITEMNO_COLUMN_BITMASK
                    | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByItemNo_ProductId",
            new String[] { String.class.getName(), String.class.getName() });

    /**
     * Returns all the orders where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @return the matching orders
     */
    @Override
    public List<Order> findByItemNo_ProductId(String bemisProductId, String itemNo) {
        return findByItemNo_ProductId(bemisProductId, itemNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where bemisProductId = &#63; and itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByItemNo_ProductId(String bemisProductId, String itemNo, int start, int end) {
        return findByItemNo_ProductId(bemisProductId, itemNo, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where bemisProductId = &#63; and itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByItemNo_ProductId(String bemisProductId, String itemNo, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByItemNo_ProductId(bemisProductId, itemNo, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where bemisProductId = &#63; and itemNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByItemNo_ProductId(String bemisProductId, String itemNo, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO_PRODUCTID;
            finderArgs = new Object[] { bemisProductId, itemNo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ITEMNO_PRODUCTID;
            finderArgs = new Object[] { bemisProductId, itemNo,

                    start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(bemisProductId, order.getBemisProductId())
                            || !Objects.equals(itemNo, order.getItemNo())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(4);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_2);
            }

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByItemNo_ProductId_First(String bemisProductId, String itemNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByItemNo_ProductId_First(bemisProductId, itemNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisProductId=");
        msg.append(bemisProductId);

        msg.append(", itemNo=");
        msg.append(itemNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_ProductId_First(String bemisProductId, String itemNo,
            OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByItemNo_ProductId(bemisProductId, itemNo, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByItemNo_ProductId_Last(String bemisProductId, String itemNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByItemNo_ProductId_Last(bemisProductId, itemNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisProductId=");
        msg.append(bemisProductId);

        msg.append(", itemNo=");
        msg.append(itemNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_ProductId_Last(String bemisProductId, String itemNo,
            OrderByComparator<Order> orderByComparator) {
        int count = countByItemNo_ProductId(bemisProductId, itemNo);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByItemNo_ProductId(bemisProductId, itemNo, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByItemNo_ProductId_PrevAndNext(long orderId, String bemisProductId, String itemNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByItemNo_ProductId_PrevAndNext(session, order, bemisProductId, itemNo, orderByComparator,
                    true);

            array[1] = order;

            array[2] = getByItemNo_ProductId_PrevAndNext(session, order, bemisProductId, itemNo, orderByComparator,
                    false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByItemNo_ProductId_PrevAndNext(Session session, Order order, String bemisProductId,
            String itemNo, OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(4);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindBemisProductId = false;

        if (bemisProductId == null) {
            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_1);
        } else if (bemisProductId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_3);
        } else {
            bindBemisProductId = true;

            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_2);
        }

        boolean bindItemNo = false;

        if (itemNo == null) {
            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_1);
        } else if (itemNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_3);
        } else {
            bindItemNo = true;

            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindBemisProductId) {
            qPos.add(bemisProductId);
        }

        if (bindItemNo) {
            qPos.add(itemNo);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where bemisProductId = &#63; and itemNo = &#63; from the database.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     */
    @Override
    public void removeByItemNo_ProductId(String bemisProductId, String itemNo) {
        for (Order order : findByItemNo_ProductId(bemisProductId, itemNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where bemisProductId = &#63; and itemNo = &#63;.
     *
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @return the number of matching orders
     */
    @Override
    public int countByItemNo_ProductId(String bemisProductId, String itemNo) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID;

        Object[] finderArgs = new Object[] { bemisProductId, itemNo };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_2);
            }

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_1 = "order_.bemisProductId IS NULL AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_2 = "order_.bemisProductId = ? AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_BEMISPRODUCTID_3 = "(order_.bemisProductId IS NULL OR order_.bemisProductId = '') AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_1 = "order_.itemNo IS NULL";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_2 = "order_.itemNo = ?";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_ITEMNO_3 = "(order_.itemNo IS NULL OR order_.itemNo = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PORDERNO_ITEMNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPOrderNo_ItemNo",
            new String[] { String.class.getName(), String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORDERNO_ITEMNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPOrderNo_ItemNo",
            new String[] { String.class.getName(), String.class.getName() },
            OrderModelImpl.ITEMNO_COLUMN_BITMASK | OrderModelImpl.PURCHASEORDERNO_COLUMN_BITMASK
                    | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_PORDERNO_ITEMNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPOrderNo_ItemNo",
            new String[] { String.class.getName(), String.class.getName() });

    /**
     * Returns all the orders where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @return the matching orders
     */
    @Override
    public List<Order> findByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo) {
        return findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo, int start, int end) {
        return findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo, int start, int end,
            OrderByComparator<Order> orderByComparator) {
        return findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo, int start, int end,
            OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORDERNO_ITEMNO;
            finderArgs = new Object[] { itemNo, purchaseOrderNo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PORDERNO_ITEMNO;
            finderArgs = new Object[] { itemNo, purchaseOrderNo,

                    start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(itemNo, order.getItemNo())
                            || !Objects.equals(purchaseOrderNo, order.getPurchaseOrderNo())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(4);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_2);
            }

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPOrderNo_ItemNo_First(String itemNo, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByPOrderNo_ItemNo_First(itemNo, purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("itemNo=");
        msg.append(itemNo);

        msg.append(", purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPOrderNo_ItemNo_First(String itemNo, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPOrderNo_ItemNo_Last(String itemNo, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByPOrderNo_ItemNo_Last(itemNo, purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("itemNo=");
        msg.append(itemNo);

        msg.append(", purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPOrderNo_ItemNo_Last(String itemNo, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) {
        int count = countByPOrderNo_ItemNo(itemNo, purchaseOrderNo);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, count - 1, count, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByPOrderNo_ItemNo_PrevAndNext(long orderId, String itemNo, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByPOrderNo_ItemNo_PrevAndNext(session, order, itemNo, purchaseOrderNo, orderByComparator,
                    true);

            array[1] = order;

            array[2] = getByPOrderNo_ItemNo_PrevAndNext(session, order, itemNo, purchaseOrderNo, orderByComparator,
                    false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByPOrderNo_ItemNo_PrevAndNext(Session session, Order order, String itemNo,
            String purchaseOrderNo, OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(4);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindItemNo = false;

        if (itemNo == null) {
            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_1);
        } else if (itemNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_3);
        } else {
            bindItemNo = true;

            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_2);
        }

        boolean bindPurchaseOrderNo = false;

        if (purchaseOrderNo == null) {
            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_1);
        } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_3);
        } else {
            bindPurchaseOrderNo = true;

            query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindItemNo) {
            qPos.add(itemNo);
        }

        if (bindPurchaseOrderNo) {
            qPos.add(purchaseOrderNo);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where itemNo = &#63; and purchaseOrderNo = &#63; from the database.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     */
    @Override
    public void removeByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo) {
        for (Order order : findByPOrderNo_ItemNo(itemNo, purchaseOrderNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where itemNo = &#63; and purchaseOrderNo = &#63;.
     *
     * @param itemNo the item no
     * @param purchaseOrderNo the purchase order no
     * @return the number of matching orders
     */
    @Override
    public int countByPOrderNo_ItemNo(String itemNo, String purchaseOrderNo) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_PORDERNO_ITEMNO;

        Object[] finderArgs = new Object[] { itemNo, purchaseOrderNo };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_2);
            }

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_1 = "order_.itemNo IS NULL AND ";
    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_2 = "order_.itemNo = ? AND ";
    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_ITEMNO_3 = "(order_.itemNo IS NULL OR order_.itemNo = '') AND ";
    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_1 = "order_.purchaseOrderNo IS NULL";
    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_2 = "order_.purchaseOrderNo = ?";
    private static final String _FINDER_COLUMN_PORDERNO_ITEMNO_PURCHASEORDERNO_3 = "(order_.purchaseOrderNo IS NULL OR order_.purchaseOrderNo = '')";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByPurchaseOrderNo_BemisCustId",
            new String[] { String.class.getName(), String.class.getName(),

                    Integer.class.getName(), Integer.class.getName(), OrderByComparator.class.getName() });
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByPurchaseOrderNo_BemisCustId",
            new String[] { String.class.getName(), String.class.getName() },
            OrderModelImpl.BEMISCUSTID_COLUMN_BITMASK | OrderModelImpl.PURCHASEORDERNO_COLUMN_BITMASK
                    | OrderModelImpl.MATERIALAVAILDATE_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_PURCHASEORDERNO_BEMISCUSTID = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByPurchaseOrderNo_BemisCustId",
            new String[] { String.class.getName(), String.class.getName() });

    /**
     * Returns all the orders where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @return the matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo) {
        return findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
                null);
    }

    /**
     * Returns a range of all the orders where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo, int start,
            int end) {
        return findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, start, end, null);
    }

    /**
     * Returns an ordered range of all the orders where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo, int start,
            int end, OrderByComparator<Order> orderByComparator) {
        return findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of matching orders
     */
    @Override
    public List<Order> findByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo, int start,
            int end, OrderByComparator<Order> orderByComparator, boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID;
            finderArgs = new Object[] { bemisCustId, purchaseOrderNo };
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID;
            finderArgs = new Object[] { bemisCustId, purchaseOrderNo,

                    start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);

            if ((list != null) && !list.isEmpty()) {
                for (Order order : list) {
                    if (!Objects.equals(bemisCustId, order.getBemisCustId())
                            || !Objects.equals(purchaseOrderNo, order.getPurchaseOrderNo())) {
                        list = null;

                        break;
                    }
                }
            }
        }

        if (list == null) {
            StringBundler query = null;

            if (orderByComparator != null) {
                query = new StringBundler(4 + (orderByComparator.getOrderByFields().length * 2));
            } else {
                query = new StringBundler(4);
            }

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindBemisCustId = false;

            if (bemisCustId == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_1);
            } else if (bemisCustId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_3);
            } else {
                bindBemisCustId = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_2);
            }

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_2);
            }

            if (orderByComparator != null) {
                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);
            } else if (pagination) {
                query.append(OrderModelImpl.ORDER_BY_JPQL);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustId) {
                    qPos.add(bemisCustId);
                }

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Returns the first order in the ordered set where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPurchaseOrderNo_BemisCustId_First(String bemisCustId, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByPurchaseOrderNo_BemisCustId_First(bemisCustId, purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisCustId=");
        msg.append(bemisCustId);

        msg.append(", purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the first order in the ordered set where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the first matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPurchaseOrderNo_BemisCustId_First(String bemisCustId, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) {
        List<Order> list = findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, 0, 1, orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the last order in the ordered set where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByPurchaseOrderNo_BemisCustId_Last(String bemisCustId, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = fetchByPurchaseOrderNo_BemisCustId_Last(bemisCustId, purchaseOrderNo, orderByComparator);

        if (order != null) {
            return order;
        }

        StringBundler msg = new StringBundler(6);

        msg.append(_NO_SUCH_ENTITY_WITH_KEY);

        msg.append("bemisCustId=");
        msg.append(bemisCustId);

        msg.append(", purchaseOrderNo=");
        msg.append(purchaseOrderNo);

        msg.append(StringPool.CLOSE_CURLY_BRACE);

        throw new NoSuchOrderException(msg.toString());
    }

    /**
     * Returns the last order in the ordered set where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the last matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByPurchaseOrderNo_BemisCustId_Last(String bemisCustId, String purchaseOrderNo,
            OrderByComparator<Order> orderByComparator) {
        int count = countByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo);

        if (count == 0) {
            return null;
        }

        List<Order> list = findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, count - 1, count,
                orderByComparator);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    /**
     * Returns the orders before and after the current order in the ordered set where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param orderId the primary key of the current order
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
     * @return the previous, current, and next order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order[] findByPurchaseOrderNo_BemisCustId_PrevAndNext(long orderId, String bemisCustId,
            String purchaseOrderNo, OrderByComparator<Order> orderByComparator) throws NoSuchOrderException {
        Order order = findByPrimaryKey(orderId);

        Session session = null;

        try {
            session = openSession();

            Order[] array = new OrderImpl[3];

            array[0] = getByPurchaseOrderNo_BemisCustId_PrevAndNext(session, order, bemisCustId, purchaseOrderNo,
                    orderByComparator, true);

            array[1] = order;

            array[2] = getByPurchaseOrderNo_BemisCustId_PrevAndNext(session, order, bemisCustId, purchaseOrderNo,
                    orderByComparator, false);

            return array;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    protected Order getByPurchaseOrderNo_BemisCustId_PrevAndNext(Session session, Order order, String bemisCustId,
            String purchaseOrderNo, OrderByComparator<Order> orderByComparator, boolean previous) {
        StringBundler query = null;

        if (orderByComparator != null) {
            query = new StringBundler(5 + (orderByComparator.getOrderByConditionFields().length * 3)
                    + (orderByComparator.getOrderByFields().length * 3));
        } else {
            query = new StringBundler(4);
        }

        query.append(_SQL_SELECT_ORDER__WHERE);

        boolean bindBemisCustId = false;

        if (bemisCustId == null) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_1);
        } else if (bemisCustId.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_3);
        } else {
            bindBemisCustId = true;

            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_2);
        }

        boolean bindPurchaseOrderNo = false;

        if (purchaseOrderNo == null) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_1);
        } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_3);
        } else {
            bindPurchaseOrderNo = true;

            query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_2);
        }

        if (orderByComparator != null) {
            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();

            if (orderByConditionFields.length > 0) {
                query.append(WHERE_AND);
            }

            for (int i = 0; i < orderByConditionFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByConditionFields[i]);

                if ((i + 1) < orderByConditionFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
                    } else {
                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(WHERE_GREATER_THAN);
                    } else {
                        query.append(WHERE_LESSER_THAN);
                    }
                }
            }

            query.append(ORDER_BY_CLAUSE);

            String[] orderByFields = orderByComparator.getOrderByFields();

            for (int i = 0; i < orderByFields.length; i++) {
                query.append(_ORDER_BY_ENTITY_ALIAS);
                query.append(orderByFields[i]);

                if ((i + 1) < orderByFields.length) {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC_HAS_NEXT);
                    } else {
                        query.append(ORDER_BY_DESC_HAS_NEXT);
                    }
                } else {
                    if (orderByComparator.isAscending() ^ previous) {
                        query.append(ORDER_BY_ASC);
                    } else {
                        query.append(ORDER_BY_DESC);
                    }
                }
            }
        } else {
            query.append(OrderModelImpl.ORDER_BY_JPQL);
        }

        String sql = query.toString();

        Query q = session.createQuery(sql);

        q.setFirstResult(0);
        q.setMaxResults(2);

        QueryPos qPos = QueryPos.getInstance(q);

        if (bindBemisCustId) {
            qPos.add(bemisCustId);
        }

        if (bindPurchaseOrderNo) {
            qPos.add(purchaseOrderNo);
        }

        if (orderByComparator != null) {
            Object[] values = orderByComparator.getOrderByConditionValues(order);

            for (Object value : values) {
                qPos.add(value);
            }
        }

        List<Order> list = q.list();

        if (list.size() == 2) {
            return list.get(1);
        } else {
            return null;
        }
    }

    /**
     * Removes all the orders where bemisCustId = &#63; and purchaseOrderNo = &#63; from the database.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     */
    @Override
    public void removeByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo) {
        for (Order order : findByPurchaseOrderNo_BemisCustId(bemisCustId, purchaseOrderNo, QueryUtil.ALL_POS,
                QueryUtil.ALL_POS, null)) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders where bemisCustId = &#63; and purchaseOrderNo = &#63;.
     *
     * @param bemisCustId the bemis cust ID
     * @param purchaseOrderNo the purchase order no
     * @return the number of matching orders
     */
    @Override
    public int countByPurchaseOrderNo_BemisCustId(String bemisCustId, String purchaseOrderNo) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_PURCHASEORDERNO_BEMISCUSTID;

        Object[] finderArgs = new Object[] { bemisCustId, purchaseOrderNo };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindBemisCustId = false;

            if (bemisCustId == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_1);
            } else if (bemisCustId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_3);
            } else {
                bindBemisCustId = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_2);
            }

            boolean bindPurchaseOrderNo = false;

            if (purchaseOrderNo == null) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_1);
            } else if (purchaseOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_3);
            } else {
                bindPurchaseOrderNo = true;

                query.append(_FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_2);
            }

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisCustId) {
                    qPos.add(bemisCustId);
                }

                if (bindPurchaseOrderNo) {
                    qPos.add(purchaseOrderNo);
                }

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_1 = "order_.bemisCustId IS NULL AND ";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_2 = "order_.bemisCustId = ? AND ";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_BEMISCUSTID_3 = "(order_.bemisCustId IS NULL OR order_.bemisCustId = '') AND ";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_1 = "order_.purchaseOrderNo IS NULL";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_2 = "order_.purchaseOrderNo = ?";
    private static final String _FINDER_COLUMN_PURCHASEORDERNO_BEMISCUSTID_PURCHASEORDERNO_3 = "(order_.purchaseOrderNo IS NULL OR order_.purchaseOrderNo = '')";
    public static final FinderPath FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, OrderImpl.class,
            FINDER_CLASS_NAME_ENTITY, "fetchByItemNo_ProductId_LineNo_BOrderNo",
            new String[] { String.class.getName(), String.class.getName(), String.class.getName(),
                    Integer.class.getName() },
            OrderModelImpl.BEMISORDERNO_COLUMN_BITMASK | OrderModelImpl.BEMISPRODUCTID_COLUMN_BITMASK
                    | OrderModelImpl.ITEMNO_COLUMN_BITMASK | OrderModelImpl.LINENUMBER_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO = new FinderPath(
            OrderModelImpl.ENTITY_CACHE_ENABLED, OrderModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByItemNo_ProductId_LineNo_BOrderNo",
            new String[] { String.class.getName(), String.class.getName(), String.class.getName(),
                    Integer.class.getName() });

    /**
     * Returns the order where bemisOrderNo = &#63; and bemisProductId = &#63; and itemNo = &#63; and lineNumber = &#63; or throws a {@link NoSuchOrderException} if it could not be found.
     *
     * @param bemisOrderNo the bemis order no
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param lineNumber the line number
     * @return the matching order
     * @throws NoSuchOrderException if a matching order could not be found
     */
    @Override
    public Order findByItemNo_ProductId_LineNo_BOrderNo(String bemisOrderNo, String bemisProductId, String itemNo,
            int lineNumber) throws NoSuchOrderException {
        Order order = fetchByItemNo_ProductId_LineNo_BOrderNo(bemisOrderNo, bemisProductId, itemNo, lineNumber);

        if (order == null) {
            StringBundler msg = new StringBundler(10);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("bemisOrderNo=");
            msg.append(bemisOrderNo);

            msg.append(", bemisProductId=");
            msg.append(bemisProductId);

            msg.append(", itemNo=");
            msg.append(itemNo);

            msg.append(", lineNumber=");
            msg.append(lineNumber);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isDebugEnabled()) {
                _log.debug(msg.toString());
            }

            throw new NoSuchOrderException(msg.toString());
        }

        return order;
    }

    /**
     * Returns the order where bemisOrderNo = &#63; and bemisProductId = &#63; and itemNo = &#63; and lineNumber = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param bemisOrderNo the bemis order no
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param lineNumber the line number
     * @return the matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_ProductId_LineNo_BOrderNo(String bemisOrderNo, String bemisProductId, String itemNo,
            int lineNumber) {
        return fetchByItemNo_ProductId_LineNo_BOrderNo(bemisOrderNo, bemisProductId, itemNo, lineNumber, true);
    }

    /**
     * Returns the order where bemisOrderNo = &#63; and bemisProductId = &#63; and itemNo = &#63; and lineNumber = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param bemisOrderNo the bemis order no
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param lineNumber the line number
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the matching order, or <code>null</code> if a matching order could not be found
     */
    @Override
    public Order fetchByItemNo_ProductId_LineNo_BOrderNo(String bemisOrderNo, String bemisProductId, String itemNo,
            int lineNumber, boolean retrieveFromCache) {
        Object[] finderArgs = new Object[] { bemisOrderNo, bemisProductId, itemNo, lineNumber };

        Object result = null;

        if (retrieveFromCache) {
            result = finderCache.getResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, finderArgs, this);
        }

        if (result instanceof Order) {
            Order order = (Order) result;

            if (!Objects.equals(bemisOrderNo, order.getBemisOrderNo())
                    || !Objects.equals(bemisProductId, order.getBemisProductId())
                    || !Objects.equals(itemNo, order.getItemNo()) || (lineNumber != order.getLineNumber())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(6);

            query.append(_SQL_SELECT_ORDER__WHERE);

            boolean bindBemisOrderNo = false;

            if (bemisOrderNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_1);
            } else if (bemisOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_3);
            } else {
                bindBemisOrderNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_2);
            }

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_2);
            }

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_2);
            }

            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_LINENUMBER_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisOrderNo) {
                    qPos.add(bemisOrderNo);
                }

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                qPos.add(lineNumber);

                List<Order> list = q.list();

                if (list.isEmpty()) {
                    finderCache.putResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, finderArgs, list);
                } else {
                    Order order = list.get(0);

                    result = order;

                    cacheResult(order);

                    if ((order.getBemisOrderNo() == null) || !order.getBemisOrderNo().equals(bemisOrderNo)
                            || (order.getBemisProductId() == null)
                            || !order.getBemisProductId().equals(bemisProductId) || (order.getItemNo() == null)
                            || !order.getItemNo().equals(itemNo) || (order.getLineNumber() != lineNumber)) {
                        finderCache.putResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, finderArgs,
                                order);
                    }
                }
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (Order) result;
        }
    }

    /**
     * Removes the order where bemisOrderNo = &#63; and bemisProductId = &#63; and itemNo = &#63; and lineNumber = &#63; from the database.
     *
     * @param bemisOrderNo the bemis order no
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param lineNumber the line number
     * @return the order that was removed
     */
    @Override
    public Order removeByItemNo_ProductId_LineNo_BOrderNo(String bemisOrderNo, String bemisProductId, String itemNo,
            int lineNumber) throws NoSuchOrderException {
        Order order = findByItemNo_ProductId_LineNo_BOrderNo(bemisOrderNo, bemisProductId, itemNo, lineNumber);

        return remove(order);
    }

    /**
     * Returns the number of orders where bemisOrderNo = &#63; and bemisProductId = &#63; and itemNo = &#63; and lineNumber = &#63;.
     *
     * @param bemisOrderNo the bemis order no
     * @param bemisProductId the bemis product ID
     * @param itemNo the item no
     * @param lineNumber the line number
     * @return the number of matching orders
     */
    @Override
    public int countByItemNo_ProductId_LineNo_BOrderNo(String bemisOrderNo, String bemisProductId, String itemNo,
            int lineNumber) {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO;

        Object[] finderArgs = new Object[] { bemisOrderNo, bemisProductId, itemNo, lineNumber };

        Long count = (Long) finderCache.getResult(finderPath, finderArgs, this);

        if (count == null) {
            StringBundler query = new StringBundler(5);

            query.append(_SQL_COUNT_ORDER__WHERE);

            boolean bindBemisOrderNo = false;

            if (bemisOrderNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_1);
            } else if (bemisOrderNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_3);
            } else {
                bindBemisOrderNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_2);
            }

            boolean bindBemisProductId = false;

            if (bemisProductId == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_1);
            } else if (bemisProductId.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_3);
            } else {
                bindBemisProductId = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_2);
            }

            boolean bindItemNo = false;

            if (itemNo == null) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_1);
            } else if (itemNo.equals(StringPool.BLANK)) {
                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_3);
            } else {
                bindItemNo = true;

                query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_2);
            }

            query.append(_FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_LINENUMBER_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                if (bindBemisOrderNo) {
                    qPos.add(bemisOrderNo);
                }

                if (bindBemisProductId) {
                    qPos.add(bemisProductId);
                }

                if (bindItemNo) {
                    qPos.add(itemNo);
                }

                qPos.add(lineNumber);

                count = (Long) q.uniqueResult();

                finderCache.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_1 = "order_.bemisOrderNo IS NULL AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_2 = "order_.bemisOrderNo = ? AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISORDERNO_3 = "(order_.bemisOrderNo IS NULL OR order_.bemisOrderNo = '') AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_1 = "order_.bemisProductId IS NULL AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_2 = "order_.bemisProductId = ? AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_BEMISPRODUCTID_3 = "(order_.bemisProductId IS NULL OR order_.bemisProductId = '') AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_1 = "order_.itemNo IS NULL AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_2 = "order_.itemNo = ? AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_ITEMNO_3 = "(order_.itemNo IS NULL OR order_.itemNo = '') AND ";
    private static final String _FINDER_COLUMN_ITEMNO_PRODUCTID_LINENO_BORDERNO_LINENUMBER_2 = "order_.lineNumber = ?";

    public OrderPersistenceImpl() {
        setModelClass(Order.class);
    }

    /**
     * Caches the order in the entity cache if it is enabled.
     *
     * @param order the order
     */
    @Override
    public void cacheResult(Order order) {
        entityCache.putResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, order.getPrimaryKey(), order);

        finderCache.putResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, new Object[] {
                order.getBemisOrderNo(), order.getBemisProductId(), order.getItemNo(), order.getLineNumber() },
                order);

        order.resetOriginalValues();
    }

    /**
     * Caches the orders in the entity cache if it is enabled.
     *
     * @param orders the orders
     */
    @Override
    public void cacheResult(List<Order> orders) {
        for (Order order : orders) {
            if (entityCache.getResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class,
                    order.getPrimaryKey()) == null) {
                cacheResult(order);
            } else {
                order.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all orders.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        entityCache.clearCache(OrderImpl.class);

        finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the order.
     *
     * <p>
     * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(Order order) {
        entityCache.removeResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, order.getPrimaryKey());

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache((OrderModelImpl) order);
    }

    @Override
    public void clearCache(List<Order> orders) {
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (Order order : orders) {
            entityCache.removeResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, order.getPrimaryKey());

            clearUniqueFindersCache((OrderModelImpl) order);
        }
    }

    protected void cacheUniqueFindersCache(OrderModelImpl orderModelImpl, boolean isNew) {
        if (isNew) {
            Object[] args = new Object[] { orderModelImpl.getBemisOrderNo(), orderModelImpl.getBemisProductId(),
                    orderModelImpl.getItemNo(), orderModelImpl.getLineNumber() };

            finderCache.putResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args, Long.valueOf(1));
            finderCache.putResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args, orderModelImpl);
        } else {
            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getBemisOrderNo(), orderModelImpl.getBemisProductId(),
                        orderModelImpl.getItemNo(), orderModelImpl.getLineNumber() };

                finderCache.putResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args, Long.valueOf(1));
                finderCache.putResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args, orderModelImpl);
            }
        }
    }

    protected void clearUniqueFindersCache(OrderModelImpl orderModelImpl) {
        Object[] args = new Object[] { orderModelImpl.getBemisOrderNo(), orderModelImpl.getBemisProductId(),
                orderModelImpl.getItemNo(), orderModelImpl.getLineNumber() };

        finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args);
        finderCache.removeResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args);

        if ((orderModelImpl.getColumnBitmask()
                & FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO.getColumnBitmask()) != 0) {
            args = new Object[] { orderModelImpl.getOriginalBemisOrderNo(),
                    orderModelImpl.getOriginalBemisProductId(), orderModelImpl.getOriginalItemNo(),
                    orderModelImpl.getOriginalLineNumber() };

            finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args);
            finderCache.removeResult(FINDER_PATH_FETCH_BY_ITEMNO_PRODUCTID_LINENO_BORDERNO, args);
        }
    }

    /**
     * Creates a new order with the primary key. Does not add the order to the database.
     *
     * @param orderId the primary key for the new order
     * @return the new order
     */
    @Override
    public Order create(long orderId) {
        Order order = new OrderImpl();

        order.setNew(true);
        order.setPrimaryKey(orderId);

        order.setCompanyId(companyProvider.getCompanyId());

        return order;
    }

    /**
     * Removes the order with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param orderId the primary key of the order
     * @return the order that was removed
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order remove(long orderId) throws NoSuchOrderException {
        return remove((Serializable) orderId);
    }

    /**
     * Removes the order with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the order
     * @return the order that was removed
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order remove(Serializable primaryKey) throws NoSuchOrderException {
        Session session = null;

        try {
            session = openSession();

            Order order = (Order) session.get(OrderImpl.class, primaryKey);

            if (order == null) {
                if (_log.isDebugEnabled()) {
                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchOrderException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            return remove(order);
        } catch (NoSuchOrderException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected Order removeImpl(Order order) {
        order = toUnwrappedModel(order);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(order)) {
                order = (Order) session.get(OrderImpl.class, order.getPrimaryKeyObj());
            }

            if (order != null) {
                session.delete(order);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (order != null) {
            clearCache(order);
        }

        return order;
    }

    @Override
    public Order updateImpl(Order order) {
        order = toUnwrappedModel(order);

        boolean isNew = order.isNew();

        OrderModelImpl orderModelImpl = (OrderModelImpl) order;

        ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

        Date now = new Date();

        if (isNew && (order.getCreateDate() == null)) {
            if (serviceContext == null) {
                order.setCreateDate(now);
            } else {
                order.setCreateDate(serviceContext.getCreateDate(now));
            }
        }

        if (!orderModelImpl.hasSetModifiedDate()) {
            if (serviceContext == null) {
                order.setModifiedDate(now);
            } else {
                order.setModifiedDate(serviceContext.getModifiedDate(now));
            }
        }

        Session session = null;

        try {
            session = openSession();

            if (order.isNew()) {
                session.save(order);

                order.setNew(false);
            } else {
                order = (Order) session.merge(order);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !OrderModelImpl.COLUMN_BITMASK_ENABLED) {
            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        else {
            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISCUSTID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalBemisCustId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISCUSTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISCUSTID, args);

                args = new Object[] { orderModelImpl.getBemisCustId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_BEMISCUSTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_BEMISCUSTID, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalItemNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO, args);

                args = new Object[] { orderModelImpl.getItemNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PRODUCTID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalBemisProductId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PRODUCTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PRODUCTID, args);

                args = new Object[] { orderModelImpl.getBemisProductId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PRODUCTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PRODUCTID, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PURCHASEORDERNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO, args);

                args = new Object[] { orderModelImpl.getPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PURCHASEORDERNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SHIPTOID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalShipToId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_SHIPTOID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SHIPTOID, args);

                args = new Object[] { orderModelImpl.getShipToId() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_SHIPTOID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SHIPTOID, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO_PRODUCTID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalBemisProductId(),
                        orderModelImpl.getOriginalItemNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO_PRODUCTID, args);

                args = new Object[] { orderModelImpl.getBemisProductId(), orderModelImpl.getItemNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_ITEMNO_PRODUCTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ITEMNO_PRODUCTID, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORDERNO_ITEMNO.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalItemNo(),
                        orderModelImpl.getOriginalPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PORDERNO_ITEMNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORDERNO_ITEMNO, args);

                args = new Object[] { orderModelImpl.getItemNo(), orderModelImpl.getPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PORDERNO_ITEMNO, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PORDERNO_ITEMNO, args);
            }

            if ((orderModelImpl.getColumnBitmask()
                    & FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] { orderModelImpl.getOriginalBemisCustId(),
                        orderModelImpl.getOriginalPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PURCHASEORDERNO_BEMISCUSTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID, args);

                args = new Object[] { orderModelImpl.getBemisCustId(), orderModelImpl.getPurchaseOrderNo() };

                finderCache.removeResult(FINDER_PATH_COUNT_BY_PURCHASEORDERNO_BEMISCUSTID, args);
                finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_PURCHASEORDERNO_BEMISCUSTID, args);
            }
        }

        entityCache.putResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, order.getPrimaryKey(), order,
                false);

        clearUniqueFindersCache(orderModelImpl);
        cacheUniqueFindersCache(orderModelImpl, isNew);

        order.resetOriginalValues();

        return order;
    }

    protected Order toUnwrappedModel(Order order) {
        if (order instanceof OrderImpl) {
            return order;
        }

        OrderImpl orderImpl = new OrderImpl();

        orderImpl.setNew(order.isNew());
        orderImpl.setPrimaryKey(order.getPrimaryKey());

        orderImpl.setOrderId(order.getOrderId());
        orderImpl.setGroupId(order.getGroupId());
        orderImpl.setCompanyId(order.getCompanyId());
        orderImpl.setUserId(order.getUserId());
        orderImpl.setUserName(order.getUserName());
        orderImpl.setCreateDate(order.getCreateDate());
        orderImpl.setModifiedDate(order.getModifiedDate());
        orderImpl.setItemNo(order.getItemNo());
        orderImpl.setBemisProductId(order.getBemisProductId());
        orderImpl.setProdDesc1(order.getProdDesc1());
        orderImpl.setProdDesc2(order.getProdDesc2());
        orderImpl.setProdDesc3(order.getProdDesc3());
        orderImpl.setProdDesc4(order.getProdDesc4());
        orderImpl.setPurchaseOrderNo(order.getPurchaseOrderNo());
        orderImpl.setBemisOrderNo(order.getBemisOrderNo());
        orderImpl.setLineNumber(order.getLineNumber());
        orderImpl.setUom(order.getUom());
        orderImpl.setOrderDate(order.getOrderDate());
        orderImpl.setOrderedQty(order.getOrderedQty());
        orderImpl.setRequestedDate(order.getRequestedDate());
        orderImpl.setMaterialAvailDate(order.getMaterialAvailDate());
        orderImpl.setOnHandQty(order.getOnHandQty());
        orderImpl.setProducedQty(order.getProducedQty());
        orderImpl.setScheduledReleases(order.getScheduledReleases());
        orderImpl.setAvailQty(order.getAvailQty());
        orderImpl.setShippedQty(order.getShippedQty());
        orderImpl.setShipToId(order.getShipToId());
        orderImpl.setBemisCustId(order.getBemisCustId());
        orderImpl.setOpenStatus(order.isOpenStatus());

        return orderImpl;
    }

    /**
     * Returns the order with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the order
     * @return the order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order findByPrimaryKey(Serializable primaryKey) throws NoSuchOrderException {
        Order order = fetchByPrimaryKey(primaryKey);

        if (order == null) {
            if (_log.isDebugEnabled()) {
                _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchOrderException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
        }

        return order;
    }

    /**
     * Returns the order with the primary key or throws a {@link NoSuchOrderException} if it could not be found.
     *
     * @param orderId the primary key of the order
     * @return the order
     * @throws NoSuchOrderException if a order with the primary key could not be found
     */
    @Override
    public Order findByPrimaryKey(long orderId) throws NoSuchOrderException {
        return findByPrimaryKey((Serializable) orderId);
    }

    /**
     * Returns the order with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the order
     * @return the order, or <code>null</code> if a order with the primary key could not be found
     */
    @Override
    public Order fetchByPrimaryKey(Serializable primaryKey) {
        Serializable serializable = entityCache.getResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class,
                primaryKey);

        if (serializable == nullModel) {
            return null;
        }

        Order order = (Order) serializable;

        if (order == null) {
            Session session = null;

            try {
                session = openSession();

                order = (Order) session.get(OrderImpl.class, primaryKey);

                if (order != null) {
                    cacheResult(order);
                } else {
                    entityCache.putResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, primaryKey,
                            nullModel);
                }
            } catch (Exception e) {
                entityCache.removeResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return order;
    }

    /**
     * Returns the order with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param orderId the primary key of the order
     * @return the order, or <code>null</code> if a order with the primary key could not be found
     */
    @Override
    public Order fetchByPrimaryKey(long orderId) {
        return fetchByPrimaryKey((Serializable) orderId);
    }

    @Override
    public Map<Serializable, Order> fetchByPrimaryKeys(Set<Serializable> primaryKeys) {
        if (primaryKeys.isEmpty()) {
            return Collections.emptyMap();
        }

        Map<Serializable, Order> map = new HashMap<Serializable, Order>();

        if (primaryKeys.size() == 1) {
            Iterator<Serializable> iterator = primaryKeys.iterator();

            Serializable primaryKey = iterator.next();

            Order order = fetchByPrimaryKey(primaryKey);

            if (order != null) {
                map.put(primaryKey, order);
            }

            return map;
        }

        Set<Serializable> uncachedPrimaryKeys = null;

        for (Serializable primaryKey : primaryKeys) {
            Serializable serializable = entityCache.getResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class,
                    primaryKey);

            if (serializable != nullModel) {
                if (serializable == null) {
                    if (uncachedPrimaryKeys == null) {
                        uncachedPrimaryKeys = new HashSet<Serializable>();
                    }

                    uncachedPrimaryKeys.add(primaryKey);
                } else {
                    map.put(primaryKey, (Order) serializable);
                }
            }
        }

        if (uncachedPrimaryKeys == null) {
            return map;
        }

        StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) + 1);

        query.append(_SQL_SELECT_ORDER__WHERE_PKS_IN);

        for (Serializable primaryKey : uncachedPrimaryKeys) {
            query.append(String.valueOf(primaryKey));

            query.append(StringPool.COMMA);
        }

        query.setIndex(query.index() - 1);

        query.append(StringPool.CLOSE_PARENTHESIS);

        String sql = query.toString();

        Session session = null;

        try {
            session = openSession();

            Query q = session.createQuery(sql);

            for (Order order : (List<Order>) q.list()) {
                map.put(order.getPrimaryKeyObj(), order);

                cacheResult(order);

                uncachedPrimaryKeys.remove(order.getPrimaryKeyObj());
            }

            for (Serializable primaryKey : uncachedPrimaryKeys) {
                entityCache.putResult(OrderModelImpl.ENTITY_CACHE_ENABLED, OrderImpl.class, primaryKey, nullModel);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        return map;
    }

    /**
     * Returns all the orders.
     *
     * @return the orders
     */
    @Override
    public List<Order> findAll() {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the orders.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @return the range of orders
     */
    @Override
    public List<Order> findAll(int start, int end) {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the orders.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of orders
     */
    @Override
    public List<Order> findAll(int start, int end, OrderByComparator<Order> orderByComparator) {
        return findAll(start, end, orderByComparator, true);
    }

    /**
     * Returns an ordered range of all the orders.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link OrderModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of orders
     * @param end the upper bound of the range of orders (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @param retrieveFromCache whether to retrieve from the finder cache
     * @return the ordered range of orders
     */
    @Override
    public List<Order> findAll(int start, int end, OrderByComparator<Order> orderByComparator,
            boolean retrieveFromCache) {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) && (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<Order> list = null;

        if (retrieveFromCache) {
            list = (List<Order>) finderCache.getResult(finderPath, finderArgs, this);
        }

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 + (orderByComparator.getOrderByFields().length * 2));

                query.append(_SQL_SELECT_ORDER_);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_ORDER_;

                if (pagination) {
                    sql = sql.concat(OrderModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end, false);

                    Collections.sort(list);

                    list = Collections.unmodifiableList(list);
                } else {
                    list = (List<Order>) QueryUtil.list(q, getDialect(), start, end);
                }

                cacheResult(list);

                finderCache.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                finderCache.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the orders from the database.
     *
     */
    @Override
    public void removeAll() {
        for (Order order : findAll()) {
            remove(order);
        }
    }

    /**
     * Returns the number of orders.
     *
     * @return the number of orders
     */
    @Override
    public int countAll() {
        Long count = (Long) finderCache.getResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_ORDER_);

                count = (Long) q.uniqueResult();

                finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                finderCache.removeResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    @Override
    protected Map<String, Integer> getTableColumnsMap() {
        return OrderModelImpl.TABLE_COLUMNS_MAP;
    }

    /**
     * Initializes the order persistence.
     */
    public void afterPropertiesSet() {
    }

    public void destroy() {
        entityCache.removeCache(OrderImpl.class.getName());
        finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    @ServiceReference(type = CompanyProviderWrapper.class)
    protected CompanyProvider companyProvider;
    @ServiceReference(type = EntityCache.class)
    protected EntityCache entityCache;
    @ServiceReference(type = FinderCache.class)
    protected FinderCache finderCache;
    private static final String _SQL_SELECT_ORDER_ = "SELECT order_ FROM Order order_";
    private static final String _SQL_SELECT_ORDER__WHERE_PKS_IN = "SELECT order_ FROM Order order_ WHERE orderId IN (";
    private static final String _SQL_SELECT_ORDER__WHERE = "SELECT order_ FROM Order order_ WHERE ";
    private static final String _SQL_COUNT_ORDER_ = "SELECT COUNT(order_) FROM Order order_";
    private static final String _SQL_COUNT_ORDER__WHERE = "SELECT COUNT(order_) FROM Order order_ WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "order_.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Order exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Order exists with the key {";
    private static final Log _log = LogFactoryUtil.getLog(OrderPersistenceImpl.class);
}