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