001    /**
002     * Copyright (c) 2000-2012 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.portlet.expando.service.persistence;
016    
017    import com.liferay.portal.NoSuchModelException;
018    import com.liferay.portal.kernel.bean.BeanReference;
019    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.exception.SystemException;
028    import com.liferay.portal.kernel.log.Log;
029    import com.liferay.portal.kernel.log.LogFactoryUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.model.CacheModel;
037    import com.liferay.portal.model.ModelListener;
038    import com.liferay.portal.service.persistence.UserPersistence;
039    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
040    
041    import com.liferay.portlet.expando.NoSuchRowException;
042    import com.liferay.portlet.expando.model.ExpandoRow;
043    import com.liferay.portlet.expando.model.impl.ExpandoRowImpl;
044    import com.liferay.portlet.expando.model.impl.ExpandoRowModelImpl;
045    
046    import java.io.Serializable;
047    
048    import java.util.ArrayList;
049    import java.util.Collections;
050    import java.util.List;
051    
052    /**
053     * The persistence implementation for the expando row service.
054     *
055     * <p>
056     * Caching information and settings can be found in <code>portal.properties</code>
057     * </p>
058     *
059     * @author Brian Wing Shun Chan
060     * @see ExpandoRowPersistence
061     * @see ExpandoRowUtil
062     * @generated
063     */
064    public class ExpandoRowPersistenceImpl extends BasePersistenceImpl<ExpandoRow>
065            implements ExpandoRowPersistence {
066            /*
067             * NOTE FOR DEVELOPERS:
068             *
069             * Never modify or reference this class directly. Always use {@link ExpandoRowUtil} to access the expando row persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
070             */
071            public static final String FINDER_CLASS_NAME_ENTITY = ExpandoRowImpl.class.getName();
072            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
073                    ".List1";
074            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
075                    ".List2";
076            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
077                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByTableId",
079                            new String[] {
080                                    Long.class.getName(),
081                                    
082                            "java.lang.Integer", "java.lang.Integer",
083                                    "com.liferay.portal.kernel.util.OrderByComparator"
084                            });
085            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID =
086                    new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
087                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
088                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByTableId",
089                            new String[] { Long.class.getName() },
090                            ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK);
091            public static final FinderPath FINDER_PATH_COUNT_BY_TABLEID = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
092                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
093                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByTableId",
094                            new String[] { Long.class.getName() });
095            public static final FinderPath FINDER_PATH_FETCH_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
096                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
097                            FINDER_CLASS_NAME_ENTITY, "fetchByT_C",
098                            new String[] { Long.class.getName(), Long.class.getName() },
099                            ExpandoRowModelImpl.TABLEID_COLUMN_BITMASK |
100                            ExpandoRowModelImpl.CLASSPK_COLUMN_BITMASK);
101            public static final FinderPath FINDER_PATH_COUNT_BY_T_C = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
102                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
103                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByT_C",
104                            new String[] { Long.class.getName(), Long.class.getName() });
105            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
106                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
107                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
108            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
109                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, ExpandoRowImpl.class,
110                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
111            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
112                            ExpandoRowModelImpl.FINDER_CACHE_ENABLED, Long.class,
113                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
114    
115            /**
116             * Caches the expando row in the entity cache if it is enabled.
117             *
118             * @param expandoRow the expando row
119             */
120            public void cacheResult(ExpandoRow expandoRow) {
121                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
122                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
123    
124                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
125                            new Object[] {
126                                    Long.valueOf(expandoRow.getTableId()),
127                                    Long.valueOf(expandoRow.getClassPK())
128                            }, expandoRow);
129    
130                    expandoRow.resetOriginalValues();
131            }
132    
133            /**
134             * Caches the expando rows in the entity cache if it is enabled.
135             *
136             * @param expandoRows the expando rows
137             */
138            public void cacheResult(List<ExpandoRow> expandoRows) {
139                    for (ExpandoRow expandoRow : expandoRows) {
140                            if (EntityCacheUtil.getResult(
141                                                    ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
142                                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey()) == null) {
143                                    cacheResult(expandoRow);
144                            }
145                            else {
146                                    expandoRow.resetOriginalValues();
147                            }
148                    }
149            }
150    
151            /**
152             * Clears the cache for all expando rows.
153             *
154             * <p>
155             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
156             * </p>
157             */
158            @Override
159            public void clearCache() {
160                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
161                            CacheRegistryUtil.clear(ExpandoRowImpl.class.getName());
162                    }
163    
164                    EntityCacheUtil.clearCache(ExpandoRowImpl.class.getName());
165    
166                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
167                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
168                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
169            }
170    
171            /**
172             * Clears the cache for the expando row.
173             *
174             * <p>
175             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
176             * </p>
177             */
178            @Override
179            public void clearCache(ExpandoRow expandoRow) {
180                    EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
181                            ExpandoRowImpl.class, expandoRow.getPrimaryKey());
182    
183                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
184                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
185    
186                    clearUniqueFindersCache(expandoRow);
187            }
188    
189            @Override
190            public void clearCache(List<ExpandoRow> expandoRows) {
191                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
192                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
193    
194                    for (ExpandoRow expandoRow : expandoRows) {
195                            EntityCacheUtil.removeResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
196                                    ExpandoRowImpl.class, expandoRow.getPrimaryKey());
197    
198                            clearUniqueFindersCache(expandoRow);
199                    }
200            }
201    
202            protected void clearUniqueFindersCache(ExpandoRow expandoRow) {
203                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
204                            new Object[] {
205                                    Long.valueOf(expandoRow.getTableId()),
206                                    Long.valueOf(expandoRow.getClassPK())
207                            });
208            }
209    
210            /**
211             * Creates a new expando row with the primary key. Does not add the expando row to the database.
212             *
213             * @param rowId the primary key for the new expando row
214             * @return the new expando row
215             */
216            public ExpandoRow create(long rowId) {
217                    ExpandoRow expandoRow = new ExpandoRowImpl();
218    
219                    expandoRow.setNew(true);
220                    expandoRow.setPrimaryKey(rowId);
221    
222                    return expandoRow;
223            }
224    
225            /**
226             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
227             *
228             * @param rowId the primary key of the expando row
229             * @return the expando row that was removed
230             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
231             * @throws SystemException if a system exception occurred
232             */
233            public ExpandoRow remove(long rowId)
234                    throws NoSuchRowException, SystemException {
235                    return remove(Long.valueOf(rowId));
236            }
237    
238            /**
239             * Removes the expando row with the primary key from the database. Also notifies the appropriate model listeners.
240             *
241             * @param primaryKey the primary key of the expando row
242             * @return the expando row that was removed
243             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
244             * @throws SystemException if a system exception occurred
245             */
246            @Override
247            public ExpandoRow remove(Serializable primaryKey)
248                    throws NoSuchRowException, SystemException {
249                    Session session = null;
250    
251                    try {
252                            session = openSession();
253    
254                            ExpandoRow expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
255                                            primaryKey);
256    
257                            if (expandoRow == null) {
258                                    if (_log.isWarnEnabled()) {
259                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
260                                    }
261    
262                                    throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
263                                            primaryKey);
264                            }
265    
266                            return remove(expandoRow);
267                    }
268                    catch (NoSuchRowException nsee) {
269                            throw nsee;
270                    }
271                    catch (Exception e) {
272                            throw processException(e);
273                    }
274                    finally {
275                            closeSession(session);
276                    }
277            }
278    
279            @Override
280            protected ExpandoRow removeImpl(ExpandoRow expandoRow)
281                    throws SystemException {
282                    expandoRow = toUnwrappedModel(expandoRow);
283    
284                    Session session = null;
285    
286                    try {
287                            session = openSession();
288    
289                            if (expandoRow.isCachedModel()) {
290                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
291                                                    expandoRow.getPrimaryKeyObj());
292                            }
293    
294                            session.delete(expandoRow);
295                    }
296                    catch (Exception e) {
297                            throw processException(e);
298                    }
299                    finally {
300                            closeSession(session);
301                    }
302    
303                    clearCache(expandoRow);
304    
305                    return expandoRow;
306            }
307    
308            @Override
309            public ExpandoRow updateImpl(
310                    com.liferay.portlet.expando.model.ExpandoRow expandoRow)
311                    throws SystemException {
312                    expandoRow = toUnwrappedModel(expandoRow);
313    
314                    boolean isNew = expandoRow.isNew();
315    
316                    ExpandoRowModelImpl expandoRowModelImpl = (ExpandoRowModelImpl)expandoRow;
317    
318                    Session session = null;
319    
320                    try {
321                            session = openSession();
322    
323                            if (expandoRow.isNew()) {
324                                    session.save(expandoRow);
325    
326                                    expandoRow.setNew(false);
327                            }
328                            else {
329                                    session.merge(expandoRow);
330                            }
331                    }
332                    catch (Exception e) {
333                            throw processException(e);
334                    }
335                    finally {
336                            closeSession(session);
337                    }
338    
339                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
340    
341                    if (isNew || !ExpandoRowModelImpl.COLUMN_BITMASK_ENABLED) {
342                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
343                    }
344    
345                    else {
346                            if ((expandoRowModelImpl.getColumnBitmask() &
347                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID.getColumnBitmask()) != 0) {
348                                    Object[] args = new Object[] {
349                                                    Long.valueOf(expandoRowModelImpl.getOriginalTableId())
350                                            };
351    
352                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
353                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
354                                            args);
355    
356                                    args = new Object[] {
357                                                    Long.valueOf(expandoRowModelImpl.getTableId())
358                                            };
359    
360                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_TABLEID, args);
361                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID,
362                                            args);
363                            }
364                    }
365    
366                    EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
367                            ExpandoRowImpl.class, expandoRow.getPrimaryKey(), expandoRow);
368    
369                    if (isNew) {
370                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
371                                    new Object[] {
372                                            Long.valueOf(expandoRow.getTableId()),
373                                            Long.valueOf(expandoRow.getClassPK())
374                                    }, expandoRow);
375                    }
376                    else {
377                            if ((expandoRowModelImpl.getColumnBitmask() &
378                                            FINDER_PATH_FETCH_BY_T_C.getColumnBitmask()) != 0) {
379                                    Object[] args = new Object[] {
380                                                    Long.valueOf(expandoRowModelImpl.getOriginalTableId()),
381                                                    Long.valueOf(expandoRowModelImpl.getOriginalClassPK())
382                                            };
383    
384                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_T_C, args);
385    
386                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C, args);
387    
388                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
389                                            new Object[] {
390                                                    Long.valueOf(expandoRow.getTableId()),
391                                                    Long.valueOf(expandoRow.getClassPK())
392                                            }, expandoRow);
393                            }
394                    }
395    
396                    return expandoRow;
397            }
398    
399            protected ExpandoRow toUnwrappedModel(ExpandoRow expandoRow) {
400                    if (expandoRow instanceof ExpandoRowImpl) {
401                            return expandoRow;
402                    }
403    
404                    ExpandoRowImpl expandoRowImpl = new ExpandoRowImpl();
405    
406                    expandoRowImpl.setNew(expandoRow.isNew());
407                    expandoRowImpl.setPrimaryKey(expandoRow.getPrimaryKey());
408    
409                    expandoRowImpl.setRowId(expandoRow.getRowId());
410                    expandoRowImpl.setCompanyId(expandoRow.getCompanyId());
411                    expandoRowImpl.setModifiedDate(expandoRow.getModifiedDate());
412                    expandoRowImpl.setTableId(expandoRow.getTableId());
413                    expandoRowImpl.setClassPK(expandoRow.getClassPK());
414    
415                    return expandoRowImpl;
416            }
417    
418            /**
419             * Returns the expando row with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
420             *
421             * @param primaryKey the primary key of the expando row
422             * @return the expando row
423             * @throws com.liferay.portal.NoSuchModelException if a expando row with the primary key could not be found
424             * @throws SystemException if a system exception occurred
425             */
426            @Override
427            public ExpandoRow findByPrimaryKey(Serializable primaryKey)
428                    throws NoSuchModelException, SystemException {
429                    return findByPrimaryKey(((Long)primaryKey).longValue());
430            }
431    
432            /**
433             * Returns the expando row with the primary key or throws a {@link com.liferay.portlet.expando.NoSuchRowException} if it could not be found.
434             *
435             * @param rowId the primary key of the expando row
436             * @return the expando row
437             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
438             * @throws SystemException if a system exception occurred
439             */
440            public ExpandoRow findByPrimaryKey(long rowId)
441                    throws NoSuchRowException, SystemException {
442                    ExpandoRow expandoRow = fetchByPrimaryKey(rowId);
443    
444                    if (expandoRow == null) {
445                            if (_log.isWarnEnabled()) {
446                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + rowId);
447                            }
448    
449                            throw new NoSuchRowException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
450                                    rowId);
451                    }
452    
453                    return expandoRow;
454            }
455    
456            /**
457             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
458             *
459             * @param primaryKey the primary key of the expando row
460             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
461             * @throws SystemException if a system exception occurred
462             */
463            @Override
464            public ExpandoRow fetchByPrimaryKey(Serializable primaryKey)
465                    throws SystemException {
466                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
467            }
468    
469            /**
470             * Returns the expando row with the primary key or returns <code>null</code> if it could not be found.
471             *
472             * @param rowId the primary key of the expando row
473             * @return the expando row, or <code>null</code> if a expando row with the primary key could not be found
474             * @throws SystemException if a system exception occurred
475             */
476            public ExpandoRow fetchByPrimaryKey(long rowId) throws SystemException {
477                    ExpandoRow expandoRow = (ExpandoRow)EntityCacheUtil.getResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
478                                    ExpandoRowImpl.class, rowId);
479    
480                    if (expandoRow == _nullExpandoRow) {
481                            return null;
482                    }
483    
484                    if (expandoRow == null) {
485                            Session session = null;
486    
487                            boolean hasException = false;
488    
489                            try {
490                                    session = openSession();
491    
492                                    expandoRow = (ExpandoRow)session.get(ExpandoRowImpl.class,
493                                                    Long.valueOf(rowId));
494                            }
495                            catch (Exception e) {
496                                    hasException = true;
497    
498                                    throw processException(e);
499                            }
500                            finally {
501                                    if (expandoRow != null) {
502                                            cacheResult(expandoRow);
503                                    }
504                                    else if (!hasException) {
505                                            EntityCacheUtil.putResult(ExpandoRowModelImpl.ENTITY_CACHE_ENABLED,
506                                                    ExpandoRowImpl.class, rowId, _nullExpandoRow);
507                                    }
508    
509                                    closeSession(session);
510                            }
511                    }
512    
513                    return expandoRow;
514            }
515    
516            /**
517             * Returns all the expando rows where tableId = &#63;.
518             *
519             * @param tableId the table ID
520             * @return the matching expando rows
521             * @throws SystemException if a system exception occurred
522             */
523            public List<ExpandoRow> findByTableId(long tableId)
524                    throws SystemException {
525                    return findByTableId(tableId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
526            }
527    
528            /**
529             * Returns a range of all the expando rows where tableId = &#63;.
530             *
531             * <p>
532             * 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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
533             * </p>
534             *
535             * @param tableId the table ID
536             * @param start the lower bound of the range of expando rows
537             * @param end the upper bound of the range of expando rows (not inclusive)
538             * @return the range of matching expando rows
539             * @throws SystemException if a system exception occurred
540             */
541            public List<ExpandoRow> findByTableId(long tableId, int start, int end)
542                    throws SystemException {
543                    return findByTableId(tableId, start, end, null);
544            }
545    
546            /**
547             * Returns an ordered range of all the expando rows where tableId = &#63;.
548             *
549             * <p>
550             * 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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
551             * </p>
552             *
553             * @param tableId the table ID
554             * @param start the lower bound of the range of expando rows
555             * @param end the upper bound of the range of expando rows (not inclusive)
556             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
557             * @return the ordered range of matching expando rows
558             * @throws SystemException if a system exception occurred
559             */
560            public List<ExpandoRow> findByTableId(long tableId, int start, int end,
561                    OrderByComparator orderByComparator) throws SystemException {
562                    FinderPath finderPath = null;
563                    Object[] finderArgs = null;
564    
565                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
566                                    (orderByComparator == null)) {
567                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_TABLEID;
568                            finderArgs = new Object[] { tableId };
569                    }
570                    else {
571                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_TABLEID;
572                            finderArgs = new Object[] { tableId, start, end, orderByComparator };
573                    }
574    
575                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
576                                    finderArgs, this);
577    
578                    if ((list != null) && !list.isEmpty()) {
579                            for (ExpandoRow expandoRow : list) {
580                                    if ((tableId != expandoRow.getTableId())) {
581                                            list = null;
582    
583                                            break;
584                                    }
585                            }
586                    }
587    
588                    if (list == null) {
589                            StringBundler query = null;
590    
591                            if (orderByComparator != null) {
592                                    query = new StringBundler(3 +
593                                                    (orderByComparator.getOrderByFields().length * 3));
594                            }
595                            else {
596                                    query = new StringBundler(2);
597                            }
598    
599                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
600    
601                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
602    
603                            if (orderByComparator != null) {
604                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
605                                            orderByComparator);
606                            }
607    
608                            String sql = query.toString();
609    
610                            Session session = null;
611    
612                            try {
613                                    session = openSession();
614    
615                                    Query q = session.createQuery(sql);
616    
617                                    QueryPos qPos = QueryPos.getInstance(q);
618    
619                                    qPos.add(tableId);
620    
621                                    list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(), start,
622                                                    end);
623                            }
624                            catch (Exception e) {
625                                    throw processException(e);
626                            }
627                            finally {
628                                    if (list == null) {
629                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
630                                    }
631                                    else {
632                                            cacheResult(list);
633    
634                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
635                                    }
636    
637                                    closeSession(session);
638                            }
639                    }
640    
641                    return list;
642            }
643    
644            /**
645             * Returns the first expando row in the ordered set where tableId = &#63;.
646             *
647             * @param tableId the table ID
648             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
649             * @return the first matching expando row
650             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
651             * @throws SystemException if a system exception occurred
652             */
653            public ExpandoRow findByTableId_First(long tableId,
654                    OrderByComparator orderByComparator)
655                    throws NoSuchRowException, SystemException {
656                    ExpandoRow expandoRow = fetchByTableId_First(tableId, orderByComparator);
657    
658                    if (expandoRow != null) {
659                            return expandoRow;
660                    }
661    
662                    StringBundler msg = new StringBundler(4);
663    
664                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
665    
666                    msg.append("tableId=");
667                    msg.append(tableId);
668    
669                    msg.append(StringPool.CLOSE_CURLY_BRACE);
670    
671                    throw new NoSuchRowException(msg.toString());
672            }
673    
674            /**
675             * Returns the first expando row in the ordered set where tableId = &#63;.
676             *
677             * @param tableId the table ID
678             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
679             * @return the first matching expando row, or <code>null</code> if a matching expando row could not be found
680             * @throws SystemException if a system exception occurred
681             */
682            public ExpandoRow fetchByTableId_First(long tableId,
683                    OrderByComparator orderByComparator) throws SystemException {
684                    List<ExpandoRow> list = findByTableId(tableId, 0, 1, orderByComparator);
685    
686                    if (!list.isEmpty()) {
687                            return list.get(0);
688                    }
689    
690                    return null;
691            }
692    
693            /**
694             * Returns the last expando row in the ordered set where tableId = &#63;.
695             *
696             * @param tableId the table ID
697             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
698             * @return the last matching expando row
699             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
700             * @throws SystemException if a system exception occurred
701             */
702            public ExpandoRow findByTableId_Last(long tableId,
703                    OrderByComparator orderByComparator)
704                    throws NoSuchRowException, SystemException {
705                    ExpandoRow expandoRow = fetchByTableId_Last(tableId, orderByComparator);
706    
707                    if (expandoRow != null) {
708                            return expandoRow;
709                    }
710    
711                    StringBundler msg = new StringBundler(4);
712    
713                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
714    
715                    msg.append("tableId=");
716                    msg.append(tableId);
717    
718                    msg.append(StringPool.CLOSE_CURLY_BRACE);
719    
720                    throw new NoSuchRowException(msg.toString());
721            }
722    
723            /**
724             * Returns the last expando row in the ordered set where tableId = &#63;.
725             *
726             * @param tableId the table ID
727             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
728             * @return the last matching expando row, or <code>null</code> if a matching expando row could not be found
729             * @throws SystemException if a system exception occurred
730             */
731            public ExpandoRow fetchByTableId_Last(long tableId,
732                    OrderByComparator orderByComparator) throws SystemException {
733                    int count = countByTableId(tableId);
734    
735                    List<ExpandoRow> list = findByTableId(tableId, count - 1, count,
736                                    orderByComparator);
737    
738                    if (!list.isEmpty()) {
739                            return list.get(0);
740                    }
741    
742                    return null;
743            }
744    
745            /**
746             * Returns the expando rows before and after the current expando row in the ordered set where tableId = &#63;.
747             *
748             * @param rowId the primary key of the current expando row
749             * @param tableId the table ID
750             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
751             * @return the previous, current, and next expando row
752             * @throws com.liferay.portlet.expando.NoSuchRowException if a expando row with the primary key could not be found
753             * @throws SystemException if a system exception occurred
754             */
755            public ExpandoRow[] findByTableId_PrevAndNext(long rowId, long tableId,
756                    OrderByComparator orderByComparator)
757                    throws NoSuchRowException, SystemException {
758                    ExpandoRow expandoRow = findByPrimaryKey(rowId);
759    
760                    Session session = null;
761    
762                    try {
763                            session = openSession();
764    
765                            ExpandoRow[] array = new ExpandoRowImpl[3];
766    
767                            array[0] = getByTableId_PrevAndNext(session, expandoRow, tableId,
768                                            orderByComparator, true);
769    
770                            array[1] = expandoRow;
771    
772                            array[2] = getByTableId_PrevAndNext(session, expandoRow, tableId,
773                                            orderByComparator, false);
774    
775                            return array;
776                    }
777                    catch (Exception e) {
778                            throw processException(e);
779                    }
780                    finally {
781                            closeSession(session);
782                    }
783            }
784    
785            protected ExpandoRow getByTableId_PrevAndNext(Session session,
786                    ExpandoRow expandoRow, long tableId,
787                    OrderByComparator orderByComparator, boolean previous) {
788                    StringBundler query = null;
789    
790                    if (orderByComparator != null) {
791                            query = new StringBundler(6 +
792                                            (orderByComparator.getOrderByFields().length * 6));
793                    }
794                    else {
795                            query = new StringBundler(3);
796                    }
797    
798                    query.append(_SQL_SELECT_EXPANDOROW_WHERE);
799    
800                    query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
801    
802                    if (orderByComparator != null) {
803                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
804    
805                            if (orderByConditionFields.length > 0) {
806                                    query.append(WHERE_AND);
807                            }
808    
809                            for (int i = 0; i < orderByConditionFields.length; i++) {
810                                    query.append(_ORDER_BY_ENTITY_ALIAS);
811                                    query.append(orderByConditionFields[i]);
812    
813                                    if ((i + 1) < orderByConditionFields.length) {
814                                            if (orderByComparator.isAscending() ^ previous) {
815                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
816                                            }
817                                            else {
818                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
819                                            }
820                                    }
821                                    else {
822                                            if (orderByComparator.isAscending() ^ previous) {
823                                                    query.append(WHERE_GREATER_THAN);
824                                            }
825                                            else {
826                                                    query.append(WHERE_LESSER_THAN);
827                                            }
828                                    }
829                            }
830    
831                            query.append(ORDER_BY_CLAUSE);
832    
833                            String[] orderByFields = orderByComparator.getOrderByFields();
834    
835                            for (int i = 0; i < orderByFields.length; i++) {
836                                    query.append(_ORDER_BY_ENTITY_ALIAS);
837                                    query.append(orderByFields[i]);
838    
839                                    if ((i + 1) < orderByFields.length) {
840                                            if (orderByComparator.isAscending() ^ previous) {
841                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
842                                            }
843                                            else {
844                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
845                                            }
846                                    }
847                                    else {
848                                            if (orderByComparator.isAscending() ^ previous) {
849                                                    query.append(ORDER_BY_ASC);
850                                            }
851                                            else {
852                                                    query.append(ORDER_BY_DESC);
853                                            }
854                                    }
855                            }
856                    }
857    
858                    String sql = query.toString();
859    
860                    Query q = session.createQuery(sql);
861    
862                    q.setFirstResult(0);
863                    q.setMaxResults(2);
864    
865                    QueryPos qPos = QueryPos.getInstance(q);
866    
867                    qPos.add(tableId);
868    
869                    if (orderByComparator != null) {
870                            Object[] values = orderByComparator.getOrderByConditionValues(expandoRow);
871    
872                            for (Object value : values) {
873                                    qPos.add(value);
874                            }
875                    }
876    
877                    List<ExpandoRow> list = q.list();
878    
879                    if (list.size() == 2) {
880                            return list.get(1);
881                    }
882                    else {
883                            return null;
884                    }
885            }
886    
887            /**
888             * Returns the expando row where tableId = &#63; and classPK = &#63; or throws a {@link com.liferay.portlet.expando.NoSuchRowException} if it could not be found.
889             *
890             * @param tableId the table ID
891             * @param classPK the class p k
892             * @return the matching expando row
893             * @throws com.liferay.portlet.expando.NoSuchRowException if a matching expando row could not be found
894             * @throws SystemException if a system exception occurred
895             */
896            public ExpandoRow findByT_C(long tableId, long classPK)
897                    throws NoSuchRowException, SystemException {
898                    ExpandoRow expandoRow = fetchByT_C(tableId, classPK);
899    
900                    if (expandoRow == null) {
901                            StringBundler msg = new StringBundler(6);
902    
903                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
904    
905                            msg.append("tableId=");
906                            msg.append(tableId);
907    
908                            msg.append(", classPK=");
909                            msg.append(classPK);
910    
911                            msg.append(StringPool.CLOSE_CURLY_BRACE);
912    
913                            if (_log.isWarnEnabled()) {
914                                    _log.warn(msg.toString());
915                            }
916    
917                            throw new NoSuchRowException(msg.toString());
918                    }
919    
920                    return expandoRow;
921            }
922    
923            /**
924             * Returns the expando row where tableId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
925             *
926             * @param tableId the table ID
927             * @param classPK the class p k
928             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
929             * @throws SystemException if a system exception occurred
930             */
931            public ExpandoRow fetchByT_C(long tableId, long classPK)
932                    throws SystemException {
933                    return fetchByT_C(tableId, classPK, true);
934            }
935    
936            /**
937             * Returns the expando row where tableId = &#63; and classPK = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
938             *
939             * @param tableId the table ID
940             * @param classPK the class p k
941             * @param retrieveFromCache whether to use the finder cache
942             * @return the matching expando row, or <code>null</code> if a matching expando row could not be found
943             * @throws SystemException if a system exception occurred
944             */
945            public ExpandoRow fetchByT_C(long tableId, long classPK,
946                    boolean retrieveFromCache) throws SystemException {
947                    Object[] finderArgs = new Object[] { tableId, classPK };
948    
949                    Object result = null;
950    
951                    if (retrieveFromCache) {
952                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_T_C,
953                                            finderArgs, this);
954                    }
955    
956                    if (result instanceof ExpandoRow) {
957                            ExpandoRow expandoRow = (ExpandoRow)result;
958    
959                            if ((tableId != expandoRow.getTableId()) ||
960                                            (classPK != expandoRow.getClassPK())) {
961                                    result = null;
962                            }
963                    }
964    
965                    if (result == null) {
966                            StringBundler query = new StringBundler(3);
967    
968                            query.append(_SQL_SELECT_EXPANDOROW_WHERE);
969    
970                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
971    
972                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
973    
974                            String sql = query.toString();
975    
976                            Session session = null;
977    
978                            try {
979                                    session = openSession();
980    
981                                    Query q = session.createQuery(sql);
982    
983                                    QueryPos qPos = QueryPos.getInstance(q);
984    
985                                    qPos.add(tableId);
986    
987                                    qPos.add(classPK);
988    
989                                    List<ExpandoRow> list = q.list();
990    
991                                    result = list;
992    
993                                    ExpandoRow expandoRow = null;
994    
995                                    if (list.isEmpty()) {
996                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
997                                                    finderArgs, list);
998                                    }
999                                    else {
1000                                            expandoRow = list.get(0);
1001    
1002                                            cacheResult(expandoRow);
1003    
1004                                            if ((expandoRow.getTableId() != tableId) ||
1005                                                            (expandoRow.getClassPK() != classPK)) {
1006                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_T_C,
1007                                                            finderArgs, expandoRow);
1008                                            }
1009                                    }
1010    
1011                                    return expandoRow;
1012                            }
1013                            catch (Exception e) {
1014                                    throw processException(e);
1015                            }
1016                            finally {
1017                                    if (result == null) {
1018                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_T_C,
1019                                                    finderArgs);
1020                                    }
1021    
1022                                    closeSession(session);
1023                            }
1024                    }
1025                    else {
1026                            if (result instanceof List<?>) {
1027                                    return null;
1028                            }
1029                            else {
1030                                    return (ExpandoRow)result;
1031                            }
1032                    }
1033            }
1034    
1035            /**
1036             * Returns all the expando rows.
1037             *
1038             * @return the expando rows
1039             * @throws SystemException if a system exception occurred
1040             */
1041            public List<ExpandoRow> findAll() throws SystemException {
1042                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1043            }
1044    
1045            /**
1046             * Returns a range of all the expando rows.
1047             *
1048             * <p>
1049             * 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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1050             * </p>
1051             *
1052             * @param start the lower bound of the range of expando rows
1053             * @param end the upper bound of the range of expando rows (not inclusive)
1054             * @return the range of expando rows
1055             * @throws SystemException if a system exception occurred
1056             */
1057            public List<ExpandoRow> findAll(int start, int end)
1058                    throws SystemException {
1059                    return findAll(start, end, null);
1060            }
1061    
1062            /**
1063             * Returns an ordered range of all the expando rows.
1064             *
1065             * <p>
1066             * 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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1067             * </p>
1068             *
1069             * @param start the lower bound of the range of expando rows
1070             * @param end the upper bound of the range of expando rows (not inclusive)
1071             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1072             * @return the ordered range of expando rows
1073             * @throws SystemException if a system exception occurred
1074             */
1075            public List<ExpandoRow> findAll(int start, int end,
1076                    OrderByComparator orderByComparator) throws SystemException {
1077                    FinderPath finderPath = null;
1078                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
1079    
1080                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1081                                    (orderByComparator == null)) {
1082                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1083                            finderArgs = FINDER_ARGS_EMPTY;
1084                    }
1085                    else {
1086                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1087                            finderArgs = new Object[] { start, end, orderByComparator };
1088                    }
1089    
1090                    List<ExpandoRow> list = (List<ExpandoRow>)FinderCacheUtil.getResult(finderPath,
1091                                    finderArgs, this);
1092    
1093                    if (list == null) {
1094                            StringBundler query = null;
1095                            String sql = null;
1096    
1097                            if (orderByComparator != null) {
1098                                    query = new StringBundler(2 +
1099                                                    (orderByComparator.getOrderByFields().length * 3));
1100    
1101                                    query.append(_SQL_SELECT_EXPANDOROW);
1102    
1103                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1104                                            orderByComparator);
1105    
1106                                    sql = query.toString();
1107                            }
1108                            else {
1109                                    sql = _SQL_SELECT_EXPANDOROW;
1110                            }
1111    
1112                            Session session = null;
1113    
1114                            try {
1115                                    session = openSession();
1116    
1117                                    Query q = session.createQuery(sql);
1118    
1119                                    if (orderByComparator == null) {
1120                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1121                                                            start, end, false);
1122    
1123                                            Collections.sort(list);
1124                                    }
1125                                    else {
1126                                            list = (List<ExpandoRow>)QueryUtil.list(q, getDialect(),
1127                                                            start, end);
1128                                    }
1129                            }
1130                            catch (Exception e) {
1131                                    throw processException(e);
1132                            }
1133                            finally {
1134                                    if (list == null) {
1135                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
1136                                    }
1137                                    else {
1138                                            cacheResult(list);
1139    
1140                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
1141                                    }
1142    
1143                                    closeSession(session);
1144                            }
1145                    }
1146    
1147                    return list;
1148            }
1149    
1150            /**
1151             * Removes all the expando rows where tableId = &#63; from the database.
1152             *
1153             * @param tableId the table ID
1154             * @throws SystemException if a system exception occurred
1155             */
1156            public void removeByTableId(long tableId) throws SystemException {
1157                    for (ExpandoRow expandoRow : findByTableId(tableId)) {
1158                            remove(expandoRow);
1159                    }
1160            }
1161    
1162            /**
1163             * Removes the expando row where tableId = &#63; and classPK = &#63; from the database.
1164             *
1165             * @param tableId the table ID
1166             * @param classPK the class p k
1167             * @return the expando row that was removed
1168             * @throws SystemException if a system exception occurred
1169             */
1170            public ExpandoRow removeByT_C(long tableId, long classPK)
1171                    throws NoSuchRowException, SystemException {
1172                    ExpandoRow expandoRow = findByT_C(tableId, classPK);
1173    
1174                    return remove(expandoRow);
1175            }
1176    
1177            /**
1178             * Removes all the expando rows from the database.
1179             *
1180             * @throws SystemException if a system exception occurred
1181             */
1182            public void removeAll() throws SystemException {
1183                    for (ExpandoRow expandoRow : findAll()) {
1184                            remove(expandoRow);
1185                    }
1186            }
1187    
1188            /**
1189             * Returns the number of expando rows where tableId = &#63;.
1190             *
1191             * @param tableId the table ID
1192             * @return the number of matching expando rows
1193             * @throws SystemException if a system exception occurred
1194             */
1195            public int countByTableId(long tableId) throws SystemException {
1196                    Object[] finderArgs = new Object[] { tableId };
1197    
1198                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_TABLEID,
1199                                    finderArgs, this);
1200    
1201                    if (count == null) {
1202                            StringBundler query = new StringBundler(2);
1203    
1204                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1205    
1206                            query.append(_FINDER_COLUMN_TABLEID_TABLEID_2);
1207    
1208                            String sql = query.toString();
1209    
1210                            Session session = null;
1211    
1212                            try {
1213                                    session = openSession();
1214    
1215                                    Query q = session.createQuery(sql);
1216    
1217                                    QueryPos qPos = QueryPos.getInstance(q);
1218    
1219                                    qPos.add(tableId);
1220    
1221                                    count = (Long)q.uniqueResult();
1222                            }
1223                            catch (Exception e) {
1224                                    throw processException(e);
1225                            }
1226                            finally {
1227                                    if (count == null) {
1228                                            count = Long.valueOf(0);
1229                                    }
1230    
1231                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_TABLEID,
1232                                            finderArgs, count);
1233    
1234                                    closeSession(session);
1235                            }
1236                    }
1237    
1238                    return count.intValue();
1239            }
1240    
1241            /**
1242             * Returns the number of expando rows where tableId = &#63; and classPK = &#63;.
1243             *
1244             * @param tableId the table ID
1245             * @param classPK the class p k
1246             * @return the number of matching expando rows
1247             * @throws SystemException if a system exception occurred
1248             */
1249            public int countByT_C(long tableId, long classPK) throws SystemException {
1250                    Object[] finderArgs = new Object[] { tableId, classPK };
1251    
1252                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_T_C,
1253                                    finderArgs, this);
1254    
1255                    if (count == null) {
1256                            StringBundler query = new StringBundler(3);
1257    
1258                            query.append(_SQL_COUNT_EXPANDOROW_WHERE);
1259    
1260                            query.append(_FINDER_COLUMN_T_C_TABLEID_2);
1261    
1262                            query.append(_FINDER_COLUMN_T_C_CLASSPK_2);
1263    
1264                            String sql = query.toString();
1265    
1266                            Session session = null;
1267    
1268                            try {
1269                                    session = openSession();
1270    
1271                                    Query q = session.createQuery(sql);
1272    
1273                                    QueryPos qPos = QueryPos.getInstance(q);
1274    
1275                                    qPos.add(tableId);
1276    
1277                                    qPos.add(classPK);
1278    
1279                                    count = (Long)q.uniqueResult();
1280                            }
1281                            catch (Exception e) {
1282                                    throw processException(e);
1283                            }
1284                            finally {
1285                                    if (count == null) {
1286                                            count = Long.valueOf(0);
1287                                    }
1288    
1289                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_T_C, finderArgs,
1290                                            count);
1291    
1292                                    closeSession(session);
1293                            }
1294                    }
1295    
1296                    return count.intValue();
1297            }
1298    
1299            /**
1300             * Returns the number of expando rows.
1301             *
1302             * @return the number of expando rows
1303             * @throws SystemException if a system exception occurred
1304             */
1305            public int countAll() throws SystemException {
1306                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1307                                    FINDER_ARGS_EMPTY, this);
1308    
1309                    if (count == null) {
1310                            Session session = null;
1311    
1312                            try {
1313                                    session = openSession();
1314    
1315                                    Query q = session.createQuery(_SQL_COUNT_EXPANDOROW);
1316    
1317                                    count = (Long)q.uniqueResult();
1318                            }
1319                            catch (Exception e) {
1320                                    throw processException(e);
1321                            }
1322                            finally {
1323                                    if (count == null) {
1324                                            count = Long.valueOf(0);
1325                                    }
1326    
1327                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1328                                            FINDER_ARGS_EMPTY, count);
1329    
1330                                    closeSession(session);
1331                            }
1332                    }
1333    
1334                    return count.intValue();
1335            }
1336    
1337            /**
1338             * Initializes the expando row persistence.
1339             */
1340            public void afterPropertiesSet() {
1341                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1342                                            com.liferay.portal.util.PropsUtil.get(
1343                                                    "value.object.listener.com.liferay.portlet.expando.model.ExpandoRow")));
1344    
1345                    if (listenerClassNames.length > 0) {
1346                            try {
1347                                    List<ModelListener<ExpandoRow>> listenersList = new ArrayList<ModelListener<ExpandoRow>>();
1348    
1349                                    for (String listenerClassName : listenerClassNames) {
1350                                            listenersList.add((ModelListener<ExpandoRow>)InstanceFactory.newInstance(
1351                                                            listenerClassName));
1352                                    }
1353    
1354                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1355                            }
1356                            catch (Exception e) {
1357                                    _log.error(e);
1358                            }
1359                    }
1360            }
1361    
1362            public void destroy() {
1363                    EntityCacheUtil.removeCache(ExpandoRowImpl.class.getName());
1364                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
1365                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
1366            }
1367    
1368            @BeanReference(type = ExpandoColumnPersistence.class)
1369            protected ExpandoColumnPersistence expandoColumnPersistence;
1370            @BeanReference(type = ExpandoRowPersistence.class)
1371            protected ExpandoRowPersistence expandoRowPersistence;
1372            @BeanReference(type = ExpandoTablePersistence.class)
1373            protected ExpandoTablePersistence expandoTablePersistence;
1374            @BeanReference(type = ExpandoValuePersistence.class)
1375            protected ExpandoValuePersistence expandoValuePersistence;
1376            @BeanReference(type = UserPersistence.class)
1377            protected UserPersistence userPersistence;
1378            private static final String _SQL_SELECT_EXPANDOROW = "SELECT expandoRow FROM ExpandoRow expandoRow";
1379            private static final String _SQL_SELECT_EXPANDOROW_WHERE = "SELECT expandoRow FROM ExpandoRow expandoRow WHERE ";
1380            private static final String _SQL_COUNT_EXPANDOROW = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow";
1381            private static final String _SQL_COUNT_EXPANDOROW_WHERE = "SELECT COUNT(expandoRow) FROM ExpandoRow expandoRow WHERE ";
1382            private static final String _FINDER_COLUMN_TABLEID_TABLEID_2 = "expandoRow.tableId = ?";
1383            private static final String _FINDER_COLUMN_T_C_TABLEID_2 = "expandoRow.tableId = ? AND ";
1384            private static final String _FINDER_COLUMN_T_C_CLASSPK_2 = "expandoRow.classPK = ?";
1385            private static final String _ORDER_BY_ENTITY_ALIAS = "expandoRow.";
1386            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ExpandoRow exists with the primary key ";
1387            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ExpandoRow exists with the key {";
1388            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
1389            private static Log _log = LogFactoryUtil.getLog(ExpandoRowPersistenceImpl.class);
1390            private static ExpandoRow _nullExpandoRow = new ExpandoRowImpl() {
1391                            @Override
1392                            public Object clone() {
1393                                    return this;
1394                            }
1395    
1396                            @Override
1397                            public CacheModel<ExpandoRow> toCacheModel() {
1398                                    return _nullExpandoRowCacheModel;
1399                            }
1400                    };
1401    
1402            private static CacheModel<ExpandoRow> _nullExpandoRowCacheModel = new CacheModel<ExpandoRow>() {
1403                            public ExpandoRow toEntityModel() {
1404                                    return _nullExpandoRow;
1405                            }
1406                    };
1407    }