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