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