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