001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.exception.NoSuchAddressException;
030    import com.liferay.portal.kernel.log.Log;
031    import com.liferay.portal.kernel.log.LogFactoryUtil;
032    import com.liferay.portal.kernel.model.Address;
033    import com.liferay.portal.kernel.model.CacheModel;
034    import com.liferay.portal.kernel.model.MVCCModel;
035    import com.liferay.portal.kernel.service.ServiceContext;
036    import com.liferay.portal.kernel.service.ServiceContextThreadLocal;
037    import com.liferay.portal.kernel.service.persistence.AddressPersistence;
038    import com.liferay.portal.kernel.service.persistence.CompanyProvider;
039    import com.liferay.portal.kernel.service.persistence.CompanyProviderWrapper;
040    import com.liferay.portal.kernel.service.persistence.impl.BasePersistenceImpl;
041    import com.liferay.portal.kernel.util.OrderByComparator;
042    import com.liferay.portal.kernel.util.SetUtil;
043    import com.liferay.portal.kernel.util.StringBundler;
044    import com.liferay.portal.kernel.util.StringPool;
045    import com.liferay.portal.kernel.util.Validator;
046    import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
047    import com.liferay.portal.model.impl.AddressImpl;
048    import com.liferay.portal.model.impl.AddressModelImpl;
049    
050    import java.io.Serializable;
051    
052    import java.util.Collections;
053    import java.util.Date;
054    import java.util.HashMap;
055    import java.util.HashSet;
056    import java.util.Iterator;
057    import java.util.List;
058    import java.util.Map;
059    import java.util.Objects;
060    import java.util.Set;
061    
062    /**
063     * The persistence implementation for the address service.
064     *
065     * <p>
066     * Caching information and settings can be found in <code>portal.properties</code>
067     * </p>
068     *
069     * @author Brian Wing Shun Chan
070     * @see AddressPersistence
071     * @see com.liferay.portal.kernel.service.persistence.AddressUtil
072     * @generated
073     */
074    @ProviderType
075    public class AddressPersistenceImpl extends BasePersistenceImpl<Address>
076            implements AddressPersistence {
077            /*
078             * NOTE FOR DEVELOPERS:
079             *
080             * Never modify or reference this class directly. Always use {@link AddressUtil} to access the address persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
081             */
082            public static final String FINDER_CLASS_NAME_ENTITY = AddressImpl.class.getName();
083            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
084                    ".List1";
085            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
086                    ".List2";
087            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
088                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
090            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
091                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
092                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
093            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
094                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
095                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
096            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
097                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
098                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid",
099                            new String[] {
100                                    String.class.getName(),
101                                    
102                            Integer.class.getName(), Integer.class.getName(),
103                                    OrderByComparator.class.getName()
104                            });
105            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
106                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
107                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid",
108                            new String[] { String.class.getName() },
109                            AddressModelImpl.UUID_COLUMN_BITMASK |
110                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
111            public static final FinderPath FINDER_PATH_COUNT_BY_UUID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
112                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
113                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid",
114                            new String[] { String.class.getName() });
115    
116            /**
117             * Returns all the addresses where uuid = &#63;.
118             *
119             * @param uuid the uuid
120             * @return the matching addresses
121             */
122            @Override
123            public List<Address> findByUuid(String uuid) {
124                    return findByUuid(uuid, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
125            }
126    
127            /**
128             * Returns a range of all the addresses where uuid = &#63;.
129             *
130             * <p>
131             * 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 AddressModelImpl}. 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.
132             * </p>
133             *
134             * @param uuid the uuid
135             * @param start the lower bound of the range of addresses
136             * @param end the upper bound of the range of addresses (not inclusive)
137             * @return the range of matching addresses
138             */
139            @Override
140            public List<Address> findByUuid(String uuid, int start, int end) {
141                    return findByUuid(uuid, start, end, null);
142            }
143    
144            /**
145             * Returns an ordered range of all the addresses where uuid = &#63;.
146             *
147             * <p>
148             * 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 AddressModelImpl}. 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.
149             * </p>
150             *
151             * @param uuid the uuid
152             * @param start the lower bound of the range of addresses
153             * @param end the upper bound of the range of addresses (not inclusive)
154             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
155             * @return the ordered range of matching addresses
156             */
157            @Override
158            public List<Address> findByUuid(String uuid, int start, int end,
159                    OrderByComparator<Address> orderByComparator) {
160                    return findByUuid(uuid, start, end, orderByComparator, true);
161            }
162    
163            /**
164             * Returns an ordered range of all the addresses where uuid = &#63;.
165             *
166             * <p>
167             * 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 AddressModelImpl}. 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.
168             * </p>
169             *
170             * @param uuid the uuid
171             * @param start the lower bound of the range of addresses
172             * @param end the upper bound of the range of addresses (not inclusive)
173             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
174             * @param retrieveFromCache whether to retrieve from the finder cache
175             * @return the ordered range of matching addresses
176             */
177            @Override
178            public List<Address> findByUuid(String uuid, int start, int end,
179                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
180                    boolean pagination = true;
181                    FinderPath finderPath = null;
182                    Object[] finderArgs = null;
183    
184                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
185                                    (orderByComparator == null)) {
186                            pagination = false;
187                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID;
188                            finderArgs = new Object[] { uuid };
189                    }
190                    else {
191                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID;
192                            finderArgs = new Object[] { uuid, start, end, orderByComparator };
193                    }
194    
195                    List<Address> list = null;
196    
197                    if (retrieveFromCache) {
198                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
199                                            this);
200    
201                            if ((list != null) && !list.isEmpty()) {
202                                    for (Address address : list) {
203                                            if (!Objects.equals(uuid, address.getUuid())) {
204                                                    list = null;
205    
206                                                    break;
207                                            }
208                                    }
209                            }
210                    }
211    
212                    if (list == null) {
213                            StringBundler query = null;
214    
215                            if (orderByComparator != null) {
216                                    query = new StringBundler(3 +
217                                                    (orderByComparator.getOrderByFields().length * 2));
218                            }
219                            else {
220                                    query = new StringBundler(3);
221                            }
222    
223                            query.append(_SQL_SELECT_ADDRESS_WHERE);
224    
225                            boolean bindUuid = false;
226    
227                            if (uuid == null) {
228                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
229                            }
230                            else if (uuid.equals(StringPool.BLANK)) {
231                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
232                            }
233                            else {
234                                    bindUuid = true;
235    
236                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
237                            }
238    
239                            if (orderByComparator != null) {
240                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
241                                            orderByComparator);
242                            }
243                            else
244                             if (pagination) {
245                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
246                            }
247    
248                            String sql = query.toString();
249    
250                            Session session = null;
251    
252                            try {
253                                    session = openSession();
254    
255                                    Query q = session.createQuery(sql);
256    
257                                    QueryPos qPos = QueryPos.getInstance(q);
258    
259                                    if (bindUuid) {
260                                            qPos.add(uuid);
261                                    }
262    
263                                    if (!pagination) {
264                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
265                                                            start, end, false);
266    
267                                            Collections.sort(list);
268    
269                                            list = Collections.unmodifiableList(list);
270                                    }
271                                    else {
272                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
273                                                            start, end);
274                                    }
275    
276                                    cacheResult(list);
277    
278                                    finderCache.putResult(finderPath, finderArgs, list);
279                            }
280                            catch (Exception e) {
281                                    finderCache.removeResult(finderPath, finderArgs);
282    
283                                    throw processException(e);
284                            }
285                            finally {
286                                    closeSession(session);
287                            }
288                    }
289    
290                    return list;
291            }
292    
293            /**
294             * Returns the first address in the ordered set where uuid = &#63;.
295             *
296             * @param uuid the uuid
297             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
298             * @return the first matching address
299             * @throws NoSuchAddressException if a matching address could not be found
300             */
301            @Override
302            public Address findByUuid_First(String uuid,
303                    OrderByComparator<Address> orderByComparator)
304                    throws NoSuchAddressException {
305                    Address address = fetchByUuid_First(uuid, orderByComparator);
306    
307                    if (address != null) {
308                            return address;
309                    }
310    
311                    StringBundler msg = new StringBundler(4);
312    
313                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
314    
315                    msg.append("uuid=");
316                    msg.append(uuid);
317    
318                    msg.append(StringPool.CLOSE_CURLY_BRACE);
319    
320                    throw new NoSuchAddressException(msg.toString());
321            }
322    
323            /**
324             * Returns the first address in the ordered set where uuid = &#63;.
325             *
326             * @param uuid the uuid
327             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
328             * @return the first matching address, or <code>null</code> if a matching address could not be found
329             */
330            @Override
331            public Address fetchByUuid_First(String uuid,
332                    OrderByComparator<Address> orderByComparator) {
333                    List<Address> list = findByUuid(uuid, 0, 1, orderByComparator);
334    
335                    if (!list.isEmpty()) {
336                            return list.get(0);
337                    }
338    
339                    return null;
340            }
341    
342            /**
343             * Returns the last address in the ordered set where uuid = &#63;.
344             *
345             * @param uuid the uuid
346             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
347             * @return the last matching address
348             * @throws NoSuchAddressException if a matching address could not be found
349             */
350            @Override
351            public Address findByUuid_Last(String uuid,
352                    OrderByComparator<Address> orderByComparator)
353                    throws NoSuchAddressException {
354                    Address address = fetchByUuid_Last(uuid, orderByComparator);
355    
356                    if (address != null) {
357                            return address;
358                    }
359    
360                    StringBundler msg = new StringBundler(4);
361    
362                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
363    
364                    msg.append("uuid=");
365                    msg.append(uuid);
366    
367                    msg.append(StringPool.CLOSE_CURLY_BRACE);
368    
369                    throw new NoSuchAddressException(msg.toString());
370            }
371    
372            /**
373             * Returns the last address in the ordered set where uuid = &#63;.
374             *
375             * @param uuid the uuid
376             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
377             * @return the last matching address, or <code>null</code> if a matching address could not be found
378             */
379            @Override
380            public Address fetchByUuid_Last(String uuid,
381                    OrderByComparator<Address> orderByComparator) {
382                    int count = countByUuid(uuid);
383    
384                    if (count == 0) {
385                            return null;
386                    }
387    
388                    List<Address> list = findByUuid(uuid, count - 1, count,
389                                    orderByComparator);
390    
391                    if (!list.isEmpty()) {
392                            return list.get(0);
393                    }
394    
395                    return null;
396            }
397    
398            /**
399             * Returns the addresses before and after the current address in the ordered set where uuid = &#63;.
400             *
401             * @param addressId the primary key of the current address
402             * @param uuid the uuid
403             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
404             * @return the previous, current, and next address
405             * @throws NoSuchAddressException if a address with the primary key could not be found
406             */
407            @Override
408            public Address[] findByUuid_PrevAndNext(long addressId, String uuid,
409                    OrderByComparator<Address> orderByComparator)
410                    throws NoSuchAddressException {
411                    Address address = findByPrimaryKey(addressId);
412    
413                    Session session = null;
414    
415                    try {
416                            session = openSession();
417    
418                            Address[] array = new AddressImpl[3];
419    
420                            array[0] = getByUuid_PrevAndNext(session, address, uuid,
421                                            orderByComparator, true);
422    
423                            array[1] = address;
424    
425                            array[2] = getByUuid_PrevAndNext(session, address, uuid,
426                                            orderByComparator, false);
427    
428                            return array;
429                    }
430                    catch (Exception e) {
431                            throw processException(e);
432                    }
433                    finally {
434                            closeSession(session);
435                    }
436            }
437    
438            protected Address getByUuid_PrevAndNext(Session session, Address address,
439                    String uuid, OrderByComparator<Address> orderByComparator,
440                    boolean previous) {
441                    StringBundler query = null;
442    
443                    if (orderByComparator != null) {
444                            query = new StringBundler(4 +
445                                            (orderByComparator.getOrderByConditionFields().length * 3) +
446                                            (orderByComparator.getOrderByFields().length * 3));
447                    }
448                    else {
449                            query = new StringBundler(3);
450                    }
451    
452                    query.append(_SQL_SELECT_ADDRESS_WHERE);
453    
454                    boolean bindUuid = false;
455    
456                    if (uuid == null) {
457                            query.append(_FINDER_COLUMN_UUID_UUID_1);
458                    }
459                    else if (uuid.equals(StringPool.BLANK)) {
460                            query.append(_FINDER_COLUMN_UUID_UUID_3);
461                    }
462                    else {
463                            bindUuid = true;
464    
465                            query.append(_FINDER_COLUMN_UUID_UUID_2);
466                    }
467    
468                    if (orderByComparator != null) {
469                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
470    
471                            if (orderByConditionFields.length > 0) {
472                                    query.append(WHERE_AND);
473                            }
474    
475                            for (int i = 0; i < orderByConditionFields.length; i++) {
476                                    query.append(_ORDER_BY_ENTITY_ALIAS);
477                                    query.append(orderByConditionFields[i]);
478    
479                                    if ((i + 1) < orderByConditionFields.length) {
480                                            if (orderByComparator.isAscending() ^ previous) {
481                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
482                                            }
483                                            else {
484                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
485                                            }
486                                    }
487                                    else {
488                                            if (orderByComparator.isAscending() ^ previous) {
489                                                    query.append(WHERE_GREATER_THAN);
490                                            }
491                                            else {
492                                                    query.append(WHERE_LESSER_THAN);
493                                            }
494                                    }
495                            }
496    
497                            query.append(ORDER_BY_CLAUSE);
498    
499                            String[] orderByFields = orderByComparator.getOrderByFields();
500    
501                            for (int i = 0; i < orderByFields.length; i++) {
502                                    query.append(_ORDER_BY_ENTITY_ALIAS);
503                                    query.append(orderByFields[i]);
504    
505                                    if ((i + 1) < orderByFields.length) {
506                                            if (orderByComparator.isAscending() ^ previous) {
507                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
508                                            }
509                                            else {
510                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
511                                            }
512                                    }
513                                    else {
514                                            if (orderByComparator.isAscending() ^ previous) {
515                                                    query.append(ORDER_BY_ASC);
516                                            }
517                                            else {
518                                                    query.append(ORDER_BY_DESC);
519                                            }
520                                    }
521                            }
522                    }
523                    else {
524                            query.append(AddressModelImpl.ORDER_BY_JPQL);
525                    }
526    
527                    String sql = query.toString();
528    
529                    Query q = session.createQuery(sql);
530    
531                    q.setFirstResult(0);
532                    q.setMaxResults(2);
533    
534                    QueryPos qPos = QueryPos.getInstance(q);
535    
536                    if (bindUuid) {
537                            qPos.add(uuid);
538                    }
539    
540                    if (orderByComparator != null) {
541                            Object[] values = orderByComparator.getOrderByConditionValues(address);
542    
543                            for (Object value : values) {
544                                    qPos.add(value);
545                            }
546                    }
547    
548                    List<Address> list = q.list();
549    
550                    if (list.size() == 2) {
551                            return list.get(1);
552                    }
553                    else {
554                            return null;
555                    }
556            }
557    
558            /**
559             * Removes all the addresses where uuid = &#63; from the database.
560             *
561             * @param uuid the uuid
562             */
563            @Override
564            public void removeByUuid(String uuid) {
565                    for (Address address : findByUuid(uuid, QueryUtil.ALL_POS,
566                                    QueryUtil.ALL_POS, null)) {
567                            remove(address);
568                    }
569            }
570    
571            /**
572             * Returns the number of addresses where uuid = &#63;.
573             *
574             * @param uuid the uuid
575             * @return the number of matching addresses
576             */
577            @Override
578            public int countByUuid(String uuid) {
579                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID;
580    
581                    Object[] finderArgs = new Object[] { uuid };
582    
583                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
584    
585                    if (count == null) {
586                            StringBundler query = new StringBundler(2);
587    
588                            query.append(_SQL_COUNT_ADDRESS_WHERE);
589    
590                            boolean bindUuid = false;
591    
592                            if (uuid == null) {
593                                    query.append(_FINDER_COLUMN_UUID_UUID_1);
594                            }
595                            else if (uuid.equals(StringPool.BLANK)) {
596                                    query.append(_FINDER_COLUMN_UUID_UUID_3);
597                            }
598                            else {
599                                    bindUuid = true;
600    
601                                    query.append(_FINDER_COLUMN_UUID_UUID_2);
602                            }
603    
604                            String sql = query.toString();
605    
606                            Session session = null;
607    
608                            try {
609                                    session = openSession();
610    
611                                    Query q = session.createQuery(sql);
612    
613                                    QueryPos qPos = QueryPos.getInstance(q);
614    
615                                    if (bindUuid) {
616                                            qPos.add(uuid);
617                                    }
618    
619                                    count = (Long)q.uniqueResult();
620    
621                                    finderCache.putResult(finderPath, finderArgs, count);
622                            }
623                            catch (Exception e) {
624                                    finderCache.removeResult(finderPath, finderArgs);
625    
626                                    throw processException(e);
627                            }
628                            finally {
629                                    closeSession(session);
630                            }
631                    }
632    
633                    return count.intValue();
634            }
635    
636            private static final String _FINDER_COLUMN_UUID_UUID_1 = "address.uuid IS NULL";
637            private static final String _FINDER_COLUMN_UUID_UUID_2 = "address.uuid = ?";
638            private static final String _FINDER_COLUMN_UUID_UUID_3 = "(address.uuid IS NULL OR address.uuid = '')";
639            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
640                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
641                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUuid_C",
642                            new String[] {
643                                    String.class.getName(), Long.class.getName(),
644                                    
645                            Integer.class.getName(), Integer.class.getName(),
646                                    OrderByComparator.class.getName()
647                            });
648            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C =
649                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
650                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
651                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUuid_C",
652                            new String[] { String.class.getName(), Long.class.getName() },
653                            AddressModelImpl.UUID_COLUMN_BITMASK |
654                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
655                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
656            public static final FinderPath FINDER_PATH_COUNT_BY_UUID_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
657                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
658                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUuid_C",
659                            new String[] { String.class.getName(), Long.class.getName() });
660    
661            /**
662             * Returns all the addresses where uuid = &#63; and companyId = &#63;.
663             *
664             * @param uuid the uuid
665             * @param companyId the company ID
666             * @return the matching addresses
667             */
668            @Override
669            public List<Address> findByUuid_C(String uuid, long companyId) {
670                    return findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
671                            QueryUtil.ALL_POS, null);
672            }
673    
674            /**
675             * Returns a range of all the addresses where uuid = &#63; and companyId = &#63;.
676             *
677             * <p>
678             * 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 AddressModelImpl}. 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.
679             * </p>
680             *
681             * @param uuid the uuid
682             * @param companyId the company ID
683             * @param start the lower bound of the range of addresses
684             * @param end the upper bound of the range of addresses (not inclusive)
685             * @return the range of matching addresses
686             */
687            @Override
688            public List<Address> findByUuid_C(String uuid, long companyId, int start,
689                    int end) {
690                    return findByUuid_C(uuid, companyId, start, end, null);
691            }
692    
693            /**
694             * Returns an ordered range of all the addresses where uuid = &#63; and companyId = &#63;.
695             *
696             * <p>
697             * 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 AddressModelImpl}. 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.
698             * </p>
699             *
700             * @param uuid the uuid
701             * @param companyId the company ID
702             * @param start the lower bound of the range of addresses
703             * @param end the upper bound of the range of addresses (not inclusive)
704             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
705             * @return the ordered range of matching addresses
706             */
707            @Override
708            public List<Address> findByUuid_C(String uuid, long companyId, int start,
709                    int end, OrderByComparator<Address> orderByComparator) {
710                    return findByUuid_C(uuid, companyId, start, end, orderByComparator, true);
711            }
712    
713            /**
714             * Returns an ordered range of all the addresses where uuid = &#63; and companyId = &#63;.
715             *
716             * <p>
717             * 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 AddressModelImpl}. 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.
718             * </p>
719             *
720             * @param uuid the uuid
721             * @param companyId the company ID
722             * @param start the lower bound of the range of addresses
723             * @param end the upper bound of the range of addresses (not inclusive)
724             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
725             * @param retrieveFromCache whether to retrieve from the finder cache
726             * @return the ordered range of matching addresses
727             */
728            @Override
729            public List<Address> findByUuid_C(String uuid, long companyId, int start,
730                    int end, OrderByComparator<Address> orderByComparator,
731                    boolean retrieveFromCache) {
732                    boolean pagination = true;
733                    FinderPath finderPath = null;
734                    Object[] finderArgs = null;
735    
736                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
737                                    (orderByComparator == null)) {
738                            pagination = false;
739                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C;
740                            finderArgs = new Object[] { uuid, companyId };
741                    }
742                    else {
743                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_UUID_C;
744                            finderArgs = new Object[] {
745                                            uuid, companyId,
746                                            
747                                            start, end, orderByComparator
748                                    };
749                    }
750    
751                    List<Address> list = null;
752    
753                    if (retrieveFromCache) {
754                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
755                                            this);
756    
757                            if ((list != null) && !list.isEmpty()) {
758                                    for (Address address : list) {
759                                            if (!Objects.equals(uuid, address.getUuid()) ||
760                                                            (companyId != address.getCompanyId())) {
761                                                    list = null;
762    
763                                                    break;
764                                            }
765                                    }
766                            }
767                    }
768    
769                    if (list == null) {
770                            StringBundler query = null;
771    
772                            if (orderByComparator != null) {
773                                    query = new StringBundler(4 +
774                                                    (orderByComparator.getOrderByFields().length * 2));
775                            }
776                            else {
777                                    query = new StringBundler(4);
778                            }
779    
780                            query.append(_SQL_SELECT_ADDRESS_WHERE);
781    
782                            boolean bindUuid = false;
783    
784                            if (uuid == null) {
785                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
786                            }
787                            else if (uuid.equals(StringPool.BLANK)) {
788                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
789                            }
790                            else {
791                                    bindUuid = true;
792    
793                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
794                            }
795    
796                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
797    
798                            if (orderByComparator != null) {
799                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
800                                            orderByComparator);
801                            }
802                            else
803                             if (pagination) {
804                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
805                            }
806    
807                            String sql = query.toString();
808    
809                            Session session = null;
810    
811                            try {
812                                    session = openSession();
813    
814                                    Query q = session.createQuery(sql);
815    
816                                    QueryPos qPos = QueryPos.getInstance(q);
817    
818                                    if (bindUuid) {
819                                            qPos.add(uuid);
820                                    }
821    
822                                    qPos.add(companyId);
823    
824                                    if (!pagination) {
825                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
826                                                            start, end, false);
827    
828                                            Collections.sort(list);
829    
830                                            list = Collections.unmodifiableList(list);
831                                    }
832                                    else {
833                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
834                                                            start, end);
835                                    }
836    
837                                    cacheResult(list);
838    
839                                    finderCache.putResult(finderPath, finderArgs, list);
840                            }
841                            catch (Exception e) {
842                                    finderCache.removeResult(finderPath, finderArgs);
843    
844                                    throw processException(e);
845                            }
846                            finally {
847                                    closeSession(session);
848                            }
849                    }
850    
851                    return list;
852            }
853    
854            /**
855             * Returns the first address in the ordered set where uuid = &#63; and companyId = &#63;.
856             *
857             * @param uuid the uuid
858             * @param companyId the company ID
859             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
860             * @return the first matching address
861             * @throws NoSuchAddressException if a matching address could not be found
862             */
863            @Override
864            public Address findByUuid_C_First(String uuid, long companyId,
865                    OrderByComparator<Address> orderByComparator)
866                    throws NoSuchAddressException {
867                    Address address = fetchByUuid_C_First(uuid, companyId, orderByComparator);
868    
869                    if (address != null) {
870                            return address;
871                    }
872    
873                    StringBundler msg = new StringBundler(6);
874    
875                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
876    
877                    msg.append("uuid=");
878                    msg.append(uuid);
879    
880                    msg.append(", companyId=");
881                    msg.append(companyId);
882    
883                    msg.append(StringPool.CLOSE_CURLY_BRACE);
884    
885                    throw new NoSuchAddressException(msg.toString());
886            }
887    
888            /**
889             * Returns the first address in the ordered set where uuid = &#63; and companyId = &#63;.
890             *
891             * @param uuid the uuid
892             * @param companyId the company ID
893             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
894             * @return the first matching address, or <code>null</code> if a matching address could not be found
895             */
896            @Override
897            public Address fetchByUuid_C_First(String uuid, long companyId,
898                    OrderByComparator<Address> orderByComparator) {
899                    List<Address> list = findByUuid_C(uuid, companyId, 0, 1,
900                                    orderByComparator);
901    
902                    if (!list.isEmpty()) {
903                            return list.get(0);
904                    }
905    
906                    return null;
907            }
908    
909            /**
910             * Returns the last address in the ordered set where uuid = &#63; and companyId = &#63;.
911             *
912             * @param uuid the uuid
913             * @param companyId the company ID
914             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
915             * @return the last matching address
916             * @throws NoSuchAddressException if a matching address could not be found
917             */
918            @Override
919            public Address findByUuid_C_Last(String uuid, long companyId,
920                    OrderByComparator<Address> orderByComparator)
921                    throws NoSuchAddressException {
922                    Address address = fetchByUuid_C_Last(uuid, companyId, orderByComparator);
923    
924                    if (address != null) {
925                            return address;
926                    }
927    
928                    StringBundler msg = new StringBundler(6);
929    
930                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
931    
932                    msg.append("uuid=");
933                    msg.append(uuid);
934    
935                    msg.append(", companyId=");
936                    msg.append(companyId);
937    
938                    msg.append(StringPool.CLOSE_CURLY_BRACE);
939    
940                    throw new NoSuchAddressException(msg.toString());
941            }
942    
943            /**
944             * Returns the last address in the ordered set where uuid = &#63; and companyId = &#63;.
945             *
946             * @param uuid the uuid
947             * @param companyId the company ID
948             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
949             * @return the last matching address, or <code>null</code> if a matching address could not be found
950             */
951            @Override
952            public Address fetchByUuid_C_Last(String uuid, long companyId,
953                    OrderByComparator<Address> orderByComparator) {
954                    int count = countByUuid_C(uuid, companyId);
955    
956                    if (count == 0) {
957                            return null;
958                    }
959    
960                    List<Address> list = findByUuid_C(uuid, companyId, count - 1, count,
961                                    orderByComparator);
962    
963                    if (!list.isEmpty()) {
964                            return list.get(0);
965                    }
966    
967                    return null;
968            }
969    
970            /**
971             * Returns the addresses before and after the current address in the ordered set where uuid = &#63; and companyId = &#63;.
972             *
973             * @param addressId the primary key of the current address
974             * @param uuid the uuid
975             * @param companyId the company ID
976             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
977             * @return the previous, current, and next address
978             * @throws NoSuchAddressException if a address with the primary key could not be found
979             */
980            @Override
981            public Address[] findByUuid_C_PrevAndNext(long addressId, String uuid,
982                    long companyId, OrderByComparator<Address> orderByComparator)
983                    throws NoSuchAddressException {
984                    Address address = findByPrimaryKey(addressId);
985    
986                    Session session = null;
987    
988                    try {
989                            session = openSession();
990    
991                            Address[] array = new AddressImpl[3];
992    
993                            array[0] = getByUuid_C_PrevAndNext(session, address, uuid,
994                                            companyId, orderByComparator, true);
995    
996                            array[1] = address;
997    
998                            array[2] = getByUuid_C_PrevAndNext(session, address, uuid,
999                                            companyId, orderByComparator, false);
1000    
1001                            return array;
1002                    }
1003                    catch (Exception e) {
1004                            throw processException(e);
1005                    }
1006                    finally {
1007                            closeSession(session);
1008                    }
1009            }
1010    
1011            protected Address getByUuid_C_PrevAndNext(Session session, Address address,
1012                    String uuid, long companyId,
1013                    OrderByComparator<Address> orderByComparator, boolean previous) {
1014                    StringBundler query = null;
1015    
1016                    if (orderByComparator != null) {
1017                            query = new StringBundler(5 +
1018                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1019                                            (orderByComparator.getOrderByFields().length * 3));
1020                    }
1021                    else {
1022                            query = new StringBundler(4);
1023                    }
1024    
1025                    query.append(_SQL_SELECT_ADDRESS_WHERE);
1026    
1027                    boolean bindUuid = false;
1028    
1029                    if (uuid == null) {
1030                            query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1031                    }
1032                    else if (uuid.equals(StringPool.BLANK)) {
1033                            query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1034                    }
1035                    else {
1036                            bindUuid = true;
1037    
1038                            query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1039                    }
1040    
1041                    query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1042    
1043                    if (orderByComparator != null) {
1044                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1045    
1046                            if (orderByConditionFields.length > 0) {
1047                                    query.append(WHERE_AND);
1048                            }
1049    
1050                            for (int i = 0; i < orderByConditionFields.length; i++) {
1051                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1052                                    query.append(orderByConditionFields[i]);
1053    
1054                                    if ((i + 1) < orderByConditionFields.length) {
1055                                            if (orderByComparator.isAscending() ^ previous) {
1056                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1057                                            }
1058                                            else {
1059                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1060                                            }
1061                                    }
1062                                    else {
1063                                            if (orderByComparator.isAscending() ^ previous) {
1064                                                    query.append(WHERE_GREATER_THAN);
1065                                            }
1066                                            else {
1067                                                    query.append(WHERE_LESSER_THAN);
1068                                            }
1069                                    }
1070                            }
1071    
1072                            query.append(ORDER_BY_CLAUSE);
1073    
1074                            String[] orderByFields = orderByComparator.getOrderByFields();
1075    
1076                            for (int i = 0; i < orderByFields.length; i++) {
1077                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1078                                    query.append(orderByFields[i]);
1079    
1080                                    if ((i + 1) < orderByFields.length) {
1081                                            if (orderByComparator.isAscending() ^ previous) {
1082                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1083                                            }
1084                                            else {
1085                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1086                                            }
1087                                    }
1088                                    else {
1089                                            if (orderByComparator.isAscending() ^ previous) {
1090                                                    query.append(ORDER_BY_ASC);
1091                                            }
1092                                            else {
1093                                                    query.append(ORDER_BY_DESC);
1094                                            }
1095                                    }
1096                            }
1097                    }
1098                    else {
1099                            query.append(AddressModelImpl.ORDER_BY_JPQL);
1100                    }
1101    
1102                    String sql = query.toString();
1103    
1104                    Query q = session.createQuery(sql);
1105    
1106                    q.setFirstResult(0);
1107                    q.setMaxResults(2);
1108    
1109                    QueryPos qPos = QueryPos.getInstance(q);
1110    
1111                    if (bindUuid) {
1112                            qPos.add(uuid);
1113                    }
1114    
1115                    qPos.add(companyId);
1116    
1117                    if (orderByComparator != null) {
1118                            Object[] values = orderByComparator.getOrderByConditionValues(address);
1119    
1120                            for (Object value : values) {
1121                                    qPos.add(value);
1122                            }
1123                    }
1124    
1125                    List<Address> list = q.list();
1126    
1127                    if (list.size() == 2) {
1128                            return list.get(1);
1129                    }
1130                    else {
1131                            return null;
1132                    }
1133            }
1134    
1135            /**
1136             * Removes all the addresses where uuid = &#63; and companyId = &#63; from the database.
1137             *
1138             * @param uuid the uuid
1139             * @param companyId the company ID
1140             */
1141            @Override
1142            public void removeByUuid_C(String uuid, long companyId) {
1143                    for (Address address : findByUuid_C(uuid, companyId, QueryUtil.ALL_POS,
1144                                    QueryUtil.ALL_POS, null)) {
1145                            remove(address);
1146                    }
1147            }
1148    
1149            /**
1150             * Returns the number of addresses where uuid = &#63; and companyId = &#63;.
1151             *
1152             * @param uuid the uuid
1153             * @param companyId the company ID
1154             * @return the number of matching addresses
1155             */
1156            @Override
1157            public int countByUuid_C(String uuid, long companyId) {
1158                    FinderPath finderPath = FINDER_PATH_COUNT_BY_UUID_C;
1159    
1160                    Object[] finderArgs = new Object[] { uuid, companyId };
1161    
1162                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1163    
1164                    if (count == null) {
1165                            StringBundler query = new StringBundler(3);
1166    
1167                            query.append(_SQL_COUNT_ADDRESS_WHERE);
1168    
1169                            boolean bindUuid = false;
1170    
1171                            if (uuid == null) {
1172                                    query.append(_FINDER_COLUMN_UUID_C_UUID_1);
1173                            }
1174                            else if (uuid.equals(StringPool.BLANK)) {
1175                                    query.append(_FINDER_COLUMN_UUID_C_UUID_3);
1176                            }
1177                            else {
1178                                    bindUuid = true;
1179    
1180                                    query.append(_FINDER_COLUMN_UUID_C_UUID_2);
1181                            }
1182    
1183                            query.append(_FINDER_COLUMN_UUID_C_COMPANYID_2);
1184    
1185                            String sql = query.toString();
1186    
1187                            Session session = null;
1188    
1189                            try {
1190                                    session = openSession();
1191    
1192                                    Query q = session.createQuery(sql);
1193    
1194                                    QueryPos qPos = QueryPos.getInstance(q);
1195    
1196                                    if (bindUuid) {
1197                                            qPos.add(uuid);
1198                                    }
1199    
1200                                    qPos.add(companyId);
1201    
1202                                    count = (Long)q.uniqueResult();
1203    
1204                                    finderCache.putResult(finderPath, finderArgs, count);
1205                            }
1206                            catch (Exception e) {
1207                                    finderCache.removeResult(finderPath, finderArgs);
1208    
1209                                    throw processException(e);
1210                            }
1211                            finally {
1212                                    closeSession(session);
1213                            }
1214                    }
1215    
1216                    return count.intValue();
1217            }
1218    
1219            private static final String _FINDER_COLUMN_UUID_C_UUID_1 = "address.uuid IS NULL AND ";
1220            private static final String _FINDER_COLUMN_UUID_C_UUID_2 = "address.uuid = ? AND ";
1221            private static final String _FINDER_COLUMN_UUID_C_UUID_3 = "(address.uuid IS NULL OR address.uuid = '') AND ";
1222            private static final String _FINDER_COLUMN_UUID_C_COMPANYID_2 = "address.companyId = ?";
1223            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID =
1224                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1225                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
1226                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByCompanyId",
1227                            new String[] {
1228                                    Long.class.getName(),
1229                                    
1230                            Integer.class.getName(), Integer.class.getName(),
1231                                    OrderByComparator.class.getName()
1232                            });
1233            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID =
1234                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1235                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
1236                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByCompanyId",
1237                            new String[] { Long.class.getName() },
1238                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
1239                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
1240            public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1241                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
1242                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByCompanyId",
1243                            new String[] { Long.class.getName() });
1244    
1245            /**
1246             * Returns all the addresses where companyId = &#63;.
1247             *
1248             * @param companyId the company ID
1249             * @return the matching addresses
1250             */
1251            @Override
1252            public List<Address> findByCompanyId(long companyId) {
1253                    return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1254                            null);
1255            }
1256    
1257            /**
1258             * Returns a range of all the addresses where companyId = &#63;.
1259             *
1260             * <p>
1261             * 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 AddressModelImpl}. 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.
1262             * </p>
1263             *
1264             * @param companyId the company ID
1265             * @param start the lower bound of the range of addresses
1266             * @param end the upper bound of the range of addresses (not inclusive)
1267             * @return the range of matching addresses
1268             */
1269            @Override
1270            public List<Address> findByCompanyId(long companyId, int start, int end) {
1271                    return findByCompanyId(companyId, start, end, null);
1272            }
1273    
1274            /**
1275             * Returns an ordered range of all the addresses where companyId = &#63;.
1276             *
1277             * <p>
1278             * 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 AddressModelImpl}. 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.
1279             * </p>
1280             *
1281             * @param companyId the company ID
1282             * @param start the lower bound of the range of addresses
1283             * @param end the upper bound of the range of addresses (not inclusive)
1284             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1285             * @return the ordered range of matching addresses
1286             */
1287            @Override
1288            public List<Address> findByCompanyId(long companyId, int start, int end,
1289                    OrderByComparator<Address> orderByComparator) {
1290                    return findByCompanyId(companyId, start, end, orderByComparator, true);
1291            }
1292    
1293            /**
1294             * Returns an ordered range of all the addresses where companyId = &#63;.
1295             *
1296             * <p>
1297             * 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 AddressModelImpl}. 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.
1298             * </p>
1299             *
1300             * @param companyId the company ID
1301             * @param start the lower bound of the range of addresses
1302             * @param end the upper bound of the range of addresses (not inclusive)
1303             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1304             * @param retrieveFromCache whether to retrieve from the finder cache
1305             * @return the ordered range of matching addresses
1306             */
1307            @Override
1308            public List<Address> findByCompanyId(long companyId, int start, int end,
1309                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
1310                    boolean pagination = true;
1311                    FinderPath finderPath = null;
1312                    Object[] finderArgs = null;
1313    
1314                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1315                                    (orderByComparator == null)) {
1316                            pagination = false;
1317                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID;
1318                            finderArgs = new Object[] { companyId };
1319                    }
1320                    else {
1321                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_COMPANYID;
1322                            finderArgs = new Object[] { companyId, start, end, orderByComparator };
1323                    }
1324    
1325                    List<Address> list = null;
1326    
1327                    if (retrieveFromCache) {
1328                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
1329                                            this);
1330    
1331                            if ((list != null) && !list.isEmpty()) {
1332                                    for (Address address : list) {
1333                                            if ((companyId != address.getCompanyId())) {
1334                                                    list = null;
1335    
1336                                                    break;
1337                                            }
1338                                    }
1339                            }
1340                    }
1341    
1342                    if (list == null) {
1343                            StringBundler query = null;
1344    
1345                            if (orderByComparator != null) {
1346                                    query = new StringBundler(3 +
1347                                                    (orderByComparator.getOrderByFields().length * 2));
1348                            }
1349                            else {
1350                                    query = new StringBundler(3);
1351                            }
1352    
1353                            query.append(_SQL_SELECT_ADDRESS_WHERE);
1354    
1355                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1356    
1357                            if (orderByComparator != null) {
1358                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1359                                            orderByComparator);
1360                            }
1361                            else
1362                             if (pagination) {
1363                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
1364                            }
1365    
1366                            String sql = query.toString();
1367    
1368                            Session session = null;
1369    
1370                            try {
1371                                    session = openSession();
1372    
1373                                    Query q = session.createQuery(sql);
1374    
1375                                    QueryPos qPos = QueryPos.getInstance(q);
1376    
1377                                    qPos.add(companyId);
1378    
1379                                    if (!pagination) {
1380                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
1381                                                            start, end, false);
1382    
1383                                            Collections.sort(list);
1384    
1385                                            list = Collections.unmodifiableList(list);
1386                                    }
1387                                    else {
1388                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
1389                                                            start, end);
1390                                    }
1391    
1392                                    cacheResult(list);
1393    
1394                                    finderCache.putResult(finderPath, finderArgs, list);
1395                            }
1396                            catch (Exception e) {
1397                                    finderCache.removeResult(finderPath, finderArgs);
1398    
1399                                    throw processException(e);
1400                            }
1401                            finally {
1402                                    closeSession(session);
1403                            }
1404                    }
1405    
1406                    return list;
1407            }
1408    
1409            /**
1410             * Returns the first address in the ordered set where companyId = &#63;.
1411             *
1412             * @param companyId the company ID
1413             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1414             * @return the first matching address
1415             * @throws NoSuchAddressException if a matching address could not be found
1416             */
1417            @Override
1418            public Address findByCompanyId_First(long companyId,
1419                    OrderByComparator<Address> orderByComparator)
1420                    throws NoSuchAddressException {
1421                    Address address = fetchByCompanyId_First(companyId, orderByComparator);
1422    
1423                    if (address != null) {
1424                            return address;
1425                    }
1426    
1427                    StringBundler msg = new StringBundler(4);
1428    
1429                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1430    
1431                    msg.append("companyId=");
1432                    msg.append(companyId);
1433    
1434                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1435    
1436                    throw new NoSuchAddressException(msg.toString());
1437            }
1438    
1439            /**
1440             * Returns the first address in the ordered set where companyId = &#63;.
1441             *
1442             * @param companyId the company ID
1443             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1444             * @return the first matching address, or <code>null</code> if a matching address could not be found
1445             */
1446            @Override
1447            public Address fetchByCompanyId_First(long companyId,
1448                    OrderByComparator<Address> orderByComparator) {
1449                    List<Address> list = findByCompanyId(companyId, 0, 1, orderByComparator);
1450    
1451                    if (!list.isEmpty()) {
1452                            return list.get(0);
1453                    }
1454    
1455                    return null;
1456            }
1457    
1458            /**
1459             * Returns the last address in the ordered set where companyId = &#63;.
1460             *
1461             * @param companyId the company ID
1462             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1463             * @return the last matching address
1464             * @throws NoSuchAddressException if a matching address could not be found
1465             */
1466            @Override
1467            public Address findByCompanyId_Last(long companyId,
1468                    OrderByComparator<Address> orderByComparator)
1469                    throws NoSuchAddressException {
1470                    Address address = fetchByCompanyId_Last(companyId, orderByComparator);
1471    
1472                    if (address != null) {
1473                            return address;
1474                    }
1475    
1476                    StringBundler msg = new StringBundler(4);
1477    
1478                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1479    
1480                    msg.append("companyId=");
1481                    msg.append(companyId);
1482    
1483                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1484    
1485                    throw new NoSuchAddressException(msg.toString());
1486            }
1487    
1488            /**
1489             * Returns the last address in the ordered set where companyId = &#63;.
1490             *
1491             * @param companyId the company ID
1492             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1493             * @return the last matching address, or <code>null</code> if a matching address could not be found
1494             */
1495            @Override
1496            public Address fetchByCompanyId_Last(long companyId,
1497                    OrderByComparator<Address> orderByComparator) {
1498                    int count = countByCompanyId(companyId);
1499    
1500                    if (count == 0) {
1501                            return null;
1502                    }
1503    
1504                    List<Address> list = findByCompanyId(companyId, count - 1, count,
1505                                    orderByComparator);
1506    
1507                    if (!list.isEmpty()) {
1508                            return list.get(0);
1509                    }
1510    
1511                    return null;
1512            }
1513    
1514            /**
1515             * Returns the addresses before and after the current address in the ordered set where companyId = &#63;.
1516             *
1517             * @param addressId the primary key of the current address
1518             * @param companyId the company ID
1519             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1520             * @return the previous, current, and next address
1521             * @throws NoSuchAddressException if a address with the primary key could not be found
1522             */
1523            @Override
1524            public Address[] findByCompanyId_PrevAndNext(long addressId,
1525                    long companyId, OrderByComparator<Address> orderByComparator)
1526                    throws NoSuchAddressException {
1527                    Address address = findByPrimaryKey(addressId);
1528    
1529                    Session session = null;
1530    
1531                    try {
1532                            session = openSession();
1533    
1534                            Address[] array = new AddressImpl[3];
1535    
1536                            array[0] = getByCompanyId_PrevAndNext(session, address, companyId,
1537                                            orderByComparator, true);
1538    
1539                            array[1] = address;
1540    
1541                            array[2] = getByCompanyId_PrevAndNext(session, address, companyId,
1542                                            orderByComparator, false);
1543    
1544                            return array;
1545                    }
1546                    catch (Exception e) {
1547                            throw processException(e);
1548                    }
1549                    finally {
1550                            closeSession(session);
1551                    }
1552            }
1553    
1554            protected Address getByCompanyId_PrevAndNext(Session session,
1555                    Address address, long companyId,
1556                    OrderByComparator<Address> orderByComparator, boolean previous) {
1557                    StringBundler query = null;
1558    
1559                    if (orderByComparator != null) {
1560                            query = new StringBundler(4 +
1561                                            (orderByComparator.getOrderByConditionFields().length * 3) +
1562                                            (orderByComparator.getOrderByFields().length * 3));
1563                    }
1564                    else {
1565                            query = new StringBundler(3);
1566                    }
1567    
1568                    query.append(_SQL_SELECT_ADDRESS_WHERE);
1569    
1570                    query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1571    
1572                    if (orderByComparator != null) {
1573                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1574    
1575                            if (orderByConditionFields.length > 0) {
1576                                    query.append(WHERE_AND);
1577                            }
1578    
1579                            for (int i = 0; i < orderByConditionFields.length; i++) {
1580                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1581                                    query.append(orderByConditionFields[i]);
1582    
1583                                    if ((i + 1) < orderByConditionFields.length) {
1584                                            if (orderByComparator.isAscending() ^ previous) {
1585                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1586                                            }
1587                                            else {
1588                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1589                                            }
1590                                    }
1591                                    else {
1592                                            if (orderByComparator.isAscending() ^ previous) {
1593                                                    query.append(WHERE_GREATER_THAN);
1594                                            }
1595                                            else {
1596                                                    query.append(WHERE_LESSER_THAN);
1597                                            }
1598                                    }
1599                            }
1600    
1601                            query.append(ORDER_BY_CLAUSE);
1602    
1603                            String[] orderByFields = orderByComparator.getOrderByFields();
1604    
1605                            for (int i = 0; i < orderByFields.length; i++) {
1606                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1607                                    query.append(orderByFields[i]);
1608    
1609                                    if ((i + 1) < orderByFields.length) {
1610                                            if (orderByComparator.isAscending() ^ previous) {
1611                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1612                                            }
1613                                            else {
1614                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1615                                            }
1616                                    }
1617                                    else {
1618                                            if (orderByComparator.isAscending() ^ previous) {
1619                                                    query.append(ORDER_BY_ASC);
1620                                            }
1621                                            else {
1622                                                    query.append(ORDER_BY_DESC);
1623                                            }
1624                                    }
1625                            }
1626                    }
1627                    else {
1628                            query.append(AddressModelImpl.ORDER_BY_JPQL);
1629                    }
1630    
1631                    String sql = query.toString();
1632    
1633                    Query q = session.createQuery(sql);
1634    
1635                    q.setFirstResult(0);
1636                    q.setMaxResults(2);
1637    
1638                    QueryPos qPos = QueryPos.getInstance(q);
1639    
1640                    qPos.add(companyId);
1641    
1642                    if (orderByComparator != null) {
1643                            Object[] values = orderByComparator.getOrderByConditionValues(address);
1644    
1645                            for (Object value : values) {
1646                                    qPos.add(value);
1647                            }
1648                    }
1649    
1650                    List<Address> list = q.list();
1651    
1652                    if (list.size() == 2) {
1653                            return list.get(1);
1654                    }
1655                    else {
1656                            return null;
1657                    }
1658            }
1659    
1660            /**
1661             * Removes all the addresses where companyId = &#63; from the database.
1662             *
1663             * @param companyId the company ID
1664             */
1665            @Override
1666            public void removeByCompanyId(long companyId) {
1667                    for (Address address : findByCompanyId(companyId, QueryUtil.ALL_POS,
1668                                    QueryUtil.ALL_POS, null)) {
1669                            remove(address);
1670                    }
1671            }
1672    
1673            /**
1674             * Returns the number of addresses where companyId = &#63;.
1675             *
1676             * @param companyId the company ID
1677             * @return the number of matching addresses
1678             */
1679            @Override
1680            public int countByCompanyId(long companyId) {
1681                    FinderPath finderPath = FINDER_PATH_COUNT_BY_COMPANYID;
1682    
1683                    Object[] finderArgs = new Object[] { companyId };
1684    
1685                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1686    
1687                    if (count == null) {
1688                            StringBundler query = new StringBundler(2);
1689    
1690                            query.append(_SQL_COUNT_ADDRESS_WHERE);
1691    
1692                            query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1693    
1694                            String sql = query.toString();
1695    
1696                            Session session = null;
1697    
1698                            try {
1699                                    session = openSession();
1700    
1701                                    Query q = session.createQuery(sql);
1702    
1703                                    QueryPos qPos = QueryPos.getInstance(q);
1704    
1705                                    qPos.add(companyId);
1706    
1707                                    count = (Long)q.uniqueResult();
1708    
1709                                    finderCache.putResult(finderPath, finderArgs, count);
1710                            }
1711                            catch (Exception e) {
1712                                    finderCache.removeResult(finderPath, finderArgs);
1713    
1714                                    throw processException(e);
1715                            }
1716                            finally {
1717                                    closeSession(session);
1718                            }
1719                    }
1720    
1721                    return count.intValue();
1722            }
1723    
1724            private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "address.companyId = ?";
1725            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1726                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
1727                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByUserId",
1728                            new String[] {
1729                                    Long.class.getName(),
1730                                    
1731                            Integer.class.getName(), Integer.class.getName(),
1732                                    OrderByComparator.class.getName()
1733                            });
1734            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
1735                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1736                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
1737                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByUserId",
1738                            new String[] { Long.class.getName() },
1739                            AddressModelImpl.USERID_COLUMN_BITMASK |
1740                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
1741            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
1742                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
1743                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
1744                            new String[] { Long.class.getName() });
1745    
1746            /**
1747             * Returns all the addresses where userId = &#63;.
1748             *
1749             * @param userId the user ID
1750             * @return the matching addresses
1751             */
1752            @Override
1753            public List<Address> findByUserId(long userId) {
1754                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1755            }
1756    
1757            /**
1758             * Returns a range of all the addresses where userId = &#63;.
1759             *
1760             * <p>
1761             * 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 AddressModelImpl}. 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.
1762             * </p>
1763             *
1764             * @param userId the user ID
1765             * @param start the lower bound of the range of addresses
1766             * @param end the upper bound of the range of addresses (not inclusive)
1767             * @return the range of matching addresses
1768             */
1769            @Override
1770            public List<Address> findByUserId(long userId, int start, int end) {
1771                    return findByUserId(userId, start, end, null);
1772            }
1773    
1774            /**
1775             * Returns an ordered range of all the addresses where userId = &#63;.
1776             *
1777             * <p>
1778             * 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 AddressModelImpl}. 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.
1779             * </p>
1780             *
1781             * @param userId the user ID
1782             * @param start the lower bound of the range of addresses
1783             * @param end the upper bound of the range of addresses (not inclusive)
1784             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1785             * @return the ordered range of matching addresses
1786             */
1787            @Override
1788            public List<Address> findByUserId(long userId, int start, int end,
1789                    OrderByComparator<Address> orderByComparator) {
1790                    return findByUserId(userId, start, end, orderByComparator, true);
1791            }
1792    
1793            /**
1794             * Returns an ordered range of all the addresses where userId = &#63;.
1795             *
1796             * <p>
1797             * 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 AddressModelImpl}. 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.
1798             * </p>
1799             *
1800             * @param userId the user ID
1801             * @param start the lower bound of the range of addresses
1802             * @param end the upper bound of the range of addresses (not inclusive)
1803             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1804             * @param retrieveFromCache whether to retrieve from the finder cache
1805             * @return the ordered range of matching addresses
1806             */
1807            @Override
1808            public List<Address> findByUserId(long userId, int start, int end,
1809                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
1810                    boolean pagination = true;
1811                    FinderPath finderPath = null;
1812                    Object[] finderArgs = null;
1813    
1814                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1815                                    (orderByComparator == null)) {
1816                            pagination = false;
1817                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
1818                            finderArgs = new Object[] { userId };
1819                    }
1820                    else {
1821                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
1822                            finderArgs = new Object[] { userId, start, end, orderByComparator };
1823                    }
1824    
1825                    List<Address> list = null;
1826    
1827                    if (retrieveFromCache) {
1828                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
1829                                            this);
1830    
1831                            if ((list != null) && !list.isEmpty()) {
1832                                    for (Address address : list) {
1833                                            if ((userId != address.getUserId())) {
1834                                                    list = null;
1835    
1836                                                    break;
1837                                            }
1838                                    }
1839                            }
1840                    }
1841    
1842                    if (list == null) {
1843                            StringBundler query = null;
1844    
1845                            if (orderByComparator != null) {
1846                                    query = new StringBundler(3 +
1847                                                    (orderByComparator.getOrderByFields().length * 2));
1848                            }
1849                            else {
1850                                    query = new StringBundler(3);
1851                            }
1852    
1853                            query.append(_SQL_SELECT_ADDRESS_WHERE);
1854    
1855                            query.append(_FINDER_COLUMN_USERID_USERID_2);
1856    
1857                            if (orderByComparator != null) {
1858                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1859                                            orderByComparator);
1860                            }
1861                            else
1862                             if (pagination) {
1863                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
1864                            }
1865    
1866                            String sql = query.toString();
1867    
1868                            Session session = null;
1869    
1870                            try {
1871                                    session = openSession();
1872    
1873                                    Query q = session.createQuery(sql);
1874    
1875                                    QueryPos qPos = QueryPos.getInstance(q);
1876    
1877                                    qPos.add(userId);
1878    
1879                                    if (!pagination) {
1880                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
1881                                                            start, end, false);
1882    
1883                                            Collections.sort(list);
1884    
1885                                            list = Collections.unmodifiableList(list);
1886                                    }
1887                                    else {
1888                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
1889                                                            start, end);
1890                                    }
1891    
1892                                    cacheResult(list);
1893    
1894                                    finderCache.putResult(finderPath, finderArgs, list);
1895                            }
1896                            catch (Exception e) {
1897                                    finderCache.removeResult(finderPath, finderArgs);
1898    
1899                                    throw processException(e);
1900                            }
1901                            finally {
1902                                    closeSession(session);
1903                            }
1904                    }
1905    
1906                    return list;
1907            }
1908    
1909            /**
1910             * Returns the first address in the ordered set where userId = &#63;.
1911             *
1912             * @param userId the user ID
1913             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1914             * @return the first matching address
1915             * @throws NoSuchAddressException if a matching address could not be found
1916             */
1917            @Override
1918            public Address findByUserId_First(long userId,
1919                    OrderByComparator<Address> orderByComparator)
1920                    throws NoSuchAddressException {
1921                    Address address = fetchByUserId_First(userId, orderByComparator);
1922    
1923                    if (address != null) {
1924                            return address;
1925                    }
1926    
1927                    StringBundler msg = new StringBundler(4);
1928    
1929                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1930    
1931                    msg.append("userId=");
1932                    msg.append(userId);
1933    
1934                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1935    
1936                    throw new NoSuchAddressException(msg.toString());
1937            }
1938    
1939            /**
1940             * Returns the first address in the ordered set where userId = &#63;.
1941             *
1942             * @param userId the user ID
1943             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1944             * @return the first matching address, or <code>null</code> if a matching address could not be found
1945             */
1946            @Override
1947            public Address fetchByUserId_First(long userId,
1948                    OrderByComparator<Address> orderByComparator) {
1949                    List<Address> list = findByUserId(userId, 0, 1, orderByComparator);
1950    
1951                    if (!list.isEmpty()) {
1952                            return list.get(0);
1953                    }
1954    
1955                    return null;
1956            }
1957    
1958            /**
1959             * Returns the last address in the ordered set where userId = &#63;.
1960             *
1961             * @param userId the user ID
1962             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1963             * @return the last matching address
1964             * @throws NoSuchAddressException if a matching address could not be found
1965             */
1966            @Override
1967            public Address findByUserId_Last(long userId,
1968                    OrderByComparator<Address> orderByComparator)
1969                    throws NoSuchAddressException {
1970                    Address address = fetchByUserId_Last(userId, orderByComparator);
1971    
1972                    if (address != null) {
1973                            return address;
1974                    }
1975    
1976                    StringBundler msg = new StringBundler(4);
1977    
1978                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1979    
1980                    msg.append("userId=");
1981                    msg.append(userId);
1982    
1983                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1984    
1985                    throw new NoSuchAddressException(msg.toString());
1986            }
1987    
1988            /**
1989             * Returns the last address in the ordered set where userId = &#63;.
1990             *
1991             * @param userId the user ID
1992             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1993             * @return the last matching address, or <code>null</code> if a matching address could not be found
1994             */
1995            @Override
1996            public Address fetchByUserId_Last(long userId,
1997                    OrderByComparator<Address> orderByComparator) {
1998                    int count = countByUserId(userId);
1999    
2000                    if (count == 0) {
2001                            return null;
2002                    }
2003    
2004                    List<Address> list = findByUserId(userId, count - 1, count,
2005                                    orderByComparator);
2006    
2007                    if (!list.isEmpty()) {
2008                            return list.get(0);
2009                    }
2010    
2011                    return null;
2012            }
2013    
2014            /**
2015             * Returns the addresses before and after the current address in the ordered set where userId = &#63;.
2016             *
2017             * @param addressId the primary key of the current address
2018             * @param userId the user ID
2019             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2020             * @return the previous, current, and next address
2021             * @throws NoSuchAddressException if a address with the primary key could not be found
2022             */
2023            @Override
2024            public Address[] findByUserId_PrevAndNext(long addressId, long userId,
2025                    OrderByComparator<Address> orderByComparator)
2026                    throws NoSuchAddressException {
2027                    Address address = findByPrimaryKey(addressId);
2028    
2029                    Session session = null;
2030    
2031                    try {
2032                            session = openSession();
2033    
2034                            Address[] array = new AddressImpl[3];
2035    
2036                            array[0] = getByUserId_PrevAndNext(session, address, userId,
2037                                            orderByComparator, true);
2038    
2039                            array[1] = address;
2040    
2041                            array[2] = getByUserId_PrevAndNext(session, address, userId,
2042                                            orderByComparator, false);
2043    
2044                            return array;
2045                    }
2046                    catch (Exception e) {
2047                            throw processException(e);
2048                    }
2049                    finally {
2050                            closeSession(session);
2051                    }
2052            }
2053    
2054            protected Address getByUserId_PrevAndNext(Session session, Address address,
2055                    long userId, OrderByComparator<Address> orderByComparator,
2056                    boolean previous) {
2057                    StringBundler query = null;
2058    
2059                    if (orderByComparator != null) {
2060                            query = new StringBundler(4 +
2061                                            (orderByComparator.getOrderByConditionFields().length * 3) +
2062                                            (orderByComparator.getOrderByFields().length * 3));
2063                    }
2064                    else {
2065                            query = new StringBundler(3);
2066                    }
2067    
2068                    query.append(_SQL_SELECT_ADDRESS_WHERE);
2069    
2070                    query.append(_FINDER_COLUMN_USERID_USERID_2);
2071    
2072                    if (orderByComparator != null) {
2073                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2074    
2075                            if (orderByConditionFields.length > 0) {
2076                                    query.append(WHERE_AND);
2077                            }
2078    
2079                            for (int i = 0; i < orderByConditionFields.length; i++) {
2080                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2081                                    query.append(orderByConditionFields[i]);
2082    
2083                                    if ((i + 1) < orderByConditionFields.length) {
2084                                            if (orderByComparator.isAscending() ^ previous) {
2085                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2086                                            }
2087                                            else {
2088                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2089                                            }
2090                                    }
2091                                    else {
2092                                            if (orderByComparator.isAscending() ^ previous) {
2093                                                    query.append(WHERE_GREATER_THAN);
2094                                            }
2095                                            else {
2096                                                    query.append(WHERE_LESSER_THAN);
2097                                            }
2098                                    }
2099                            }
2100    
2101                            query.append(ORDER_BY_CLAUSE);
2102    
2103                            String[] orderByFields = orderByComparator.getOrderByFields();
2104    
2105                            for (int i = 0; i < orderByFields.length; i++) {
2106                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2107                                    query.append(orderByFields[i]);
2108    
2109                                    if ((i + 1) < orderByFields.length) {
2110                                            if (orderByComparator.isAscending() ^ previous) {
2111                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2112                                            }
2113                                            else {
2114                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2115                                            }
2116                                    }
2117                                    else {
2118                                            if (orderByComparator.isAscending() ^ previous) {
2119                                                    query.append(ORDER_BY_ASC);
2120                                            }
2121                                            else {
2122                                                    query.append(ORDER_BY_DESC);
2123                                            }
2124                                    }
2125                            }
2126                    }
2127                    else {
2128                            query.append(AddressModelImpl.ORDER_BY_JPQL);
2129                    }
2130    
2131                    String sql = query.toString();
2132    
2133                    Query q = session.createQuery(sql);
2134    
2135                    q.setFirstResult(0);
2136                    q.setMaxResults(2);
2137    
2138                    QueryPos qPos = QueryPos.getInstance(q);
2139    
2140                    qPos.add(userId);
2141    
2142                    if (orderByComparator != null) {
2143                            Object[] values = orderByComparator.getOrderByConditionValues(address);
2144    
2145                            for (Object value : values) {
2146                                    qPos.add(value);
2147                            }
2148                    }
2149    
2150                    List<Address> list = q.list();
2151    
2152                    if (list.size() == 2) {
2153                            return list.get(1);
2154                    }
2155                    else {
2156                            return null;
2157                    }
2158            }
2159    
2160            /**
2161             * Removes all the addresses where userId = &#63; from the database.
2162             *
2163             * @param userId the user ID
2164             */
2165            @Override
2166            public void removeByUserId(long userId) {
2167                    for (Address address : findByUserId(userId, QueryUtil.ALL_POS,
2168                                    QueryUtil.ALL_POS, null)) {
2169                            remove(address);
2170                    }
2171            }
2172    
2173            /**
2174             * Returns the number of addresses where userId = &#63;.
2175             *
2176             * @param userId the user ID
2177             * @return the number of matching addresses
2178             */
2179            @Override
2180            public int countByUserId(long userId) {
2181                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
2182    
2183                    Object[] finderArgs = new Object[] { userId };
2184    
2185                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2186    
2187                    if (count == null) {
2188                            StringBundler query = new StringBundler(2);
2189    
2190                            query.append(_SQL_COUNT_ADDRESS_WHERE);
2191    
2192                            query.append(_FINDER_COLUMN_USERID_USERID_2);
2193    
2194                            String sql = query.toString();
2195    
2196                            Session session = null;
2197    
2198                            try {
2199                                    session = openSession();
2200    
2201                                    Query q = session.createQuery(sql);
2202    
2203                                    QueryPos qPos = QueryPos.getInstance(q);
2204    
2205                                    qPos.add(userId);
2206    
2207                                    count = (Long)q.uniqueResult();
2208    
2209                                    finderCache.putResult(finderPath, finderArgs, count);
2210                            }
2211                            catch (Exception e) {
2212                                    finderCache.removeResult(finderPath, finderArgs);
2213    
2214                                    throw processException(e);
2215                            }
2216                            finally {
2217                                    closeSession(session);
2218                            }
2219                    }
2220    
2221                    return count.intValue();
2222            }
2223    
2224            private static final String _FINDER_COLUMN_USERID_USERID_2 = "address.userId = ?";
2225            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2226                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
2227                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C",
2228                            new String[] {
2229                                    Long.class.getName(), Long.class.getName(),
2230                                    
2231                            Integer.class.getName(), Integer.class.getName(),
2232                                    OrderByComparator.class.getName()
2233                            });
2234            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2235                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
2236                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C",
2237                            new String[] { Long.class.getName(), Long.class.getName() },
2238                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
2239                            AddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
2240                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
2241            public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2242                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
2243                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C",
2244                            new String[] { Long.class.getName(), Long.class.getName() });
2245    
2246            /**
2247             * Returns all the addresses where companyId = &#63; and classNameId = &#63;.
2248             *
2249             * @param companyId the company ID
2250             * @param classNameId the class name ID
2251             * @return the matching addresses
2252             */
2253            @Override
2254            public List<Address> findByC_C(long companyId, long classNameId) {
2255                    return findByC_C(companyId, classNameId, QueryUtil.ALL_POS,
2256                            QueryUtil.ALL_POS, null);
2257            }
2258    
2259            /**
2260             * Returns a range of all the addresses where companyId = &#63; and classNameId = &#63;.
2261             *
2262             * <p>
2263             * 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 AddressModelImpl}. 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.
2264             * </p>
2265             *
2266             * @param companyId the company ID
2267             * @param classNameId the class name ID
2268             * @param start the lower bound of the range of addresses
2269             * @param end the upper bound of the range of addresses (not inclusive)
2270             * @return the range of matching addresses
2271             */
2272            @Override
2273            public List<Address> findByC_C(long companyId, long classNameId, int start,
2274                    int end) {
2275                    return findByC_C(companyId, classNameId, start, end, null);
2276            }
2277    
2278            /**
2279             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63;.
2280             *
2281             * <p>
2282             * 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 AddressModelImpl}. 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.
2283             * </p>
2284             *
2285             * @param companyId the company ID
2286             * @param classNameId the class name ID
2287             * @param start the lower bound of the range of addresses
2288             * @param end the upper bound of the range of addresses (not inclusive)
2289             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2290             * @return the ordered range of matching addresses
2291             */
2292            @Override
2293            public List<Address> findByC_C(long companyId, long classNameId, int start,
2294                    int end, OrderByComparator<Address> orderByComparator) {
2295                    return findByC_C(companyId, classNameId, start, end, orderByComparator,
2296                            true);
2297            }
2298    
2299            /**
2300             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63;.
2301             *
2302             * <p>
2303             * 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 AddressModelImpl}. 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.
2304             * </p>
2305             *
2306             * @param companyId the company ID
2307             * @param classNameId the class name ID
2308             * @param start the lower bound of the range of addresses
2309             * @param end the upper bound of the range of addresses (not inclusive)
2310             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2311             * @param retrieveFromCache whether to retrieve from the finder cache
2312             * @return the ordered range of matching addresses
2313             */
2314            @Override
2315            public List<Address> findByC_C(long companyId, long classNameId, int start,
2316                    int end, OrderByComparator<Address> orderByComparator,
2317                    boolean retrieveFromCache) {
2318                    boolean pagination = true;
2319                    FinderPath finderPath = null;
2320                    Object[] finderArgs = null;
2321    
2322                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2323                                    (orderByComparator == null)) {
2324                            pagination = false;
2325                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C;
2326                            finderArgs = new Object[] { companyId, classNameId };
2327                    }
2328                    else {
2329                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C;
2330                            finderArgs = new Object[] {
2331                                            companyId, classNameId,
2332                                            
2333                                            start, end, orderByComparator
2334                                    };
2335                    }
2336    
2337                    List<Address> list = null;
2338    
2339                    if (retrieveFromCache) {
2340                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
2341                                            this);
2342    
2343                            if ((list != null) && !list.isEmpty()) {
2344                                    for (Address address : list) {
2345                                            if ((companyId != address.getCompanyId()) ||
2346                                                            (classNameId != address.getClassNameId())) {
2347                                                    list = null;
2348    
2349                                                    break;
2350                                            }
2351                                    }
2352                            }
2353                    }
2354    
2355                    if (list == null) {
2356                            StringBundler query = null;
2357    
2358                            if (orderByComparator != null) {
2359                                    query = new StringBundler(4 +
2360                                                    (orderByComparator.getOrderByFields().length * 2));
2361                            }
2362                            else {
2363                                    query = new StringBundler(4);
2364                            }
2365    
2366                            query.append(_SQL_SELECT_ADDRESS_WHERE);
2367    
2368                            query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2369    
2370                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2371    
2372                            if (orderByComparator != null) {
2373                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2374                                            orderByComparator);
2375                            }
2376                            else
2377                             if (pagination) {
2378                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
2379                            }
2380    
2381                            String sql = query.toString();
2382    
2383                            Session session = null;
2384    
2385                            try {
2386                                    session = openSession();
2387    
2388                                    Query q = session.createQuery(sql);
2389    
2390                                    QueryPos qPos = QueryPos.getInstance(q);
2391    
2392                                    qPos.add(companyId);
2393    
2394                                    qPos.add(classNameId);
2395    
2396                                    if (!pagination) {
2397                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
2398                                                            start, end, false);
2399    
2400                                            Collections.sort(list);
2401    
2402                                            list = Collections.unmodifiableList(list);
2403                                    }
2404                                    else {
2405                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
2406                                                            start, end);
2407                                    }
2408    
2409                                    cacheResult(list);
2410    
2411                                    finderCache.putResult(finderPath, finderArgs, list);
2412                            }
2413                            catch (Exception e) {
2414                                    finderCache.removeResult(finderPath, finderArgs);
2415    
2416                                    throw processException(e);
2417                            }
2418                            finally {
2419                                    closeSession(session);
2420                            }
2421                    }
2422    
2423                    return list;
2424            }
2425    
2426            /**
2427             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63;.
2428             *
2429             * @param companyId the company ID
2430             * @param classNameId the class name ID
2431             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2432             * @return the first matching address
2433             * @throws NoSuchAddressException if a matching address could not be found
2434             */
2435            @Override
2436            public Address findByC_C_First(long companyId, long classNameId,
2437                    OrderByComparator<Address> orderByComparator)
2438                    throws NoSuchAddressException {
2439                    Address address = fetchByC_C_First(companyId, classNameId,
2440                                    orderByComparator);
2441    
2442                    if (address != null) {
2443                            return address;
2444                    }
2445    
2446                    StringBundler msg = new StringBundler(6);
2447    
2448                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2449    
2450                    msg.append("companyId=");
2451                    msg.append(companyId);
2452    
2453                    msg.append(", classNameId=");
2454                    msg.append(classNameId);
2455    
2456                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2457    
2458                    throw new NoSuchAddressException(msg.toString());
2459            }
2460    
2461            /**
2462             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63;.
2463             *
2464             * @param companyId the company ID
2465             * @param classNameId the class name ID
2466             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2467             * @return the first matching address, or <code>null</code> if a matching address could not be found
2468             */
2469            @Override
2470            public Address fetchByC_C_First(long companyId, long classNameId,
2471                    OrderByComparator<Address> orderByComparator) {
2472                    List<Address> list = findByC_C(companyId, classNameId, 0, 1,
2473                                    orderByComparator);
2474    
2475                    if (!list.isEmpty()) {
2476                            return list.get(0);
2477                    }
2478    
2479                    return null;
2480            }
2481    
2482            /**
2483             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63;.
2484             *
2485             * @param companyId the company ID
2486             * @param classNameId the class name ID
2487             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2488             * @return the last matching address
2489             * @throws NoSuchAddressException if a matching address could not be found
2490             */
2491            @Override
2492            public Address findByC_C_Last(long companyId, long classNameId,
2493                    OrderByComparator<Address> orderByComparator)
2494                    throws NoSuchAddressException {
2495                    Address address = fetchByC_C_Last(companyId, classNameId,
2496                                    orderByComparator);
2497    
2498                    if (address != null) {
2499                            return address;
2500                    }
2501    
2502                    StringBundler msg = new StringBundler(6);
2503    
2504                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2505    
2506                    msg.append("companyId=");
2507                    msg.append(companyId);
2508    
2509                    msg.append(", classNameId=");
2510                    msg.append(classNameId);
2511    
2512                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2513    
2514                    throw new NoSuchAddressException(msg.toString());
2515            }
2516    
2517            /**
2518             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63;.
2519             *
2520             * @param companyId the company ID
2521             * @param classNameId the class name ID
2522             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2523             * @return the last matching address, or <code>null</code> if a matching address could not be found
2524             */
2525            @Override
2526            public Address fetchByC_C_Last(long companyId, long classNameId,
2527                    OrderByComparator<Address> orderByComparator) {
2528                    int count = countByC_C(companyId, classNameId);
2529    
2530                    if (count == 0) {
2531                            return null;
2532                    }
2533    
2534                    List<Address> list = findByC_C(companyId, classNameId, count - 1,
2535                                    count, orderByComparator);
2536    
2537                    if (!list.isEmpty()) {
2538                            return list.get(0);
2539                    }
2540    
2541                    return null;
2542            }
2543    
2544            /**
2545             * Returns the addresses before and after the current address in the ordered set where companyId = &#63; and classNameId = &#63;.
2546             *
2547             * @param addressId the primary key of the current address
2548             * @param companyId the company ID
2549             * @param classNameId the class name ID
2550             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2551             * @return the previous, current, and next address
2552             * @throws NoSuchAddressException if a address with the primary key could not be found
2553             */
2554            @Override
2555            public Address[] findByC_C_PrevAndNext(long addressId, long companyId,
2556                    long classNameId, OrderByComparator<Address> orderByComparator)
2557                    throws NoSuchAddressException {
2558                    Address address = findByPrimaryKey(addressId);
2559    
2560                    Session session = null;
2561    
2562                    try {
2563                            session = openSession();
2564    
2565                            Address[] array = new AddressImpl[3];
2566    
2567                            array[0] = getByC_C_PrevAndNext(session, address, companyId,
2568                                            classNameId, orderByComparator, true);
2569    
2570                            array[1] = address;
2571    
2572                            array[2] = getByC_C_PrevAndNext(session, address, companyId,
2573                                            classNameId, orderByComparator, false);
2574    
2575                            return array;
2576                    }
2577                    catch (Exception e) {
2578                            throw processException(e);
2579                    }
2580                    finally {
2581                            closeSession(session);
2582                    }
2583            }
2584    
2585            protected Address getByC_C_PrevAndNext(Session session, Address address,
2586                    long companyId, long classNameId,
2587                    OrderByComparator<Address> orderByComparator, boolean previous) {
2588                    StringBundler query = null;
2589    
2590                    if (orderByComparator != null) {
2591                            query = new StringBundler(5 +
2592                                            (orderByComparator.getOrderByConditionFields().length * 3) +
2593                                            (orderByComparator.getOrderByFields().length * 3));
2594                    }
2595                    else {
2596                            query = new StringBundler(4);
2597                    }
2598    
2599                    query.append(_SQL_SELECT_ADDRESS_WHERE);
2600    
2601                    query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2602    
2603                    query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2604    
2605                    if (orderByComparator != null) {
2606                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2607    
2608                            if (orderByConditionFields.length > 0) {
2609                                    query.append(WHERE_AND);
2610                            }
2611    
2612                            for (int i = 0; i < orderByConditionFields.length; i++) {
2613                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2614                                    query.append(orderByConditionFields[i]);
2615    
2616                                    if ((i + 1) < orderByConditionFields.length) {
2617                                            if (orderByComparator.isAscending() ^ previous) {
2618                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2619                                            }
2620                                            else {
2621                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2622                                            }
2623                                    }
2624                                    else {
2625                                            if (orderByComparator.isAscending() ^ previous) {
2626                                                    query.append(WHERE_GREATER_THAN);
2627                                            }
2628                                            else {
2629                                                    query.append(WHERE_LESSER_THAN);
2630                                            }
2631                                    }
2632                            }
2633    
2634                            query.append(ORDER_BY_CLAUSE);
2635    
2636                            String[] orderByFields = orderByComparator.getOrderByFields();
2637    
2638                            for (int i = 0; i < orderByFields.length; i++) {
2639                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2640                                    query.append(orderByFields[i]);
2641    
2642                                    if ((i + 1) < orderByFields.length) {
2643                                            if (orderByComparator.isAscending() ^ previous) {
2644                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2645                                            }
2646                                            else {
2647                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2648                                            }
2649                                    }
2650                                    else {
2651                                            if (orderByComparator.isAscending() ^ previous) {
2652                                                    query.append(ORDER_BY_ASC);
2653                                            }
2654                                            else {
2655                                                    query.append(ORDER_BY_DESC);
2656                                            }
2657                                    }
2658                            }
2659                    }
2660                    else {
2661                            query.append(AddressModelImpl.ORDER_BY_JPQL);
2662                    }
2663    
2664                    String sql = query.toString();
2665    
2666                    Query q = session.createQuery(sql);
2667    
2668                    q.setFirstResult(0);
2669                    q.setMaxResults(2);
2670    
2671                    QueryPos qPos = QueryPos.getInstance(q);
2672    
2673                    qPos.add(companyId);
2674    
2675                    qPos.add(classNameId);
2676    
2677                    if (orderByComparator != null) {
2678                            Object[] values = orderByComparator.getOrderByConditionValues(address);
2679    
2680                            for (Object value : values) {
2681                                    qPos.add(value);
2682                            }
2683                    }
2684    
2685                    List<Address> list = q.list();
2686    
2687                    if (list.size() == 2) {
2688                            return list.get(1);
2689                    }
2690                    else {
2691                            return null;
2692                    }
2693            }
2694    
2695            /**
2696             * Removes all the addresses where companyId = &#63; and classNameId = &#63; from the database.
2697             *
2698             * @param companyId the company ID
2699             * @param classNameId the class name ID
2700             */
2701            @Override
2702            public void removeByC_C(long companyId, long classNameId) {
2703                    for (Address address : findByC_C(companyId, classNameId,
2704                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2705                            remove(address);
2706                    }
2707            }
2708    
2709            /**
2710             * Returns the number of addresses where companyId = &#63; and classNameId = &#63;.
2711             *
2712             * @param companyId the company ID
2713             * @param classNameId the class name ID
2714             * @return the number of matching addresses
2715             */
2716            @Override
2717            public int countByC_C(long companyId, long classNameId) {
2718                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C;
2719    
2720                    Object[] finderArgs = new Object[] { companyId, classNameId };
2721    
2722                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2723    
2724                    if (count == null) {
2725                            StringBundler query = new StringBundler(3);
2726    
2727                            query.append(_SQL_COUNT_ADDRESS_WHERE);
2728    
2729                            query.append(_FINDER_COLUMN_C_C_COMPANYID_2);
2730    
2731                            query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
2732    
2733                            String sql = query.toString();
2734    
2735                            Session session = null;
2736    
2737                            try {
2738                                    session = openSession();
2739    
2740                                    Query q = session.createQuery(sql);
2741    
2742                                    QueryPos qPos = QueryPos.getInstance(q);
2743    
2744                                    qPos.add(companyId);
2745    
2746                                    qPos.add(classNameId);
2747    
2748                                    count = (Long)q.uniqueResult();
2749    
2750                                    finderCache.putResult(finderPath, finderArgs, count);
2751                            }
2752                            catch (Exception e) {
2753                                    finderCache.removeResult(finderPath, finderArgs);
2754    
2755                                    throw processException(e);
2756                            }
2757                            finally {
2758                                    closeSession(session);
2759                            }
2760                    }
2761    
2762                    return count.intValue();
2763            }
2764    
2765            private static final String _FINDER_COLUMN_C_C_COMPANYID_2 = "address.companyId = ? AND ";
2766            private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "address.classNameId = ?";
2767            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2768                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
2769                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_C",
2770                            new String[] {
2771                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
2772                                    
2773                            Integer.class.getName(), Integer.class.getName(),
2774                                    OrderByComparator.class.getName()
2775                            });
2776            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2777                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
2778                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_C",
2779                            new String[] {
2780                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
2781                            },
2782                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
2783                            AddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
2784                            AddressModelImpl.CLASSPK_COLUMN_BITMASK |
2785                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
2786            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_C = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
2787                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
2788                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_C",
2789                            new String[] {
2790                                    Long.class.getName(), Long.class.getName(), Long.class.getName()
2791                            });
2792    
2793            /**
2794             * Returns all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2795             *
2796             * @param companyId the company ID
2797             * @param classNameId the class name ID
2798             * @param classPK the class p k
2799             * @return the matching addresses
2800             */
2801            @Override
2802            public List<Address> findByC_C_C(long companyId, long classNameId,
2803                    long classPK) {
2804                    return findByC_C_C(companyId, classNameId, classPK, QueryUtil.ALL_POS,
2805                            QueryUtil.ALL_POS, null);
2806            }
2807    
2808            /**
2809             * Returns a range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2810             *
2811             * <p>
2812             * 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 AddressModelImpl}. 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.
2813             * </p>
2814             *
2815             * @param companyId the company ID
2816             * @param classNameId the class name ID
2817             * @param classPK the class p k
2818             * @param start the lower bound of the range of addresses
2819             * @param end the upper bound of the range of addresses (not inclusive)
2820             * @return the range of matching addresses
2821             */
2822            @Override
2823            public List<Address> findByC_C_C(long companyId, long classNameId,
2824                    long classPK, int start, int end) {
2825                    return findByC_C_C(companyId, classNameId, classPK, start, end, null);
2826            }
2827    
2828            /**
2829             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2830             *
2831             * <p>
2832             * 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 AddressModelImpl}. 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.
2833             * </p>
2834             *
2835             * @param companyId the company ID
2836             * @param classNameId the class name ID
2837             * @param classPK the class p k
2838             * @param start the lower bound of the range of addresses
2839             * @param end the upper bound of the range of addresses (not inclusive)
2840             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2841             * @return the ordered range of matching addresses
2842             */
2843            @Override
2844            public List<Address> findByC_C_C(long companyId, long classNameId,
2845                    long classPK, int start, int end,
2846                    OrderByComparator<Address> orderByComparator) {
2847                    return findByC_C_C(companyId, classNameId, classPK, start, end,
2848                            orderByComparator, true);
2849            }
2850    
2851            /**
2852             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2853             *
2854             * <p>
2855             * 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 AddressModelImpl}. 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.
2856             * </p>
2857             *
2858             * @param companyId the company ID
2859             * @param classNameId the class name ID
2860             * @param classPK the class p k
2861             * @param start the lower bound of the range of addresses
2862             * @param end the upper bound of the range of addresses (not inclusive)
2863             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2864             * @param retrieveFromCache whether to retrieve from the finder cache
2865             * @return the ordered range of matching addresses
2866             */
2867            @Override
2868            public List<Address> findByC_C_C(long companyId, long classNameId,
2869                    long classPK, int start, int end,
2870                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
2871                    boolean pagination = true;
2872                    FinderPath finderPath = null;
2873                    Object[] finderArgs = null;
2874    
2875                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2876                                    (orderByComparator == null)) {
2877                            pagination = false;
2878                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C;
2879                            finderArgs = new Object[] { companyId, classNameId, classPK };
2880                    }
2881                    else {
2882                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C;
2883                            finderArgs = new Object[] {
2884                                            companyId, classNameId, classPK,
2885                                            
2886                                            start, end, orderByComparator
2887                                    };
2888                    }
2889    
2890                    List<Address> list = null;
2891    
2892                    if (retrieveFromCache) {
2893                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
2894                                            this);
2895    
2896                            if ((list != null) && !list.isEmpty()) {
2897                                    for (Address address : list) {
2898                                            if ((companyId != address.getCompanyId()) ||
2899                                                            (classNameId != address.getClassNameId()) ||
2900                                                            (classPK != address.getClassPK())) {
2901                                                    list = null;
2902    
2903                                                    break;
2904                                            }
2905                                    }
2906                            }
2907                    }
2908    
2909                    if (list == null) {
2910                            StringBundler query = null;
2911    
2912                            if (orderByComparator != null) {
2913                                    query = new StringBundler(5 +
2914                                                    (orderByComparator.getOrderByFields().length * 2));
2915                            }
2916                            else {
2917                                    query = new StringBundler(5);
2918                            }
2919    
2920                            query.append(_SQL_SELECT_ADDRESS_WHERE);
2921    
2922                            query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
2923    
2924                            query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
2925    
2926                            query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
2927    
2928                            if (orderByComparator != null) {
2929                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2930                                            orderByComparator);
2931                            }
2932                            else
2933                             if (pagination) {
2934                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
2935                            }
2936    
2937                            String sql = query.toString();
2938    
2939                            Session session = null;
2940    
2941                            try {
2942                                    session = openSession();
2943    
2944                                    Query q = session.createQuery(sql);
2945    
2946                                    QueryPos qPos = QueryPos.getInstance(q);
2947    
2948                                    qPos.add(companyId);
2949    
2950                                    qPos.add(classNameId);
2951    
2952                                    qPos.add(classPK);
2953    
2954                                    if (!pagination) {
2955                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
2956                                                            start, end, false);
2957    
2958                                            Collections.sort(list);
2959    
2960                                            list = Collections.unmodifiableList(list);
2961                                    }
2962                                    else {
2963                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
2964                                                            start, end);
2965                                    }
2966    
2967                                    cacheResult(list);
2968    
2969                                    finderCache.putResult(finderPath, finderArgs, list);
2970                            }
2971                            catch (Exception e) {
2972                                    finderCache.removeResult(finderPath, finderArgs);
2973    
2974                                    throw processException(e);
2975                            }
2976                            finally {
2977                                    closeSession(session);
2978                            }
2979                    }
2980    
2981                    return list;
2982            }
2983    
2984            /**
2985             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
2986             *
2987             * @param companyId the company ID
2988             * @param classNameId the class name ID
2989             * @param classPK the class p k
2990             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2991             * @return the first matching address
2992             * @throws NoSuchAddressException if a matching address could not be found
2993             */
2994            @Override
2995            public Address findByC_C_C_First(long companyId, long classNameId,
2996                    long classPK, OrderByComparator<Address> orderByComparator)
2997                    throws NoSuchAddressException {
2998                    Address address = fetchByC_C_C_First(companyId, classNameId, classPK,
2999                                    orderByComparator);
3000    
3001                    if (address != null) {
3002                            return address;
3003                    }
3004    
3005                    StringBundler msg = new StringBundler(8);
3006    
3007                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3008    
3009                    msg.append("companyId=");
3010                    msg.append(companyId);
3011    
3012                    msg.append(", classNameId=");
3013                    msg.append(classNameId);
3014    
3015                    msg.append(", classPK=");
3016                    msg.append(classPK);
3017    
3018                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3019    
3020                    throw new NoSuchAddressException(msg.toString());
3021            }
3022    
3023            /**
3024             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3025             *
3026             * @param companyId the company ID
3027             * @param classNameId the class name ID
3028             * @param classPK the class p k
3029             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3030             * @return the first matching address, or <code>null</code> if a matching address could not be found
3031             */
3032            @Override
3033            public Address fetchByC_C_C_First(long companyId, long classNameId,
3034                    long classPK, OrderByComparator<Address> orderByComparator) {
3035                    List<Address> list = findByC_C_C(companyId, classNameId, classPK, 0, 1,
3036                                    orderByComparator);
3037    
3038                    if (!list.isEmpty()) {
3039                            return list.get(0);
3040                    }
3041    
3042                    return null;
3043            }
3044    
3045            /**
3046             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3047             *
3048             * @param companyId the company ID
3049             * @param classNameId the class name ID
3050             * @param classPK the class p k
3051             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3052             * @return the last matching address
3053             * @throws NoSuchAddressException if a matching address could not be found
3054             */
3055            @Override
3056            public Address findByC_C_C_Last(long companyId, long classNameId,
3057                    long classPK, OrderByComparator<Address> orderByComparator)
3058                    throws NoSuchAddressException {
3059                    Address address = fetchByC_C_C_Last(companyId, classNameId, classPK,
3060                                    orderByComparator);
3061    
3062                    if (address != null) {
3063                            return address;
3064                    }
3065    
3066                    StringBundler msg = new StringBundler(8);
3067    
3068                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3069    
3070                    msg.append("companyId=");
3071                    msg.append(companyId);
3072    
3073                    msg.append(", classNameId=");
3074                    msg.append(classNameId);
3075    
3076                    msg.append(", classPK=");
3077                    msg.append(classPK);
3078    
3079                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3080    
3081                    throw new NoSuchAddressException(msg.toString());
3082            }
3083    
3084            /**
3085             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3086             *
3087             * @param companyId the company ID
3088             * @param classNameId the class name ID
3089             * @param classPK the class p k
3090             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3091             * @return the last matching address, or <code>null</code> if a matching address could not be found
3092             */
3093            @Override
3094            public Address fetchByC_C_C_Last(long companyId, long classNameId,
3095                    long classPK, OrderByComparator<Address> orderByComparator) {
3096                    int count = countByC_C_C(companyId, classNameId, classPK);
3097    
3098                    if (count == 0) {
3099                            return null;
3100                    }
3101    
3102                    List<Address> list = findByC_C_C(companyId, classNameId, classPK,
3103                                    count - 1, count, orderByComparator);
3104    
3105                    if (!list.isEmpty()) {
3106                            return list.get(0);
3107                    }
3108    
3109                    return null;
3110            }
3111    
3112            /**
3113             * Returns the addresses before and after the current address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3114             *
3115             * @param addressId the primary key of the current address
3116             * @param companyId the company ID
3117             * @param classNameId the class name ID
3118             * @param classPK the class p k
3119             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3120             * @return the previous, current, and next address
3121             * @throws NoSuchAddressException if a address with the primary key could not be found
3122             */
3123            @Override
3124            public Address[] findByC_C_C_PrevAndNext(long addressId, long companyId,
3125                    long classNameId, long classPK,
3126                    OrderByComparator<Address> orderByComparator)
3127                    throws NoSuchAddressException {
3128                    Address address = findByPrimaryKey(addressId);
3129    
3130                    Session session = null;
3131    
3132                    try {
3133                            session = openSession();
3134    
3135                            Address[] array = new AddressImpl[3];
3136    
3137                            array[0] = getByC_C_C_PrevAndNext(session, address, companyId,
3138                                            classNameId, classPK, orderByComparator, true);
3139    
3140                            array[1] = address;
3141    
3142                            array[2] = getByC_C_C_PrevAndNext(session, address, companyId,
3143                                            classNameId, classPK, orderByComparator, false);
3144    
3145                            return array;
3146                    }
3147                    catch (Exception e) {
3148                            throw processException(e);
3149                    }
3150                    finally {
3151                            closeSession(session);
3152                    }
3153            }
3154    
3155            protected Address getByC_C_C_PrevAndNext(Session session, Address address,
3156                    long companyId, long classNameId, long classPK,
3157                    OrderByComparator<Address> orderByComparator, boolean previous) {
3158                    StringBundler query = null;
3159    
3160                    if (orderByComparator != null) {
3161                            query = new StringBundler(6 +
3162                                            (orderByComparator.getOrderByConditionFields().length * 3) +
3163                                            (orderByComparator.getOrderByFields().length * 3));
3164                    }
3165                    else {
3166                            query = new StringBundler(5);
3167                    }
3168    
3169                    query.append(_SQL_SELECT_ADDRESS_WHERE);
3170    
3171                    query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
3172    
3173                    query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
3174    
3175                    query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
3176    
3177                    if (orderByComparator != null) {
3178                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3179    
3180                            if (orderByConditionFields.length > 0) {
3181                                    query.append(WHERE_AND);
3182                            }
3183    
3184                            for (int i = 0; i < orderByConditionFields.length; i++) {
3185                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3186                                    query.append(orderByConditionFields[i]);
3187    
3188                                    if ((i + 1) < orderByConditionFields.length) {
3189                                            if (orderByComparator.isAscending() ^ previous) {
3190                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3191                                            }
3192                                            else {
3193                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3194                                            }
3195                                    }
3196                                    else {
3197                                            if (orderByComparator.isAscending() ^ previous) {
3198                                                    query.append(WHERE_GREATER_THAN);
3199                                            }
3200                                            else {
3201                                                    query.append(WHERE_LESSER_THAN);
3202                                            }
3203                                    }
3204                            }
3205    
3206                            query.append(ORDER_BY_CLAUSE);
3207    
3208                            String[] orderByFields = orderByComparator.getOrderByFields();
3209    
3210                            for (int i = 0; i < orderByFields.length; i++) {
3211                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3212                                    query.append(orderByFields[i]);
3213    
3214                                    if ((i + 1) < orderByFields.length) {
3215                                            if (orderByComparator.isAscending() ^ previous) {
3216                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3217                                            }
3218                                            else {
3219                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3220                                            }
3221                                    }
3222                                    else {
3223                                            if (orderByComparator.isAscending() ^ previous) {
3224                                                    query.append(ORDER_BY_ASC);
3225                                            }
3226                                            else {
3227                                                    query.append(ORDER_BY_DESC);
3228                                            }
3229                                    }
3230                            }
3231                    }
3232                    else {
3233                            query.append(AddressModelImpl.ORDER_BY_JPQL);
3234                    }
3235    
3236                    String sql = query.toString();
3237    
3238                    Query q = session.createQuery(sql);
3239    
3240                    q.setFirstResult(0);
3241                    q.setMaxResults(2);
3242    
3243                    QueryPos qPos = QueryPos.getInstance(q);
3244    
3245                    qPos.add(companyId);
3246    
3247                    qPos.add(classNameId);
3248    
3249                    qPos.add(classPK);
3250    
3251                    if (orderByComparator != null) {
3252                            Object[] values = orderByComparator.getOrderByConditionValues(address);
3253    
3254                            for (Object value : values) {
3255                                    qPos.add(value);
3256                            }
3257                    }
3258    
3259                    List<Address> list = q.list();
3260    
3261                    if (list.size() == 2) {
3262                            return list.get(1);
3263                    }
3264                    else {
3265                            return null;
3266                    }
3267            }
3268    
3269            /**
3270             * Removes all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; from the database.
3271             *
3272             * @param companyId the company ID
3273             * @param classNameId the class name ID
3274             * @param classPK the class p k
3275             */
3276            @Override
3277            public void removeByC_C_C(long companyId, long classNameId, long classPK) {
3278                    for (Address address : findByC_C_C(companyId, classNameId, classPK,
3279                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3280                            remove(address);
3281                    }
3282            }
3283    
3284            /**
3285             * Returns the number of addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63;.
3286             *
3287             * @param companyId the company ID
3288             * @param classNameId the class name ID
3289             * @param classPK the class p k
3290             * @return the number of matching addresses
3291             */
3292            @Override
3293            public int countByC_C_C(long companyId, long classNameId, long classPK) {
3294                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_C;
3295    
3296                    Object[] finderArgs = new Object[] { companyId, classNameId, classPK };
3297    
3298                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3299    
3300                    if (count == null) {
3301                            StringBundler query = new StringBundler(4);
3302    
3303                            query.append(_SQL_COUNT_ADDRESS_WHERE);
3304    
3305                            query.append(_FINDER_COLUMN_C_C_C_COMPANYID_2);
3306    
3307                            query.append(_FINDER_COLUMN_C_C_C_CLASSNAMEID_2);
3308    
3309                            query.append(_FINDER_COLUMN_C_C_C_CLASSPK_2);
3310    
3311                            String sql = query.toString();
3312    
3313                            Session session = null;
3314    
3315                            try {
3316                                    session = openSession();
3317    
3318                                    Query q = session.createQuery(sql);
3319    
3320                                    QueryPos qPos = QueryPos.getInstance(q);
3321    
3322                                    qPos.add(companyId);
3323    
3324                                    qPos.add(classNameId);
3325    
3326                                    qPos.add(classPK);
3327    
3328                                    count = (Long)q.uniqueResult();
3329    
3330                                    finderCache.putResult(finderPath, finderArgs, count);
3331                            }
3332                            catch (Exception e) {
3333                                    finderCache.removeResult(finderPath, finderArgs);
3334    
3335                                    throw processException(e);
3336                            }
3337                            finally {
3338                                    closeSession(session);
3339                            }
3340                    }
3341    
3342                    return count.intValue();
3343            }
3344    
3345            private static final String _FINDER_COLUMN_C_C_C_COMPANYID_2 = "address.companyId = ? AND ";
3346            private static final String _FINDER_COLUMN_C_C_C_CLASSNAMEID_2 = "address.classNameId = ? AND ";
3347            private static final String _FINDER_COLUMN_C_C_C_CLASSPK_2 = "address.classPK = ?";
3348            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_M = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3349                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
3350                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_C_M",
3351                            new String[] {
3352                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3353                                    Boolean.class.getName(),
3354                                    
3355                            Integer.class.getName(), Integer.class.getName(),
3356                                    OrderByComparator.class.getName()
3357                            });
3358            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_M =
3359                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3360                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
3361                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_C_M",
3362                            new String[] {
3363                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3364                                    Boolean.class.getName()
3365                            },
3366                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
3367                            AddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
3368                            AddressModelImpl.CLASSPK_COLUMN_BITMASK |
3369                            AddressModelImpl.MAILING_COLUMN_BITMASK |
3370                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
3371            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_C_M = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3372                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
3373                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_C_M",
3374                            new String[] {
3375                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3376                                    Boolean.class.getName()
3377                            });
3378    
3379            /**
3380             * Returns all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3381             *
3382             * @param companyId the company ID
3383             * @param classNameId the class name ID
3384             * @param classPK the class p k
3385             * @param mailing the mailing
3386             * @return the matching addresses
3387             */
3388            @Override
3389            public List<Address> findByC_C_C_M(long companyId, long classNameId,
3390                    long classPK, boolean mailing) {
3391                    return findByC_C_C_M(companyId, classNameId, classPK, mailing,
3392                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3393            }
3394    
3395            /**
3396             * Returns a range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3397             *
3398             * <p>
3399             * 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 AddressModelImpl}. 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.
3400             * </p>
3401             *
3402             * @param companyId the company ID
3403             * @param classNameId the class name ID
3404             * @param classPK the class p k
3405             * @param mailing the mailing
3406             * @param start the lower bound of the range of addresses
3407             * @param end the upper bound of the range of addresses (not inclusive)
3408             * @return the range of matching addresses
3409             */
3410            @Override
3411            public List<Address> findByC_C_C_M(long companyId, long classNameId,
3412                    long classPK, boolean mailing, int start, int end) {
3413                    return findByC_C_C_M(companyId, classNameId, classPK, mailing, start,
3414                            end, null);
3415            }
3416    
3417            /**
3418             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3419             *
3420             * <p>
3421             * 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 AddressModelImpl}. 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.
3422             * </p>
3423             *
3424             * @param companyId the company ID
3425             * @param classNameId the class name ID
3426             * @param classPK the class p k
3427             * @param mailing the mailing
3428             * @param start the lower bound of the range of addresses
3429             * @param end the upper bound of the range of addresses (not inclusive)
3430             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3431             * @return the ordered range of matching addresses
3432             */
3433            @Override
3434            public List<Address> findByC_C_C_M(long companyId, long classNameId,
3435                    long classPK, boolean mailing, int start, int end,
3436                    OrderByComparator<Address> orderByComparator) {
3437                    return findByC_C_C_M(companyId, classNameId, classPK, mailing, start,
3438                            end, orderByComparator, true);
3439            }
3440    
3441            /**
3442             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3443             *
3444             * <p>
3445             * 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 AddressModelImpl}. 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.
3446             * </p>
3447             *
3448             * @param companyId the company ID
3449             * @param classNameId the class name ID
3450             * @param classPK the class p k
3451             * @param mailing the mailing
3452             * @param start the lower bound of the range of addresses
3453             * @param end the upper bound of the range of addresses (not inclusive)
3454             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3455             * @param retrieveFromCache whether to retrieve from the finder cache
3456             * @return the ordered range of matching addresses
3457             */
3458            @Override
3459            public List<Address> findByC_C_C_M(long companyId, long classNameId,
3460                    long classPK, boolean mailing, int start, int end,
3461                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
3462                    boolean pagination = true;
3463                    FinderPath finderPath = null;
3464                    Object[] finderArgs = null;
3465    
3466                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3467                                    (orderByComparator == null)) {
3468                            pagination = false;
3469                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_M;
3470                            finderArgs = new Object[] { companyId, classNameId, classPK, mailing };
3471                    }
3472                    else {
3473                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_M;
3474                            finderArgs = new Object[] {
3475                                            companyId, classNameId, classPK, mailing,
3476                                            
3477                                            start, end, orderByComparator
3478                                    };
3479                    }
3480    
3481                    List<Address> list = null;
3482    
3483                    if (retrieveFromCache) {
3484                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
3485                                            this);
3486    
3487                            if ((list != null) && !list.isEmpty()) {
3488                                    for (Address address : list) {
3489                                            if ((companyId != address.getCompanyId()) ||
3490                                                            (classNameId != address.getClassNameId()) ||
3491                                                            (classPK != address.getClassPK()) ||
3492                                                            (mailing != address.getMailing())) {
3493                                                    list = null;
3494    
3495                                                    break;
3496                                            }
3497                                    }
3498                            }
3499                    }
3500    
3501                    if (list == null) {
3502                            StringBundler query = null;
3503    
3504                            if (orderByComparator != null) {
3505                                    query = new StringBundler(6 +
3506                                                    (orderByComparator.getOrderByFields().length * 2));
3507                            }
3508                            else {
3509                                    query = new StringBundler(6);
3510                            }
3511    
3512                            query.append(_SQL_SELECT_ADDRESS_WHERE);
3513    
3514                            query.append(_FINDER_COLUMN_C_C_C_M_COMPANYID_2);
3515    
3516                            query.append(_FINDER_COLUMN_C_C_C_M_CLASSNAMEID_2);
3517    
3518                            query.append(_FINDER_COLUMN_C_C_C_M_CLASSPK_2);
3519    
3520                            query.append(_FINDER_COLUMN_C_C_C_M_MAILING_2);
3521    
3522                            if (orderByComparator != null) {
3523                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3524                                            orderByComparator);
3525                            }
3526                            else
3527                             if (pagination) {
3528                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
3529                            }
3530    
3531                            String sql = query.toString();
3532    
3533                            Session session = null;
3534    
3535                            try {
3536                                    session = openSession();
3537    
3538                                    Query q = session.createQuery(sql);
3539    
3540                                    QueryPos qPos = QueryPos.getInstance(q);
3541    
3542                                    qPos.add(companyId);
3543    
3544                                    qPos.add(classNameId);
3545    
3546                                    qPos.add(classPK);
3547    
3548                                    qPos.add(mailing);
3549    
3550                                    if (!pagination) {
3551                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
3552                                                            start, end, false);
3553    
3554                                            Collections.sort(list);
3555    
3556                                            list = Collections.unmodifiableList(list);
3557                                    }
3558                                    else {
3559                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
3560                                                            start, end);
3561                                    }
3562    
3563                                    cacheResult(list);
3564    
3565                                    finderCache.putResult(finderPath, finderArgs, list);
3566                            }
3567                            catch (Exception e) {
3568                                    finderCache.removeResult(finderPath, finderArgs);
3569    
3570                                    throw processException(e);
3571                            }
3572                            finally {
3573                                    closeSession(session);
3574                            }
3575                    }
3576    
3577                    return list;
3578            }
3579    
3580            /**
3581             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3582             *
3583             * @param companyId the company ID
3584             * @param classNameId the class name ID
3585             * @param classPK the class p k
3586             * @param mailing the mailing
3587             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3588             * @return the first matching address
3589             * @throws NoSuchAddressException if a matching address could not be found
3590             */
3591            @Override
3592            public Address findByC_C_C_M_First(long companyId, long classNameId,
3593                    long classPK, boolean mailing,
3594                    OrderByComparator<Address> orderByComparator)
3595                    throws NoSuchAddressException {
3596                    Address address = fetchByC_C_C_M_First(companyId, classNameId, classPK,
3597                                    mailing, orderByComparator);
3598    
3599                    if (address != null) {
3600                            return address;
3601                    }
3602    
3603                    StringBundler msg = new StringBundler(10);
3604    
3605                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3606    
3607                    msg.append("companyId=");
3608                    msg.append(companyId);
3609    
3610                    msg.append(", classNameId=");
3611                    msg.append(classNameId);
3612    
3613                    msg.append(", classPK=");
3614                    msg.append(classPK);
3615    
3616                    msg.append(", mailing=");
3617                    msg.append(mailing);
3618    
3619                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3620    
3621                    throw new NoSuchAddressException(msg.toString());
3622            }
3623    
3624            /**
3625             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3626             *
3627             * @param companyId the company ID
3628             * @param classNameId the class name ID
3629             * @param classPK the class p k
3630             * @param mailing the mailing
3631             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3632             * @return the first matching address, or <code>null</code> if a matching address could not be found
3633             */
3634            @Override
3635            public Address fetchByC_C_C_M_First(long companyId, long classNameId,
3636                    long classPK, boolean mailing,
3637                    OrderByComparator<Address> orderByComparator) {
3638                    List<Address> list = findByC_C_C_M(companyId, classNameId, classPK,
3639                                    mailing, 0, 1, orderByComparator);
3640    
3641                    if (!list.isEmpty()) {
3642                            return list.get(0);
3643                    }
3644    
3645                    return null;
3646            }
3647    
3648            /**
3649             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3650             *
3651             * @param companyId the company ID
3652             * @param classNameId the class name ID
3653             * @param classPK the class p k
3654             * @param mailing the mailing
3655             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3656             * @return the last matching address
3657             * @throws NoSuchAddressException if a matching address could not be found
3658             */
3659            @Override
3660            public Address findByC_C_C_M_Last(long companyId, long classNameId,
3661                    long classPK, boolean mailing,
3662                    OrderByComparator<Address> orderByComparator)
3663                    throws NoSuchAddressException {
3664                    Address address = fetchByC_C_C_M_Last(companyId, classNameId, classPK,
3665                                    mailing, orderByComparator);
3666    
3667                    if (address != null) {
3668                            return address;
3669                    }
3670    
3671                    StringBundler msg = new StringBundler(10);
3672    
3673                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3674    
3675                    msg.append("companyId=");
3676                    msg.append(companyId);
3677    
3678                    msg.append(", classNameId=");
3679                    msg.append(classNameId);
3680    
3681                    msg.append(", classPK=");
3682                    msg.append(classPK);
3683    
3684                    msg.append(", mailing=");
3685                    msg.append(mailing);
3686    
3687                    msg.append(StringPool.CLOSE_CURLY_BRACE);
3688    
3689                    throw new NoSuchAddressException(msg.toString());
3690            }
3691    
3692            /**
3693             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3694             *
3695             * @param companyId the company ID
3696             * @param classNameId the class name ID
3697             * @param classPK the class p k
3698             * @param mailing the mailing
3699             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3700             * @return the last matching address, or <code>null</code> if a matching address could not be found
3701             */
3702            @Override
3703            public Address fetchByC_C_C_M_Last(long companyId, long classNameId,
3704                    long classPK, boolean mailing,
3705                    OrderByComparator<Address> orderByComparator) {
3706                    int count = countByC_C_C_M(companyId, classNameId, classPK, mailing);
3707    
3708                    if (count == 0) {
3709                            return null;
3710                    }
3711    
3712                    List<Address> list = findByC_C_C_M(companyId, classNameId, classPK,
3713                                    mailing, count - 1, count, orderByComparator);
3714    
3715                    if (!list.isEmpty()) {
3716                            return list.get(0);
3717                    }
3718    
3719                    return null;
3720            }
3721    
3722            /**
3723             * Returns the addresses before and after the current address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3724             *
3725             * @param addressId the primary key of the current address
3726             * @param companyId the company ID
3727             * @param classNameId the class name ID
3728             * @param classPK the class p k
3729             * @param mailing the mailing
3730             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
3731             * @return the previous, current, and next address
3732             * @throws NoSuchAddressException if a address with the primary key could not be found
3733             */
3734            @Override
3735            public Address[] findByC_C_C_M_PrevAndNext(long addressId, long companyId,
3736                    long classNameId, long classPK, boolean mailing,
3737                    OrderByComparator<Address> orderByComparator)
3738                    throws NoSuchAddressException {
3739                    Address address = findByPrimaryKey(addressId);
3740    
3741                    Session session = null;
3742    
3743                    try {
3744                            session = openSession();
3745    
3746                            Address[] array = new AddressImpl[3];
3747    
3748                            array[0] = getByC_C_C_M_PrevAndNext(session, address, companyId,
3749                                            classNameId, classPK, mailing, orderByComparator, true);
3750    
3751                            array[1] = address;
3752    
3753                            array[2] = getByC_C_C_M_PrevAndNext(session, address, companyId,
3754                                            classNameId, classPK, mailing, orderByComparator, false);
3755    
3756                            return array;
3757                    }
3758                    catch (Exception e) {
3759                            throw processException(e);
3760                    }
3761                    finally {
3762                            closeSession(session);
3763                    }
3764            }
3765    
3766            protected Address getByC_C_C_M_PrevAndNext(Session session,
3767                    Address address, long companyId, long classNameId, long classPK,
3768                    boolean mailing, OrderByComparator<Address> orderByComparator,
3769                    boolean previous) {
3770                    StringBundler query = null;
3771    
3772                    if (orderByComparator != null) {
3773                            query = new StringBundler(7 +
3774                                            (orderByComparator.getOrderByConditionFields().length * 3) +
3775                                            (orderByComparator.getOrderByFields().length * 3));
3776                    }
3777                    else {
3778                            query = new StringBundler(6);
3779                    }
3780    
3781                    query.append(_SQL_SELECT_ADDRESS_WHERE);
3782    
3783                    query.append(_FINDER_COLUMN_C_C_C_M_COMPANYID_2);
3784    
3785                    query.append(_FINDER_COLUMN_C_C_C_M_CLASSNAMEID_2);
3786    
3787                    query.append(_FINDER_COLUMN_C_C_C_M_CLASSPK_2);
3788    
3789                    query.append(_FINDER_COLUMN_C_C_C_M_MAILING_2);
3790    
3791                    if (orderByComparator != null) {
3792                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3793    
3794                            if (orderByConditionFields.length > 0) {
3795                                    query.append(WHERE_AND);
3796                            }
3797    
3798                            for (int i = 0; i < orderByConditionFields.length; i++) {
3799                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3800                                    query.append(orderByConditionFields[i]);
3801    
3802                                    if ((i + 1) < orderByConditionFields.length) {
3803                                            if (orderByComparator.isAscending() ^ previous) {
3804                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3805                                            }
3806                                            else {
3807                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3808                                            }
3809                                    }
3810                                    else {
3811                                            if (orderByComparator.isAscending() ^ previous) {
3812                                                    query.append(WHERE_GREATER_THAN);
3813                                            }
3814                                            else {
3815                                                    query.append(WHERE_LESSER_THAN);
3816                                            }
3817                                    }
3818                            }
3819    
3820                            query.append(ORDER_BY_CLAUSE);
3821    
3822                            String[] orderByFields = orderByComparator.getOrderByFields();
3823    
3824                            for (int i = 0; i < orderByFields.length; i++) {
3825                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3826                                    query.append(orderByFields[i]);
3827    
3828                                    if ((i + 1) < orderByFields.length) {
3829                                            if (orderByComparator.isAscending() ^ previous) {
3830                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3831                                            }
3832                                            else {
3833                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3834                                            }
3835                                    }
3836                                    else {
3837                                            if (orderByComparator.isAscending() ^ previous) {
3838                                                    query.append(ORDER_BY_ASC);
3839                                            }
3840                                            else {
3841                                                    query.append(ORDER_BY_DESC);
3842                                            }
3843                                    }
3844                            }
3845                    }
3846                    else {
3847                            query.append(AddressModelImpl.ORDER_BY_JPQL);
3848                    }
3849    
3850                    String sql = query.toString();
3851    
3852                    Query q = session.createQuery(sql);
3853    
3854                    q.setFirstResult(0);
3855                    q.setMaxResults(2);
3856    
3857                    QueryPos qPos = QueryPos.getInstance(q);
3858    
3859                    qPos.add(companyId);
3860    
3861                    qPos.add(classNameId);
3862    
3863                    qPos.add(classPK);
3864    
3865                    qPos.add(mailing);
3866    
3867                    if (orderByComparator != null) {
3868                            Object[] values = orderByComparator.getOrderByConditionValues(address);
3869    
3870                            for (Object value : values) {
3871                                    qPos.add(value);
3872                            }
3873                    }
3874    
3875                    List<Address> list = q.list();
3876    
3877                    if (list.size() == 2) {
3878                            return list.get(1);
3879                    }
3880                    else {
3881                            return null;
3882                    }
3883            }
3884    
3885            /**
3886             * Removes all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63; from the database.
3887             *
3888             * @param companyId the company ID
3889             * @param classNameId the class name ID
3890             * @param classPK the class p k
3891             * @param mailing the mailing
3892             */
3893            @Override
3894            public void removeByC_C_C_M(long companyId, long classNameId, long classPK,
3895                    boolean mailing) {
3896                    for (Address address : findByC_C_C_M(companyId, classNameId, classPK,
3897                                    mailing, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3898                            remove(address);
3899                    }
3900            }
3901    
3902            /**
3903             * Returns the number of addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and mailing = &#63;.
3904             *
3905             * @param companyId the company ID
3906             * @param classNameId the class name ID
3907             * @param classPK the class p k
3908             * @param mailing the mailing
3909             * @return the number of matching addresses
3910             */
3911            @Override
3912            public int countByC_C_C_M(long companyId, long classNameId, long classPK,
3913                    boolean mailing) {
3914                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_C_M;
3915    
3916                    Object[] finderArgs = new Object[] {
3917                                    companyId, classNameId, classPK, mailing
3918                            };
3919    
3920                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3921    
3922                    if (count == null) {
3923                            StringBundler query = new StringBundler(5);
3924    
3925                            query.append(_SQL_COUNT_ADDRESS_WHERE);
3926    
3927                            query.append(_FINDER_COLUMN_C_C_C_M_COMPANYID_2);
3928    
3929                            query.append(_FINDER_COLUMN_C_C_C_M_CLASSNAMEID_2);
3930    
3931                            query.append(_FINDER_COLUMN_C_C_C_M_CLASSPK_2);
3932    
3933                            query.append(_FINDER_COLUMN_C_C_C_M_MAILING_2);
3934    
3935                            String sql = query.toString();
3936    
3937                            Session session = null;
3938    
3939                            try {
3940                                    session = openSession();
3941    
3942                                    Query q = session.createQuery(sql);
3943    
3944                                    QueryPos qPos = QueryPos.getInstance(q);
3945    
3946                                    qPos.add(companyId);
3947    
3948                                    qPos.add(classNameId);
3949    
3950                                    qPos.add(classPK);
3951    
3952                                    qPos.add(mailing);
3953    
3954                                    count = (Long)q.uniqueResult();
3955    
3956                                    finderCache.putResult(finderPath, finderArgs, count);
3957                            }
3958                            catch (Exception e) {
3959                                    finderCache.removeResult(finderPath, finderArgs);
3960    
3961                                    throw processException(e);
3962                            }
3963                            finally {
3964                                    closeSession(session);
3965                            }
3966                    }
3967    
3968                    return count.intValue();
3969            }
3970    
3971            private static final String _FINDER_COLUMN_C_C_C_M_COMPANYID_2 = "address.companyId = ? AND ";
3972            private static final String _FINDER_COLUMN_C_C_C_M_CLASSNAMEID_2 = "address.classNameId = ? AND ";
3973            private static final String _FINDER_COLUMN_C_C_C_M_CLASSPK_2 = "address.classPK = ? AND ";
3974            private static final String _FINDER_COLUMN_C_C_C_M_MAILING_2 = "address.mailing = ?";
3975            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_P = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3976                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
3977                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_C_C_P",
3978                            new String[] {
3979                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3980                                    Boolean.class.getName(),
3981                                    
3982                            Integer.class.getName(), Integer.class.getName(),
3983                                    OrderByComparator.class.getName()
3984                            });
3985            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P =
3986                    new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3987                            AddressModelImpl.FINDER_CACHE_ENABLED, AddressImpl.class,
3988                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_C_C_P",
3989                            new String[] {
3990                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
3991                                    Boolean.class.getName()
3992                            },
3993                            AddressModelImpl.COMPANYID_COLUMN_BITMASK |
3994                            AddressModelImpl.CLASSNAMEID_COLUMN_BITMASK |
3995                            AddressModelImpl.CLASSPK_COLUMN_BITMASK |
3996                            AddressModelImpl.PRIMARY_COLUMN_BITMASK |
3997                            AddressModelImpl.CREATEDATE_COLUMN_BITMASK);
3998            public static final FinderPath FINDER_PATH_COUNT_BY_C_C_C_P = new FinderPath(AddressModelImpl.ENTITY_CACHE_ENABLED,
3999                            AddressModelImpl.FINDER_CACHE_ENABLED, Long.class,
4000                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_C_C_P",
4001                            new String[] {
4002                                    Long.class.getName(), Long.class.getName(), Long.class.getName(),
4003                                    Boolean.class.getName()
4004                            });
4005    
4006            /**
4007             * Returns all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4008             *
4009             * @param companyId the company ID
4010             * @param classNameId the class name ID
4011             * @param classPK the class p k
4012             * @param primary the primary
4013             * @return the matching addresses
4014             */
4015            @Override
4016            public List<Address> findByC_C_C_P(long companyId, long classNameId,
4017                    long classPK, boolean primary) {
4018                    return findByC_C_C_P(companyId, classNameId, classPK, primary,
4019                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4020            }
4021    
4022            /**
4023             * Returns a range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4024             *
4025             * <p>
4026             * 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 AddressModelImpl}. 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.
4027             * </p>
4028             *
4029             * @param companyId the company ID
4030             * @param classNameId the class name ID
4031             * @param classPK the class p k
4032             * @param primary the primary
4033             * @param start the lower bound of the range of addresses
4034             * @param end the upper bound of the range of addresses (not inclusive)
4035             * @return the range of matching addresses
4036             */
4037            @Override
4038            public List<Address> findByC_C_C_P(long companyId, long classNameId,
4039                    long classPK, boolean primary, int start, int end) {
4040                    return findByC_C_C_P(companyId, classNameId, classPK, primary, start,
4041                            end, null);
4042            }
4043    
4044            /**
4045             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4046             *
4047             * <p>
4048             * 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 AddressModelImpl}. 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.
4049             * </p>
4050             *
4051             * @param companyId the company ID
4052             * @param classNameId the class name ID
4053             * @param classPK the class p k
4054             * @param primary the primary
4055             * @param start the lower bound of the range of addresses
4056             * @param end the upper bound of the range of addresses (not inclusive)
4057             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4058             * @return the ordered range of matching addresses
4059             */
4060            @Override
4061            public List<Address> findByC_C_C_P(long companyId, long classNameId,
4062                    long classPK, boolean primary, int start, int end,
4063                    OrderByComparator<Address> orderByComparator) {
4064                    return findByC_C_C_P(companyId, classNameId, classPK, primary, start,
4065                            end, orderByComparator, true);
4066            }
4067    
4068            /**
4069             * Returns an ordered range of all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4070             *
4071             * <p>
4072             * 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 AddressModelImpl}. 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.
4073             * </p>
4074             *
4075             * @param companyId the company ID
4076             * @param classNameId the class name ID
4077             * @param classPK the class p k
4078             * @param primary the primary
4079             * @param start the lower bound of the range of addresses
4080             * @param end the upper bound of the range of addresses (not inclusive)
4081             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4082             * @param retrieveFromCache whether to retrieve from the finder cache
4083             * @return the ordered range of matching addresses
4084             */
4085            @Override
4086            public List<Address> findByC_C_C_P(long companyId, long classNameId,
4087                    long classPK, boolean primary, int start, int end,
4088                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
4089                    boolean pagination = true;
4090                    FinderPath finderPath = null;
4091                    Object[] finderArgs = null;
4092    
4093                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4094                                    (orderByComparator == null)) {
4095                            pagination = false;
4096                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P;
4097                            finderArgs = new Object[] { companyId, classNameId, classPK, primary };
4098                    }
4099                    else {
4100                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_C_C_P;
4101                            finderArgs = new Object[] {
4102                                            companyId, classNameId, classPK, primary,
4103                                            
4104                                            start, end, orderByComparator
4105                                    };
4106                    }
4107    
4108                    List<Address> list = null;
4109    
4110                    if (retrieveFromCache) {
4111                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
4112                                            this);
4113    
4114                            if ((list != null) && !list.isEmpty()) {
4115                                    for (Address address : list) {
4116                                            if ((companyId != address.getCompanyId()) ||
4117                                                            (classNameId != address.getClassNameId()) ||
4118                                                            (classPK != address.getClassPK()) ||
4119                                                            (primary != address.getPrimary())) {
4120                                                    list = null;
4121    
4122                                                    break;
4123                                            }
4124                                    }
4125                            }
4126                    }
4127    
4128                    if (list == null) {
4129                            StringBundler query = null;
4130    
4131                            if (orderByComparator != null) {
4132                                    query = new StringBundler(6 +
4133                                                    (orderByComparator.getOrderByFields().length * 2));
4134                            }
4135                            else {
4136                                    query = new StringBundler(6);
4137                            }
4138    
4139                            query.append(_SQL_SELECT_ADDRESS_WHERE);
4140    
4141                            query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
4142    
4143                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
4144    
4145                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
4146    
4147                            query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
4148    
4149                            if (orderByComparator != null) {
4150                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4151                                            orderByComparator);
4152                            }
4153                            else
4154                             if (pagination) {
4155                                    query.append(AddressModelImpl.ORDER_BY_JPQL);
4156                            }
4157    
4158                            String sql = query.toString();
4159    
4160                            Session session = null;
4161    
4162                            try {
4163                                    session = openSession();
4164    
4165                                    Query q = session.createQuery(sql);
4166    
4167                                    QueryPos qPos = QueryPos.getInstance(q);
4168    
4169                                    qPos.add(companyId);
4170    
4171                                    qPos.add(classNameId);
4172    
4173                                    qPos.add(classPK);
4174    
4175                                    qPos.add(primary);
4176    
4177                                    if (!pagination) {
4178                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
4179                                                            start, end, false);
4180    
4181                                            Collections.sort(list);
4182    
4183                                            list = Collections.unmodifiableList(list);
4184                                    }
4185                                    else {
4186                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
4187                                                            start, end);
4188                                    }
4189    
4190                                    cacheResult(list);
4191    
4192                                    finderCache.putResult(finderPath, finderArgs, list);
4193                            }
4194                            catch (Exception e) {
4195                                    finderCache.removeResult(finderPath, finderArgs);
4196    
4197                                    throw processException(e);
4198                            }
4199                            finally {
4200                                    closeSession(session);
4201                            }
4202                    }
4203    
4204                    return list;
4205            }
4206    
4207            /**
4208             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4209             *
4210             * @param companyId the company ID
4211             * @param classNameId the class name ID
4212             * @param classPK the class p k
4213             * @param primary the primary
4214             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4215             * @return the first matching address
4216             * @throws NoSuchAddressException if a matching address could not be found
4217             */
4218            @Override
4219            public Address findByC_C_C_P_First(long companyId, long classNameId,
4220                    long classPK, boolean primary,
4221                    OrderByComparator<Address> orderByComparator)
4222                    throws NoSuchAddressException {
4223                    Address address = fetchByC_C_C_P_First(companyId, classNameId, classPK,
4224                                    primary, orderByComparator);
4225    
4226                    if (address != null) {
4227                            return address;
4228                    }
4229    
4230                    StringBundler msg = new StringBundler(10);
4231    
4232                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4233    
4234                    msg.append("companyId=");
4235                    msg.append(companyId);
4236    
4237                    msg.append(", classNameId=");
4238                    msg.append(classNameId);
4239    
4240                    msg.append(", classPK=");
4241                    msg.append(classPK);
4242    
4243                    msg.append(", primary=");
4244                    msg.append(primary);
4245    
4246                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4247    
4248                    throw new NoSuchAddressException(msg.toString());
4249            }
4250    
4251            /**
4252             * Returns the first address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4253             *
4254             * @param companyId the company ID
4255             * @param classNameId the class name ID
4256             * @param classPK the class p k
4257             * @param primary the primary
4258             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4259             * @return the first matching address, or <code>null</code> if a matching address could not be found
4260             */
4261            @Override
4262            public Address fetchByC_C_C_P_First(long companyId, long classNameId,
4263                    long classPK, boolean primary,
4264                    OrderByComparator<Address> orderByComparator) {
4265                    List<Address> list = findByC_C_C_P(companyId, classNameId, classPK,
4266                                    primary, 0, 1, orderByComparator);
4267    
4268                    if (!list.isEmpty()) {
4269                            return list.get(0);
4270                    }
4271    
4272                    return null;
4273            }
4274    
4275            /**
4276             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4277             *
4278             * @param companyId the company ID
4279             * @param classNameId the class name ID
4280             * @param classPK the class p k
4281             * @param primary the primary
4282             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4283             * @return the last matching address
4284             * @throws NoSuchAddressException if a matching address could not be found
4285             */
4286            @Override
4287            public Address findByC_C_C_P_Last(long companyId, long classNameId,
4288                    long classPK, boolean primary,
4289                    OrderByComparator<Address> orderByComparator)
4290                    throws NoSuchAddressException {
4291                    Address address = fetchByC_C_C_P_Last(companyId, classNameId, classPK,
4292                                    primary, orderByComparator);
4293    
4294                    if (address != null) {
4295                            return address;
4296                    }
4297    
4298                    StringBundler msg = new StringBundler(10);
4299    
4300                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4301    
4302                    msg.append("companyId=");
4303                    msg.append(companyId);
4304    
4305                    msg.append(", classNameId=");
4306                    msg.append(classNameId);
4307    
4308                    msg.append(", classPK=");
4309                    msg.append(classPK);
4310    
4311                    msg.append(", primary=");
4312                    msg.append(primary);
4313    
4314                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4315    
4316                    throw new NoSuchAddressException(msg.toString());
4317            }
4318    
4319            /**
4320             * Returns the last address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4321             *
4322             * @param companyId the company ID
4323             * @param classNameId the class name ID
4324             * @param classPK the class p k
4325             * @param primary the primary
4326             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4327             * @return the last matching address, or <code>null</code> if a matching address could not be found
4328             */
4329            @Override
4330            public Address fetchByC_C_C_P_Last(long companyId, long classNameId,
4331                    long classPK, boolean primary,
4332                    OrderByComparator<Address> orderByComparator) {
4333                    int count = countByC_C_C_P(companyId, classNameId, classPK, primary);
4334    
4335                    if (count == 0) {
4336                            return null;
4337                    }
4338    
4339                    List<Address> list = findByC_C_C_P(companyId, classNameId, classPK,
4340                                    primary, count - 1, count, orderByComparator);
4341    
4342                    if (!list.isEmpty()) {
4343                            return list.get(0);
4344                    }
4345    
4346                    return null;
4347            }
4348    
4349            /**
4350             * Returns the addresses before and after the current address in the ordered set where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4351             *
4352             * @param addressId the primary key of the current address
4353             * @param companyId the company ID
4354             * @param classNameId the class name ID
4355             * @param classPK the class p k
4356             * @param primary the primary
4357             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4358             * @return the previous, current, and next address
4359             * @throws NoSuchAddressException if a address with the primary key could not be found
4360             */
4361            @Override
4362            public Address[] findByC_C_C_P_PrevAndNext(long addressId, long companyId,
4363                    long classNameId, long classPK, boolean primary,
4364                    OrderByComparator<Address> orderByComparator)
4365                    throws NoSuchAddressException {
4366                    Address address = findByPrimaryKey(addressId);
4367    
4368                    Session session = null;
4369    
4370                    try {
4371                            session = openSession();
4372    
4373                            Address[] array = new AddressImpl[3];
4374    
4375                            array[0] = getByC_C_C_P_PrevAndNext(session, address, companyId,
4376                                            classNameId, classPK, primary, orderByComparator, true);
4377    
4378                            array[1] = address;
4379    
4380                            array[2] = getByC_C_C_P_PrevAndNext(session, address, companyId,
4381                                            classNameId, classPK, primary, orderByComparator, false);
4382    
4383                            return array;
4384                    }
4385                    catch (Exception e) {
4386                            throw processException(e);
4387                    }
4388                    finally {
4389                            closeSession(session);
4390                    }
4391            }
4392    
4393            protected Address getByC_C_C_P_PrevAndNext(Session session,
4394                    Address address, long companyId, long classNameId, long classPK,
4395                    boolean primary, OrderByComparator<Address> orderByComparator,
4396                    boolean previous) {
4397                    StringBundler query = null;
4398    
4399                    if (orderByComparator != null) {
4400                            query = new StringBundler(7 +
4401                                            (orderByComparator.getOrderByConditionFields().length * 3) +
4402                                            (orderByComparator.getOrderByFields().length * 3));
4403                    }
4404                    else {
4405                            query = new StringBundler(6);
4406                    }
4407    
4408                    query.append(_SQL_SELECT_ADDRESS_WHERE);
4409    
4410                    query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
4411    
4412                    query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
4413    
4414                    query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
4415    
4416                    query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
4417    
4418                    if (orderByComparator != null) {
4419                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
4420    
4421                            if (orderByConditionFields.length > 0) {
4422                                    query.append(WHERE_AND);
4423                            }
4424    
4425                            for (int i = 0; i < orderByConditionFields.length; i++) {
4426                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4427                                    query.append(orderByConditionFields[i]);
4428    
4429                                    if ((i + 1) < orderByConditionFields.length) {
4430                                            if (orderByComparator.isAscending() ^ previous) {
4431                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
4432                                            }
4433                                            else {
4434                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
4435                                            }
4436                                    }
4437                                    else {
4438                                            if (orderByComparator.isAscending() ^ previous) {
4439                                                    query.append(WHERE_GREATER_THAN);
4440                                            }
4441                                            else {
4442                                                    query.append(WHERE_LESSER_THAN);
4443                                            }
4444                                    }
4445                            }
4446    
4447                            query.append(ORDER_BY_CLAUSE);
4448    
4449                            String[] orderByFields = orderByComparator.getOrderByFields();
4450    
4451                            for (int i = 0; i < orderByFields.length; i++) {
4452                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4453                                    query.append(orderByFields[i]);
4454    
4455                                    if ((i + 1) < orderByFields.length) {
4456                                            if (orderByComparator.isAscending() ^ previous) {
4457                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
4458                                            }
4459                                            else {
4460                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
4461                                            }
4462                                    }
4463                                    else {
4464                                            if (orderByComparator.isAscending() ^ previous) {
4465                                                    query.append(ORDER_BY_ASC);
4466                                            }
4467                                            else {
4468                                                    query.append(ORDER_BY_DESC);
4469                                            }
4470                                    }
4471                            }
4472                    }
4473                    else {
4474                            query.append(AddressModelImpl.ORDER_BY_JPQL);
4475                    }
4476    
4477                    String sql = query.toString();
4478    
4479                    Query q = session.createQuery(sql);
4480    
4481                    q.setFirstResult(0);
4482                    q.setMaxResults(2);
4483    
4484                    QueryPos qPos = QueryPos.getInstance(q);
4485    
4486                    qPos.add(companyId);
4487    
4488                    qPos.add(classNameId);
4489    
4490                    qPos.add(classPK);
4491    
4492                    qPos.add(primary);
4493    
4494                    if (orderByComparator != null) {
4495                            Object[] values = orderByComparator.getOrderByConditionValues(address);
4496    
4497                            for (Object value : values) {
4498                                    qPos.add(value);
4499                            }
4500                    }
4501    
4502                    List<Address> list = q.list();
4503    
4504                    if (list.size() == 2) {
4505                            return list.get(1);
4506                    }
4507                    else {
4508                            return null;
4509                    }
4510            }
4511    
4512            /**
4513             * Removes all the addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63; from the database.
4514             *
4515             * @param companyId the company ID
4516             * @param classNameId the class name ID
4517             * @param classPK the class p k
4518             * @param primary the primary
4519             */
4520            @Override
4521            public void removeByC_C_C_P(long companyId, long classNameId, long classPK,
4522                    boolean primary) {
4523                    for (Address address : findByC_C_C_P(companyId, classNameId, classPK,
4524                                    primary, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
4525                            remove(address);
4526                    }
4527            }
4528    
4529            /**
4530             * Returns the number of addresses where companyId = &#63; and classNameId = &#63; and classPK = &#63; and primary = &#63;.
4531             *
4532             * @param companyId the company ID
4533             * @param classNameId the class name ID
4534             * @param classPK the class p k
4535             * @param primary the primary
4536             * @return the number of matching addresses
4537             */
4538            @Override
4539            public int countByC_C_C_P(long companyId, long classNameId, long classPK,
4540                    boolean primary) {
4541                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_C_C_P;
4542    
4543                    Object[] finderArgs = new Object[] {
4544                                    companyId, classNameId, classPK, primary
4545                            };
4546    
4547                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
4548    
4549                    if (count == null) {
4550                            StringBundler query = new StringBundler(5);
4551    
4552                            query.append(_SQL_COUNT_ADDRESS_WHERE);
4553    
4554                            query.append(_FINDER_COLUMN_C_C_C_P_COMPANYID_2);
4555    
4556                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2);
4557    
4558                            query.append(_FINDER_COLUMN_C_C_C_P_CLASSPK_2);
4559    
4560                            query.append(_FINDER_COLUMN_C_C_C_P_PRIMARY_2);
4561    
4562                            String sql = query.toString();
4563    
4564                            Session session = null;
4565    
4566                            try {
4567                                    session = openSession();
4568    
4569                                    Query q = session.createQuery(sql);
4570    
4571                                    QueryPos qPos = QueryPos.getInstance(q);
4572    
4573                                    qPos.add(companyId);
4574    
4575                                    qPos.add(classNameId);
4576    
4577                                    qPos.add(classPK);
4578    
4579                                    qPos.add(primary);
4580    
4581                                    count = (Long)q.uniqueResult();
4582    
4583                                    finderCache.putResult(finderPath, finderArgs, count);
4584                            }
4585                            catch (Exception e) {
4586                                    finderCache.removeResult(finderPath, finderArgs);
4587    
4588                                    throw processException(e);
4589                            }
4590                            finally {
4591                                    closeSession(session);
4592                            }
4593                    }
4594    
4595                    return count.intValue();
4596            }
4597    
4598            private static final String _FINDER_COLUMN_C_C_C_P_COMPANYID_2 = "address.companyId = ? AND ";
4599            private static final String _FINDER_COLUMN_C_C_C_P_CLASSNAMEID_2 = "address.classNameId = ? AND ";
4600            private static final String _FINDER_COLUMN_C_C_C_P_CLASSPK_2 = "address.classPK = ? AND ";
4601            private static final String _FINDER_COLUMN_C_C_C_P_PRIMARY_2 = "address.primary = ?";
4602    
4603            public AddressPersistenceImpl() {
4604                    setModelClass(Address.class);
4605            }
4606    
4607            /**
4608             * Caches the address in the entity cache if it is enabled.
4609             *
4610             * @param address the address
4611             */
4612            @Override
4613            public void cacheResult(Address address) {
4614                    entityCache.putResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
4615                            AddressImpl.class, address.getPrimaryKey(), address);
4616    
4617                    address.resetOriginalValues();
4618            }
4619    
4620            /**
4621             * Caches the addresses in the entity cache if it is enabled.
4622             *
4623             * @param addresses the addresses
4624             */
4625            @Override
4626            public void cacheResult(List<Address> addresses) {
4627                    for (Address address : addresses) {
4628                            if (entityCache.getResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
4629                                                    AddressImpl.class, address.getPrimaryKey()) == null) {
4630                                    cacheResult(address);
4631                            }
4632                            else {
4633                                    address.resetOriginalValues();
4634                            }
4635                    }
4636            }
4637    
4638            /**
4639             * Clears the cache for all addresses.
4640             *
4641             * <p>
4642             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
4643             * </p>
4644             */
4645            @Override
4646            public void clearCache() {
4647                    entityCache.clearCache(AddressImpl.class);
4648    
4649                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
4650                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4651                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4652            }
4653    
4654            /**
4655             * Clears the cache for the address.
4656             *
4657             * <p>
4658             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
4659             * </p>
4660             */
4661            @Override
4662            public void clearCache(Address address) {
4663                    entityCache.removeResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
4664                            AddressImpl.class, address.getPrimaryKey());
4665    
4666                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4667                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4668            }
4669    
4670            @Override
4671            public void clearCache(List<Address> addresses) {
4672                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4673                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4674    
4675                    for (Address address : addresses) {
4676                            entityCache.removeResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
4677                                    AddressImpl.class, address.getPrimaryKey());
4678                    }
4679            }
4680    
4681            /**
4682             * Creates a new address with the primary key. Does not add the address to the database.
4683             *
4684             * @param addressId the primary key for the new address
4685             * @return the new address
4686             */
4687            @Override
4688            public Address create(long addressId) {
4689                    Address address = new AddressImpl();
4690    
4691                    address.setNew(true);
4692                    address.setPrimaryKey(addressId);
4693    
4694                    String uuid = PortalUUIDUtil.generate();
4695    
4696                    address.setUuid(uuid);
4697    
4698                    address.setCompanyId(companyProvider.getCompanyId());
4699    
4700                    return address;
4701            }
4702    
4703            /**
4704             * Removes the address with the primary key from the database. Also notifies the appropriate model listeners.
4705             *
4706             * @param addressId the primary key of the address
4707             * @return the address that was removed
4708             * @throws NoSuchAddressException if a address with the primary key could not be found
4709             */
4710            @Override
4711            public Address remove(long addressId) throws NoSuchAddressException {
4712                    return remove((Serializable)addressId);
4713            }
4714    
4715            /**
4716             * Removes the address with the primary key from the database. Also notifies the appropriate model listeners.
4717             *
4718             * @param primaryKey the primary key of the address
4719             * @return the address that was removed
4720             * @throws NoSuchAddressException if a address with the primary key could not be found
4721             */
4722            @Override
4723            public Address remove(Serializable primaryKey)
4724                    throws NoSuchAddressException {
4725                    Session session = null;
4726    
4727                    try {
4728                            session = openSession();
4729    
4730                            Address address = (Address)session.get(AddressImpl.class, primaryKey);
4731    
4732                            if (address == null) {
4733                                    if (_log.isDebugEnabled()) {
4734                                            _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4735                                    }
4736    
4737                                    throw new NoSuchAddressException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4738                                            primaryKey);
4739                            }
4740    
4741                            return remove(address);
4742                    }
4743                    catch (NoSuchAddressException nsee) {
4744                            throw nsee;
4745                    }
4746                    catch (Exception e) {
4747                            throw processException(e);
4748                    }
4749                    finally {
4750                            closeSession(session);
4751                    }
4752            }
4753    
4754            @Override
4755            protected Address removeImpl(Address address) {
4756                    address = toUnwrappedModel(address);
4757    
4758                    Session session = null;
4759    
4760                    try {
4761                            session = openSession();
4762    
4763                            if (!session.contains(address)) {
4764                                    address = (Address)session.get(AddressImpl.class,
4765                                                    address.getPrimaryKeyObj());
4766                            }
4767    
4768                            if (address != null) {
4769                                    session.delete(address);
4770                            }
4771                    }
4772                    catch (Exception e) {
4773                            throw processException(e);
4774                    }
4775                    finally {
4776                            closeSession(session);
4777                    }
4778    
4779                    if (address != null) {
4780                            clearCache(address);
4781                    }
4782    
4783                    return address;
4784            }
4785    
4786            @Override
4787            public Address updateImpl(Address address) {
4788                    address = toUnwrappedModel(address);
4789    
4790                    boolean isNew = address.isNew();
4791    
4792                    AddressModelImpl addressModelImpl = (AddressModelImpl)address;
4793    
4794                    if (Validator.isNull(address.getUuid())) {
4795                            String uuid = PortalUUIDUtil.generate();
4796    
4797                            address.setUuid(uuid);
4798                    }
4799    
4800                    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();
4801    
4802                    Date now = new Date();
4803    
4804                    if (isNew && (address.getCreateDate() == null)) {
4805                            if (serviceContext == null) {
4806                                    address.setCreateDate(now);
4807                            }
4808                            else {
4809                                    address.setCreateDate(serviceContext.getCreateDate(now));
4810                            }
4811                    }
4812    
4813                    if (!addressModelImpl.hasSetModifiedDate()) {
4814                            if (serviceContext == null) {
4815                                    address.setModifiedDate(now);
4816                            }
4817                            else {
4818                                    address.setModifiedDate(serviceContext.getModifiedDate(now));
4819                            }
4820                    }
4821    
4822                    Session session = null;
4823    
4824                    try {
4825                            session = openSession();
4826    
4827                            if (address.isNew()) {
4828                                    session.save(address);
4829    
4830                                    address.setNew(false);
4831                            }
4832                            else {
4833                                    address = (Address)session.merge(address);
4834                            }
4835                    }
4836                    catch (Exception e) {
4837                            throw processException(e);
4838                    }
4839                    finally {
4840                            closeSession(session);
4841                    }
4842    
4843                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4844    
4845                    if (isNew || !AddressModelImpl.COLUMN_BITMASK_ENABLED) {
4846                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4847                    }
4848    
4849                    else {
4850                            if ((addressModelImpl.getColumnBitmask() &
4851                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID.getColumnBitmask()) != 0) {
4852                                    Object[] args = new Object[] { addressModelImpl.getOriginalUuid() };
4853    
4854                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
4855                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
4856                                            args);
4857    
4858                                    args = new Object[] { addressModelImpl.getUuid() };
4859    
4860                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID, args);
4861                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID,
4862                                            args);
4863                            }
4864    
4865                            if ((addressModelImpl.getColumnBitmask() &
4866                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C.getColumnBitmask()) != 0) {
4867                                    Object[] args = new Object[] {
4868                                                    addressModelImpl.getOriginalUuid(),
4869                                                    addressModelImpl.getOriginalCompanyId()
4870                                            };
4871    
4872                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
4873                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
4874                                            args);
4875    
4876                                    args = new Object[] {
4877                                                    addressModelImpl.getUuid(),
4878                                                    addressModelImpl.getCompanyId()
4879                                            };
4880    
4881                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_UUID_C, args);
4882                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_UUID_C,
4883                                            args);
4884                            }
4885    
4886                            if ((addressModelImpl.getColumnBitmask() &
4887                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID.getColumnBitmask()) != 0) {
4888                                    Object[] args = new Object[] {
4889                                                    addressModelImpl.getOriginalCompanyId()
4890                                            };
4891    
4892                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
4893                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
4894                                            args);
4895    
4896                                    args = new Object[] { addressModelImpl.getCompanyId() };
4897    
4898                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_COMPANYID, args);
4899                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_COMPANYID,
4900                                            args);
4901                            }
4902    
4903                            if ((addressModelImpl.getColumnBitmask() &
4904                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
4905                                    Object[] args = new Object[] {
4906                                                    addressModelImpl.getOriginalUserId()
4907                                            };
4908    
4909                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
4910                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
4911                                            args);
4912    
4913                                    args = new Object[] { addressModelImpl.getUserId() };
4914    
4915                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
4916                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
4917                                            args);
4918                            }
4919    
4920                            if ((addressModelImpl.getColumnBitmask() &
4921                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C.getColumnBitmask()) != 0) {
4922                                    Object[] args = new Object[] {
4923                                                    addressModelImpl.getOriginalCompanyId(),
4924                                                    addressModelImpl.getOriginalClassNameId()
4925                                            };
4926    
4927                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
4928                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
4929                                            args);
4930    
4931                                    args = new Object[] {
4932                                                    addressModelImpl.getCompanyId(),
4933                                                    addressModelImpl.getClassNameId()
4934                                            };
4935    
4936                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C, args);
4937                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C,
4938                                            args);
4939                            }
4940    
4941                            if ((addressModelImpl.getColumnBitmask() &
4942                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C.getColumnBitmask()) != 0) {
4943                                    Object[] args = new Object[] {
4944                                                    addressModelImpl.getOriginalCompanyId(),
4945                                                    addressModelImpl.getOriginalClassNameId(),
4946                                                    addressModelImpl.getOriginalClassPK()
4947                                            };
4948    
4949                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C, args);
4950                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C,
4951                                            args);
4952    
4953                                    args = new Object[] {
4954                                                    addressModelImpl.getCompanyId(),
4955                                                    addressModelImpl.getClassNameId(),
4956                                                    addressModelImpl.getClassPK()
4957                                            };
4958    
4959                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C, args);
4960                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C,
4961                                            args);
4962                            }
4963    
4964                            if ((addressModelImpl.getColumnBitmask() &
4965                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_M.getColumnBitmask()) != 0) {
4966                                    Object[] args = new Object[] {
4967                                                    addressModelImpl.getOriginalCompanyId(),
4968                                                    addressModelImpl.getOriginalClassNameId(),
4969                                                    addressModelImpl.getOriginalClassPK(),
4970                                                    addressModelImpl.getOriginalMailing()
4971                                            };
4972    
4973                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C_M, args);
4974                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_M,
4975                                            args);
4976    
4977                                    args = new Object[] {
4978                                                    addressModelImpl.getCompanyId(),
4979                                                    addressModelImpl.getClassNameId(),
4980                                                    addressModelImpl.getClassPK(),
4981                                                    addressModelImpl.getMailing()
4982                                            };
4983    
4984                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C_M, args);
4985                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_M,
4986                                            args);
4987                            }
4988    
4989                            if ((addressModelImpl.getColumnBitmask() &
4990                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P.getColumnBitmask()) != 0) {
4991                                    Object[] args = new Object[] {
4992                                                    addressModelImpl.getOriginalCompanyId(),
4993                                                    addressModelImpl.getOriginalClassNameId(),
4994                                                    addressModelImpl.getOriginalClassPK(),
4995                                                    addressModelImpl.getOriginalPrimary()
4996                                            };
4997    
4998                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C_P, args);
4999                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P,
5000                                            args);
5001    
5002                                    args = new Object[] {
5003                                                    addressModelImpl.getCompanyId(),
5004                                                    addressModelImpl.getClassNameId(),
5005                                                    addressModelImpl.getClassPK(),
5006                                                    addressModelImpl.getPrimary()
5007                                            };
5008    
5009                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_C_C_P, args);
5010                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_C_C_P,
5011                                            args);
5012                            }
5013                    }
5014    
5015                    entityCache.putResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5016                            AddressImpl.class, address.getPrimaryKey(), address, false);
5017    
5018                    address.resetOriginalValues();
5019    
5020                    return address;
5021            }
5022    
5023            protected Address toUnwrappedModel(Address address) {
5024                    if (address instanceof AddressImpl) {
5025                            return address;
5026                    }
5027    
5028                    AddressImpl addressImpl = new AddressImpl();
5029    
5030                    addressImpl.setNew(address.isNew());
5031                    addressImpl.setPrimaryKey(address.getPrimaryKey());
5032    
5033                    addressImpl.setMvccVersion(address.getMvccVersion());
5034                    addressImpl.setUuid(address.getUuid());
5035                    addressImpl.setAddressId(address.getAddressId());
5036                    addressImpl.setCompanyId(address.getCompanyId());
5037                    addressImpl.setUserId(address.getUserId());
5038                    addressImpl.setUserName(address.getUserName());
5039                    addressImpl.setCreateDate(address.getCreateDate());
5040                    addressImpl.setModifiedDate(address.getModifiedDate());
5041                    addressImpl.setClassNameId(address.getClassNameId());
5042                    addressImpl.setClassPK(address.getClassPK());
5043                    addressImpl.setStreet1(address.getStreet1());
5044                    addressImpl.setStreet2(address.getStreet2());
5045                    addressImpl.setStreet3(address.getStreet3());
5046                    addressImpl.setCity(address.getCity());
5047                    addressImpl.setZip(address.getZip());
5048                    addressImpl.setRegionId(address.getRegionId());
5049                    addressImpl.setCountryId(address.getCountryId());
5050                    addressImpl.setTypeId(address.getTypeId());
5051                    addressImpl.setMailing(address.isMailing());
5052                    addressImpl.setPrimary(address.isPrimary());
5053    
5054                    return addressImpl;
5055            }
5056    
5057            /**
5058             * Returns the address with the primary key or throws a {@link com.liferay.portal.kernel.exception.NoSuchModelException} if it could not be found.
5059             *
5060             * @param primaryKey the primary key of the address
5061             * @return the address
5062             * @throws NoSuchAddressException if a address with the primary key could not be found
5063             */
5064            @Override
5065            public Address findByPrimaryKey(Serializable primaryKey)
5066                    throws NoSuchAddressException {
5067                    Address address = fetchByPrimaryKey(primaryKey);
5068    
5069                    if (address == null) {
5070                            if (_log.isDebugEnabled()) {
5071                                    _log.debug(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
5072                            }
5073    
5074                            throw new NoSuchAddressException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
5075                                    primaryKey);
5076                    }
5077    
5078                    return address;
5079            }
5080    
5081            /**
5082             * Returns the address with the primary key or throws a {@link NoSuchAddressException} if it could not be found.
5083             *
5084             * @param addressId the primary key of the address
5085             * @return the address
5086             * @throws NoSuchAddressException if a address with the primary key could not be found
5087             */
5088            @Override
5089            public Address findByPrimaryKey(long addressId)
5090                    throws NoSuchAddressException {
5091                    return findByPrimaryKey((Serializable)addressId);
5092            }
5093    
5094            /**
5095             * Returns the address with the primary key or returns <code>null</code> if it could not be found.
5096             *
5097             * @param primaryKey the primary key of the address
5098             * @return the address, or <code>null</code> if a address with the primary key could not be found
5099             */
5100            @Override
5101            public Address fetchByPrimaryKey(Serializable primaryKey) {
5102                    Address address = (Address)entityCache.getResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5103                                    AddressImpl.class, primaryKey);
5104    
5105                    if (address == _nullAddress) {
5106                            return null;
5107                    }
5108    
5109                    if (address == null) {
5110                            Session session = null;
5111    
5112                            try {
5113                                    session = openSession();
5114    
5115                                    address = (Address)session.get(AddressImpl.class, primaryKey);
5116    
5117                                    if (address != null) {
5118                                            cacheResult(address);
5119                                    }
5120                                    else {
5121                                            entityCache.putResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5122                                                    AddressImpl.class, primaryKey, _nullAddress);
5123                                    }
5124                            }
5125                            catch (Exception e) {
5126                                    entityCache.removeResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5127                                            AddressImpl.class, primaryKey);
5128    
5129                                    throw processException(e);
5130                            }
5131                            finally {
5132                                    closeSession(session);
5133                            }
5134                    }
5135    
5136                    return address;
5137            }
5138    
5139            /**
5140             * Returns the address with the primary key or returns <code>null</code> if it could not be found.
5141             *
5142             * @param addressId the primary key of the address
5143             * @return the address, or <code>null</code> if a address with the primary key could not be found
5144             */
5145            @Override
5146            public Address fetchByPrimaryKey(long addressId) {
5147                    return fetchByPrimaryKey((Serializable)addressId);
5148            }
5149    
5150            @Override
5151            public Map<Serializable, Address> fetchByPrimaryKeys(
5152                    Set<Serializable> primaryKeys) {
5153                    if (primaryKeys.isEmpty()) {
5154                            return Collections.emptyMap();
5155                    }
5156    
5157                    Map<Serializable, Address> map = new HashMap<Serializable, Address>();
5158    
5159                    if (primaryKeys.size() == 1) {
5160                            Iterator<Serializable> iterator = primaryKeys.iterator();
5161    
5162                            Serializable primaryKey = iterator.next();
5163    
5164                            Address address = fetchByPrimaryKey(primaryKey);
5165    
5166                            if (address != null) {
5167                                    map.put(primaryKey, address);
5168                            }
5169    
5170                            return map;
5171                    }
5172    
5173                    Set<Serializable> uncachedPrimaryKeys = null;
5174    
5175                    for (Serializable primaryKey : primaryKeys) {
5176                            Address address = (Address)entityCache.getResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5177                                            AddressImpl.class, primaryKey);
5178    
5179                            if (address == null) {
5180                                    if (uncachedPrimaryKeys == null) {
5181                                            uncachedPrimaryKeys = new HashSet<Serializable>();
5182                                    }
5183    
5184                                    uncachedPrimaryKeys.add(primaryKey);
5185                            }
5186                            else {
5187                                    map.put(primaryKey, address);
5188                            }
5189                    }
5190    
5191                    if (uncachedPrimaryKeys == null) {
5192                            return map;
5193                    }
5194    
5195                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
5196                                    1);
5197    
5198                    query.append(_SQL_SELECT_ADDRESS_WHERE_PKS_IN);
5199    
5200                    for (Serializable primaryKey : uncachedPrimaryKeys) {
5201                            query.append(String.valueOf(primaryKey));
5202    
5203                            query.append(StringPool.COMMA);
5204                    }
5205    
5206                    query.setIndex(query.index() - 1);
5207    
5208                    query.append(StringPool.CLOSE_PARENTHESIS);
5209    
5210                    String sql = query.toString();
5211    
5212                    Session session = null;
5213    
5214                    try {
5215                            session = openSession();
5216    
5217                            Query q = session.createQuery(sql);
5218    
5219                            for (Address address : (List<Address>)q.list()) {
5220                                    map.put(address.getPrimaryKeyObj(), address);
5221    
5222                                    cacheResult(address);
5223    
5224                                    uncachedPrimaryKeys.remove(address.getPrimaryKeyObj());
5225                            }
5226    
5227                            for (Serializable primaryKey : uncachedPrimaryKeys) {
5228                                    entityCache.putResult(AddressModelImpl.ENTITY_CACHE_ENABLED,
5229                                            AddressImpl.class, primaryKey, _nullAddress);
5230                            }
5231                    }
5232                    catch (Exception e) {
5233                            throw processException(e);
5234                    }
5235                    finally {
5236                            closeSession(session);
5237                    }
5238    
5239                    return map;
5240            }
5241    
5242            /**
5243             * Returns all the addresses.
5244             *
5245             * @return the addresses
5246             */
5247            @Override
5248            public List<Address> findAll() {
5249                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
5250            }
5251    
5252            /**
5253             * Returns a range of all the addresses.
5254             *
5255             * <p>
5256             * 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 AddressModelImpl}. 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.
5257             * </p>
5258             *
5259             * @param start the lower bound of the range of addresses
5260             * @param end the upper bound of the range of addresses (not inclusive)
5261             * @return the range of addresses
5262             */
5263            @Override
5264            public List<Address> findAll(int start, int end) {
5265                    return findAll(start, end, null);
5266            }
5267    
5268            /**
5269             * Returns an ordered range of all the addresses.
5270             *
5271             * <p>
5272             * 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 AddressModelImpl}. 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.
5273             * </p>
5274             *
5275             * @param start the lower bound of the range of addresses
5276             * @param end the upper bound of the range of addresses (not inclusive)
5277             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5278             * @return the ordered range of addresses
5279             */
5280            @Override
5281            public List<Address> findAll(int start, int end,
5282                    OrderByComparator<Address> orderByComparator) {
5283                    return findAll(start, end, orderByComparator, true);
5284            }
5285    
5286            /**
5287             * Returns an ordered range of all the addresses.
5288             *
5289             * <p>
5290             * 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 AddressModelImpl}. 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.
5291             * </p>
5292             *
5293             * @param start the lower bound of the range of addresses
5294             * @param end the upper bound of the range of addresses (not inclusive)
5295             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5296             * @param retrieveFromCache whether to retrieve from the finder cache
5297             * @return the ordered range of addresses
5298             */
5299            @Override
5300            public List<Address> findAll(int start, int end,
5301                    OrderByComparator<Address> orderByComparator, boolean retrieveFromCache) {
5302                    boolean pagination = true;
5303                    FinderPath finderPath = null;
5304                    Object[] finderArgs = null;
5305    
5306                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
5307                                    (orderByComparator == null)) {
5308                            pagination = false;
5309                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
5310                            finderArgs = FINDER_ARGS_EMPTY;
5311                    }
5312                    else {
5313                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
5314                            finderArgs = new Object[] { start, end, orderByComparator };
5315                    }
5316    
5317                    List<Address> list = null;
5318    
5319                    if (retrieveFromCache) {
5320                            list = (List<Address>)finderCache.getResult(finderPath, finderArgs,
5321                                            this);
5322                    }
5323    
5324                    if (list == null) {
5325                            StringBundler query = null;
5326                            String sql = null;
5327    
5328                            if (orderByComparator != null) {
5329                                    query = new StringBundler(2 +
5330                                                    (orderByComparator.getOrderByFields().length * 2));
5331    
5332                                    query.append(_SQL_SELECT_ADDRESS);
5333    
5334                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5335                                            orderByComparator);
5336    
5337                                    sql = query.toString();
5338                            }
5339                            else {
5340                                    sql = _SQL_SELECT_ADDRESS;
5341    
5342                                    if (pagination) {
5343                                            sql = sql.concat(AddressModelImpl.ORDER_BY_JPQL);
5344                                    }
5345                            }
5346    
5347                            Session session = null;
5348    
5349                            try {
5350                                    session = openSession();
5351    
5352                                    Query q = session.createQuery(sql);
5353    
5354                                    if (!pagination) {
5355                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
5356                                                            start, end, false);
5357    
5358                                            Collections.sort(list);
5359    
5360                                            list = Collections.unmodifiableList(list);
5361                                    }
5362                                    else {
5363                                            list = (List<Address>)QueryUtil.list(q, getDialect(),
5364                                                            start, end);
5365                                    }
5366    
5367                                    cacheResult(list);
5368    
5369                                    finderCache.putResult(finderPath, finderArgs, list);
5370                            }
5371                            catch (Exception e) {
5372                                    finderCache.removeResult(finderPath, finderArgs);
5373    
5374                                    throw processException(e);
5375                            }
5376                            finally {
5377                                    closeSession(session);
5378                            }
5379                    }
5380    
5381                    return list;
5382            }
5383    
5384            /**
5385             * Removes all the addresses from the database.
5386             *
5387             */
5388            @Override
5389            public void removeAll() {
5390                    for (Address address : findAll()) {
5391                            remove(address);
5392                    }
5393            }
5394    
5395            /**
5396             * Returns the number of addresses.
5397             *
5398             * @return the number of addresses
5399             */
5400            @Override
5401            public int countAll() {
5402                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
5403                                    FINDER_ARGS_EMPTY, this);
5404    
5405                    if (count == null) {
5406                            Session session = null;
5407    
5408                            try {
5409                                    session = openSession();
5410    
5411                                    Query q = session.createQuery(_SQL_COUNT_ADDRESS);
5412    
5413                                    count = (Long)q.uniqueResult();
5414    
5415                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
5416                                            count);
5417                            }
5418                            catch (Exception e) {
5419                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
5420                                            FINDER_ARGS_EMPTY);
5421    
5422                                    throw processException(e);
5423                            }
5424                            finally {
5425                                    closeSession(session);
5426                            }
5427                    }
5428    
5429                    return count.intValue();
5430            }
5431    
5432            @Override
5433            public Set<String> getBadColumnNames() {
5434                    return _badColumnNames;
5435            }
5436    
5437            @Override
5438            protected Map<String, Integer> getTableColumnsMap() {
5439                    return AddressModelImpl.TABLE_COLUMNS_MAP;
5440            }
5441    
5442            /**
5443             * Initializes the address persistence.
5444             */
5445            public void afterPropertiesSet() {
5446            }
5447    
5448            public void destroy() {
5449                    entityCache.removeCache(AddressImpl.class.getName());
5450                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
5451                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5452                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5453            }
5454    
5455            @BeanReference(type = CompanyProviderWrapper.class)
5456            protected CompanyProvider companyProvider;
5457            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
5458            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
5459            private static final String _SQL_SELECT_ADDRESS = "SELECT address FROM Address address";
5460            private static final String _SQL_SELECT_ADDRESS_WHERE_PKS_IN = "SELECT address FROM Address address WHERE addressId IN (";
5461            private static final String _SQL_SELECT_ADDRESS_WHERE = "SELECT address FROM Address address WHERE ";
5462            private static final String _SQL_COUNT_ADDRESS = "SELECT COUNT(address) FROM Address address";
5463            private static final String _SQL_COUNT_ADDRESS_WHERE = "SELECT COUNT(address) FROM Address address WHERE ";
5464            private static final String _ORDER_BY_ENTITY_ALIAS = "address.";
5465            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Address exists with the primary key ";
5466            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Address exists with the key {";
5467            private static final Log _log = LogFactoryUtil.getLog(AddressPersistenceImpl.class);
5468            private static final Set<String> _badColumnNames = SetUtil.fromArray(new String[] {
5469                                    "uuid", "primary"
5470                            });
5471            private static final Address _nullAddress = new AddressImpl() {
5472                            @Override
5473                            public Object clone() {
5474                                    return this;
5475                            }
5476    
5477                            @Override
5478                            public CacheModel<Address> toCacheModel() {
5479                                    return _nullAddressCacheModel;
5480                            }
5481                    };
5482    
5483            private static final CacheModel<Address> _nullAddressCacheModel = new NullCacheModel();
5484    
5485            private static class NullCacheModel implements CacheModel<Address>,
5486                    MVCCModel {
5487                    @Override
5488                    public long getMvccVersion() {
5489                            return -1;
5490                    }
5491    
5492                    @Override
5493                    public void setMvccVersion(long mvccVersion) {
5494                    }
5495    
5496                    @Override
5497                    public Address toEntityModel() {
5498                            return _nullAddress;
5499                    }
5500            }
5501    }