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