001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016// Generated File -- DO NOT EDIT BY HAND
017// --------------------------------------------------------------------------------
018
019/**
020 * CommentBlobFactory.java
021 */
022
023package com.echothree.model.data.comment.server.factory;
024
025import com.echothree.model.data.comment.common.pk.CommentPK;
026
027import com.echothree.model.data.comment.server.entity.Comment;
028
029import com.echothree.model.data.comment.common.CommentBlobConstants;
030import com.echothree.model.data.comment.common.pk.CommentBlobPK;
031import com.echothree.model.data.comment.server.value.CommentBlobValue;
032import com.echothree.model.data.comment.server.entity.CommentBlob;
033import com.echothree.util.common.exception.PersistenceDatabaseException;
034import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
035import com.echothree.util.common.exception.PersistenceNotNullException;
036import com.echothree.util.server.persistence.BaseFactory;
037import com.echothree.util.server.persistence.EntityIdGenerator;
038import com.echothree.util.server.persistence.EntityPermission;
039import com.echothree.util.server.persistence.PersistenceDebugFlags;
040import com.echothree.util.server.persistence.Session;
041import com.echothree.util.common.persistence.type.ByteArray;
042import java.sql.Blob;
043import java.sql.PreparedStatement;
044import java.sql.ResultSet;
045import java.sql.SQLException;
046import java.sql.Types;
047import java.io.ByteArrayInputStream;
048import java.io.StringReader;
049import java.util.ArrayList;
050import java.util.Collection;
051import java.util.HashSet;
052import java.util.List;
053import java.util.Map;
054import java.util.Set;
055import javax.enterprise.context.ApplicationScoped;
056import javax.enterprise.inject.spi.CDI;
057import javax.inject.Inject;
058import org.jooq.ResultQuery;
059import org.slf4j.Logger;
060import org.slf4j.LoggerFactory;
061
062@ApplicationScoped
063public class CommentBlobFactory
064        implements BaseFactory<CommentBlobPK, CommentBlob> {
065    
066    @Inject
067    Session session;
068    
069    //private static final Logger log = LoggerFactory.getLogger(CommentBlobFactory.class);
070    
071    final private static String SQL_SELECT_READ_ONLY = "SELECT cmntb_commentblobid, cmntb_cmnt_commentid, cmntb_blob, cmntb_fromtime, cmntb_thrutime FROM commentblobs WHERE cmntb_commentblobid = ?";
072    final private static String SQL_SELECT_READ_WRITE = "SELECT cmntb_commentblobid, cmntb_cmnt_commentid, cmntb_blob, cmntb_fromtime, cmntb_thrutime FROM commentblobs WHERE cmntb_commentblobid = ? FOR UPDATE";
073    final private static String SQL_INSERT = "INSERT INTO commentblobs (cmntb_commentblobid, cmntb_cmnt_commentid, cmntb_blob, cmntb_fromtime, cmntb_thrutime) VALUES (?, ?, ?, ?, ?)";
074    final private static String SQL_UPDATE = "UPDATE commentblobs SET cmntb_cmnt_commentid = ?, cmntb_blob = ?, cmntb_fromtime = ?, cmntb_thrutime = ? WHERE cmntb_commentblobid = ?";
075    final private static String SQL_DELETE = "DELETE FROM commentblobs WHERE cmntb_commentblobid = ?";
076    final private static String SQL_VALID = "SELECT COUNT(*) FROM commentblobs WHERE cmntb_commentblobid = ?";
077    
078    final private static String PK_COLUMN = "cmntb_commentblobid";
079    final private static String ALL_COLUMNS = "cmntb_commentblobid, cmntb_cmnt_commentid, cmntb_blob, cmntb_fromtime, cmntb_thrutime";
080    final public static String TABLE_NAME = "commentblobs";
081    
082    final public static String CMNTB_COMMENTBLOBID = "cmntb_commentblobid";
083    final public static String CMNTB_CMNT_COMMENTID = "cmntb_cmnt_commentid";
084    final public static String CMNTB_BLOB = "cmntb_blob";
085    final public static String CMNTB_FROMTIME = "cmntb_fromtime";
086    final public static String CMNTB_THRUTIME = "cmntb_thrutime";
087    
088    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(CommentBlobConstants.COMPONENT_VENDOR_NAME, CommentBlobConstants.ENTITY_TYPE_NAME);
089    
090    /** Creates a new instance of CommentBlobFactory */
091    protected CommentBlobFactory() {
092        super();
093    }
094    
095    public static CommentBlobFactory getInstance() {
096        return CDI.current().select(CommentBlobFactory.class).get();
097    }
098    
099    @Override
100    public String getPKColumn() {
101        return PK_COLUMN;
102    }
103    
104    @Override
105    public String getAllColumns() {
106        return ALL_COLUMNS;
107    }
108    
109    @Override
110    public String getTableName() {
111        return TABLE_NAME;
112    }
113    
114    @Override
115    public String getComponentVendorName() {
116        return CommentBlobConstants.COMPONENT_VENDOR_NAME;
117    }
118    
119    @Override
120    public String getEntityTypeName() {
121        return CommentBlobConstants.ENTITY_TYPE_NAME;
122    }
123    
124    public PreparedStatement prepareStatement(String query) {
125        return session.prepareStatement(CommentBlobFactory.class, query);
126    }
127    
128    public CommentBlobPK getNextPK() {
129        return new CommentBlobPK(entityIdGenerator.getNextEntityId());
130    }
131    
132    public Set<CommentBlobPK> getPKsFromResultSetAsSet(ResultSet rs)
133            throws PersistenceDatabaseException {
134        Set<CommentBlobPK> _result = new HashSet<>();
135        
136        try {
137            while(rs.next()) {
138                _result.add(getPKFromResultSet(rs));
139            }
140        } catch (SQLException se) {
141            throw new PersistenceDatabaseException(se);
142        }
143        
144        return _result;
145    }
146    
147    public java.util.List<CommentBlobPK> getPKsFromResultSetAsList(ResultSet rs)
148            throws PersistenceDatabaseException {
149        java.util.List<CommentBlobPK> _result = new ArrayList<>();
150        
151        try {
152            while(rs.next()) {
153                _result.add(getPKFromResultSet(rs));
154            }
155        } catch (SQLException se) {
156            throw new PersistenceDatabaseException(se);
157        }
158        
159        return _result;
160    }
161    
162    public CommentBlobPK getPKFromResultSet(ResultSet rs)
163            throws PersistenceDatabaseException {
164        CommentBlobPK _result;
165        
166        try {
167            long cmntb_commentblobid = rs.getLong(CMNTB_COMMENTBLOBID);
168            Long _entityId = rs.wasNull() ? null : cmntb_commentblobid;
169            
170            _result = new CommentBlobPK(_entityId);
171        } catch (SQLException se) {
172            throw new PersistenceDatabaseException(se);
173        }
174        
175        return _result;
176    }
177    
178    public java.util.List<CommentBlobValue> getValuesFromPKs(Collection<CommentBlobPK> pks)
179            throws PersistenceDatabaseException {
180        java.util.List<CommentBlobValue> _values = new ArrayList<>(pks.size());
181        
182        for(CommentBlobPK _pk: pks) {
183            _values.add(getValueFromPK(_pk));
184        }
185        
186        return _values;
187    }
188    
189    public CommentBlobValue getValueFromPK(CommentBlobPK pk)
190            throws PersistenceDatabaseException {
191        CommentBlobValue _value;
192        
193        // See if we already have the entity in the session cache
194        CommentBlob _entity = (CommentBlob)session.getEntity(pk);
195        if(_entity == null)
196            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getCommentBlobValue();
197        else
198            _value = _entity.getCommentBlobValue();
199        
200        return _value;
201    }
202    
203    public java.util.List<CommentBlobValue> getValuesFromResultSet(ResultSet rs)
204            throws PersistenceDatabaseException {
205        java.util.List<CommentBlobValue> _result = new ArrayList<>();
206        
207        try {
208            while(rs.next()) {
209                _result.add(getValueFromResultSet(rs));
210            }
211        } catch (SQLException se) {
212            throw new PersistenceDatabaseException(se);
213        }
214        
215        return _result;
216    }
217    
218    public CommentBlobValue getValueFromResultSet(ResultSet rs)
219            throws PersistenceDatabaseException {
220        CommentBlobValue _value;
221        
222        try {
223            Long cmntb_commentblobid = rs.getLong(CMNTB_COMMENTBLOBID);
224            CommentBlobPK _pk = new CommentBlobPK(cmntb_commentblobid);
225            
226            // See if we already have the entity in the session cache
227            CommentBlob _entity = (CommentBlob)session.getEntity(_pk);
228            
229            if(_entity == null) {
230                Long cmntb_cmnt_commentid = rs.getLong(CMNTB_CMNT_COMMENTID);
231                if(rs.wasNull())
232                    cmntb_cmnt_commentid = null;
233                
234                Blob cmntb_blob = rs.getBlob(CMNTB_BLOB);
235                if(rs.wasNull())
236                    cmntb_blob = null;
237                
238                Long cmntb_fromtime = rs.getLong(CMNTB_FROMTIME);
239                if(rs.wasNull())
240                    cmntb_fromtime = null;
241                
242                Long cmntb_thrutime = rs.getLong(CMNTB_THRUTIME);
243                if(rs.wasNull())
244                    cmntb_thrutime = null;
245                
246                _value = new CommentBlobValue(_pk, new CommentPK(cmntb_cmnt_commentid), new ByteArray(cmntb_blob.getBytes(1L, (int)cmntb_blob.length())), cmntb_fromtime, cmntb_thrutime);
247            } else
248                _value = _entity.getCommentBlobValue();
249        } catch (SQLException se) {
250            throw new PersistenceDatabaseException(se);
251        }
252        
253        return _value;
254    }
255    
256    public java.util.List<CommentBlob> getEntitiesFromPKs(EntityPermission entityPermission, Collection<CommentBlobPK> pks)
257            throws PersistenceDatabaseException {
258        java.util.List<CommentBlob> _entities = new ArrayList<>(pks.size());
259        
260        for(CommentBlobPK _pk: pks) {
261            _entities.add(getEntityFromPK(entityPermission, _pk));
262        }
263        
264        return _entities;
265    }
266    
267    public CommentBlob getEntityFromValue(EntityPermission entityPermission, CommentBlobValue value) {
268        return getEntityFromPK(entityPermission, value.getPrimaryKey());
269    }
270    
271    public CommentBlob getEntityFromCache(CommentBlobPK pk) {
272        CommentBlobValue _value = (CommentBlobValue)session.getValueCache().get(pk);
273    
274        return _value == null ? null : new CommentBlob(_value, EntityPermission.READ_ONLY);
275    }
276    
277    public CommentBlob getEntityFromPK(EntityPermission entityPermission, CommentBlobPK pk)
278            throws PersistenceDatabaseException {
279        CommentBlob _entity;
280        
281        // See if we already have the entity in the session cache
282        _entity = (CommentBlob)session.getEntity(pk);
283        if(_entity != null) {
284            // If the requested permission is READ_WRITE, and the cached permission is
285            // READ_ONLY, then pretend that the cached object wasn't found, and create
286            // a new entity that is READ_WRITE.
287            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
288                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
289                    _entity = null;
290            }
291        }
292        
293        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
294            _entity = getEntityFromCache(pk);
295        }
296        
297        if(_entity == null) {
298            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
299            long _entityId = pk.getEntityId();
300            ResultSet _rs = null;
301            
302            try {
303                _ps.setLong(1, _entityId);
304                _rs = _ps.executeQuery();
305                if(_rs.next()) {
306                    _entity = getEntityFromResultSet(entityPermission, _rs);
307                }
308            } catch (SQLException se) {
309                throw new PersistenceDatabaseException(se);
310            } finally {
311                if(_rs != null) {
312                    try {
313                        _rs.close();
314                    } catch (SQLException se) {
315                        // do nothing
316                    }
317                }
318            }
319        }
320        
321        return _entity;
322    }
323    
324    public Set<CommentBlobPK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
325            throws PersistenceDatabaseException {
326        Set<CommentBlobPK> _pks;
327        ResultSet _rs = null;
328        
329        try {
330            if(params.length != 0) {
331                Session.setQueryParams(ps, params);
332            }
333            
334            _rs = ps.executeQuery();
335            _pks = getPKsFromResultSetAsSet(_rs);
336            _rs.close();
337        } catch (SQLException se) {
338            throw new PersistenceDatabaseException(se);
339        } finally {
340            if(_rs != null) {
341                try {
342                    _rs.close();
343                } catch (SQLException se) {
344                    // do nothing
345                }
346            }
347        }
348        
349        return _pks;
350    }
351    
352    public java.util.List<CommentBlobPK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
353            throws PersistenceDatabaseException {
354        java.util.List<CommentBlobPK> _pks;
355        ResultSet _rs = null;
356        
357        try {
358            if(params.length != 0) {
359                Session.setQueryParams(ps, params);
360            }
361            
362            _rs = ps.executeQuery();
363            _pks = getPKsFromResultSetAsList(_rs);
364            _rs.close();
365        } catch (SQLException se) {
366            throw new PersistenceDatabaseException(se);
367        } finally {
368            if(_rs != null) {
369                try {
370                    _rs.close();
371                } catch (SQLException se) {
372                    // do nothing
373                }
374            }
375        }
376        
377        return _pks;
378    }
379    
380    public CommentBlobPK getPKFromQuery(PreparedStatement ps, final Object... params)
381            throws PersistenceDatabaseException {
382        CommentBlobPK _pk = null;
383        ResultSet _rs = null;
384        
385        try {
386            if(params.length != 0) {
387                Session.setQueryParams(ps, params);
388            }
389            
390            _rs = ps.executeQuery();
391            if(_rs.next()) {
392                _pk = getPKFromResultSet(_rs);
393            }
394            _rs.close();
395        } catch (SQLException se) {
396            throw new PersistenceDatabaseException(se);
397        } finally {
398            if(_rs != null) {
399                try {
400                    _rs.close();
401                } catch (SQLException se) {
402                    // do nothing
403                }
404            }
405        }
406        
407        return _pk;
408    }
409    
410    public java.util.List<CommentBlob> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
411            throws PersistenceDatabaseException {
412        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, queryMap.get(entityPermission));
413        
414        return getEntitiesFromQuery(entityPermission, ps, params);
415    }
416    
417    public java.util.List<CommentBlob> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
418            throws PersistenceDatabaseException {
419        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, queryMap.get(entityPermission));
420        
421        return getEntitiesFromQuery(entityPermission, ps);
422    }
423    
424    public java.util.List<CommentBlob> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
425            throws PersistenceDatabaseException {
426        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, query.getSQL());
427        
428        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
429    }
430    
431    public java.util.List<CommentBlob> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
432            throws PersistenceDatabaseException {
433        java.util.List<CommentBlob> _entities;
434        ResultSet _rs = null;
435        
436        try {
437            if(params.length != 0) {
438                Session.setQueryParams(ps, params);
439            }
440            
441            _rs = ps.executeQuery();
442            _entities = getEntitiesFromResultSet(entityPermission, _rs);
443            _rs.close();
444        } catch (SQLException se) {
445            throw new PersistenceDatabaseException(se);
446        } finally {
447            if(_rs != null) {
448                try {
449                    _rs.close();
450                } catch (SQLException se) {
451                    // do nothing
452                }
453            }
454        }
455        
456        return _entities;
457    }
458    
459    public CommentBlob getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
460            throws PersistenceDatabaseException {
461        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, queryMap.get(entityPermission));
462        
463        return getEntityFromQuery(entityPermission, ps, params);
464    }
465    
466    public CommentBlob getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
467            throws PersistenceDatabaseException {
468        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, queryMap.get(entityPermission));
469        
470        return getEntityFromQuery(entityPermission, ps);
471    }
472    
473    public CommentBlob getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
474            throws PersistenceDatabaseException {
475        PreparedStatement ps = session.prepareStatement(CommentBlobFactory.class, query.getSQL());
476        
477        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
478    }
479    
480    public CommentBlob getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
481            throws PersistenceDatabaseException {
482        CommentBlob _entity = null;
483        ResultSet _rs = null;
484        
485        try {
486            if(params.length != 0) {
487                Session.setQueryParams(ps, params);
488            }
489            
490            _rs = ps.executeQuery();
491            if(_rs.next()) {
492                _entity = getEntityFromResultSet(entityPermission, _rs);
493            }
494            _rs.close();
495        } catch (SQLException se) {
496            throw new PersistenceDatabaseException(se);
497        } finally {
498            if(_rs != null) {
499                try {
500                    _rs.close();
501                } catch (SQLException se) {
502                    // do nothing
503                }
504            }
505        }
506        
507        return _entity;
508    }
509    
510    public java.util.List<CommentBlob> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
511            throws PersistenceDatabaseException {
512        java.util.List<CommentBlob> _result = new ArrayList<>();
513        
514        try {
515            while(rs.next()) {
516                _result.add(getEntityFromResultSet(entityPermission, rs));
517            }
518        } catch (SQLException se) {
519            throw new PersistenceDatabaseException(se);
520        }
521        
522        return _result;
523    }
524    
525    public CommentBlob getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
526            throws PersistenceDatabaseException {
527        CommentBlob _entity;
528        
529        try {
530            Long cmntb_commentblobid = rs.getLong(CMNTB_COMMENTBLOBID);
531            CommentBlobPK _pk = new CommentBlobPK(cmntb_commentblobid);
532            
533            // See if we already have the entity in the session cache
534            _entity = (CommentBlob)session.getEntity(_pk);
535            if(_entity != null) {
536                // If the requested permission is READ_WRITE, and the cached permission is
537                // READ_ONLY, then pretend that the cached object wasn't found, and create
538                // a new entity that is READ_WRITE.
539                if(entityPermission.equals(EntityPermission.READ_WRITE)) {
540                    if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
541                        _entity = null;
542                }
543            }
544            boolean foundInSessionCache = _entity != null;
545            
546            if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
547                _entity = getEntityFromCache(_pk);
548            }
549            
550            if(_entity == null) {
551                Long cmntb_cmnt_commentid = rs.getLong(CMNTB_CMNT_COMMENTID);
552                if(rs.wasNull())
553                    cmntb_cmnt_commentid = null;
554                
555                Blob cmntb_blob = rs.getBlob(CMNTB_BLOB);
556                if(rs.wasNull())
557                    cmntb_blob = null;
558                
559                Long cmntb_fromtime = rs.getLong(CMNTB_FROMTIME);
560                if(rs.wasNull())
561                    cmntb_fromtime = null;
562                
563                Long cmntb_thrutime = rs.getLong(CMNTB_THRUTIME);
564                if(rs.wasNull())
565                    cmntb_thrutime = null;
566                
567                CommentBlobValue _value = new CommentBlobValue(_pk, cmntb_cmnt_commentid == null? null: new CommentPK(cmntb_cmnt_commentid), new ByteArray(cmntb_blob.getBytes(1L, (int)cmntb_blob.length())), cmntb_fromtime, cmntb_thrutime);
568                _entity = new CommentBlob(_value, entityPermission);
569            }
570            
571            if(!foundInSessionCache) {
572                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
573                    session.putReadOnlyEntity(_pk, _entity);
574                    session.getValueCache().put(_entity.getCommentBlobValue());
575                } else {
576                    session.putReadWriteEntity(_pk, _entity);
577                }
578            }
579        } catch (SQLException se) {
580            throw new PersistenceDatabaseException(se);
581        }
582        
583        return _entity;
584    }
585    
586    public CommentBlob create(Comment comment, ByteArray blob, Long fromTime, Long thruTime)
587            throws PersistenceDatabaseException, PersistenceNotNullException {
588        return create(comment == null ? null : comment.getPrimaryKey(), blob, fromTime, thruTime);
589    }
590    
591    private void bindForCreate(PreparedStatement _ps, CommentBlobValue _value)
592            throws SQLException {
593        _ps.setLong(1, _value.getEntityId());
594        
595        CommentPK cmntb_cmnt_commentid = _value.getCommentPK();
596        if(cmntb_cmnt_commentid == null)
597            _ps.setNull(2, Types.BIGINT);
598        else
599            _ps.setLong(2, cmntb_cmnt_commentid.getEntityId());
600            
601        ByteArray cmntb_blob = _value.getBlob();
602        if(cmntb_blob == null)
603            _ps.setNull(3, Types.BLOB);
604        else
605            _ps.setBinaryStream(3, new ByteArrayInputStream(cmntb_blob.byteArrayValue()), cmntb_blob.length());
606            
607        Long cmntb_fromtime = _value.getFromTime();
608        if(cmntb_fromtime == null)
609            _ps.setNull(4, Types.BIGINT);
610        else
611            _ps.setLong(4, cmntb_fromtime);
612            
613        Long cmntb_thrutime = _value.getThruTime();
614        if(cmntb_thrutime == null)
615            _ps.setNull(5, Types.BIGINT);
616        else
617            _ps.setLong(5, cmntb_thrutime);
618            
619    }
620    
621    public CommentBlob create(CommentPK commentPK, ByteArray blob, Long fromTime, Long thruTime)
622            throws PersistenceDatabaseException, PersistenceNotNullException {
623        CommentBlobPK _pk = getNextPK();
624        CommentBlobValue _value = new CommentBlobValue(_pk, commentPK, blob, fromTime, thruTime);
625        
626        PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
627        
628        try {
629            bindForCreate(_ps, _value);
630            
631            if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
632                int _count = _ps.executeUpdate();
633                
634                if(_count != 1) {
635                    throw new PersistenceDatabaseUpdateException("insert failed, _count = " + _count);
636                }
637            } else {
638                 _ps.executeUpdate();
639            }
640            
641            session.getValueCache().put(_value);
642        } catch (SQLException se) {
643            throw new PersistenceDatabaseException(se);
644        }
645        
646        CommentBlob _entity = new CommentBlob(_value, EntityPermission.READ_ONLY);
647        session.putReadOnlyEntity(_pk, _entity);
648        
649        return _entity;
650    }
651    
652    public void create(Collection<CommentBlobValue> _values)
653            throws PersistenceDatabaseException, PersistenceNotNullException {
654        int _size = _values.size();
655        
656        if(_size > 0) {
657            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
658            List<CommentBlobValue> _cacheValues = new ArrayList<>(_size);
659            
660            try {
661                for(CommentBlobValue _value : _values) {
662                    _value.setEntityId(entityIdGenerator.getNextEntityId());
663                    bindForCreate(_ps, _value);
664                    
665                    _ps.addBatch();
666                    
667                    _cacheValues.add(_value);
668                }
669                
670                if(PersistenceDebugFlags.CheckEntityInsertRowCount) {
671                    int[] _counts = _ps.executeBatch();
672                    
673                    for(int _countOffset = 0 ; _countOffset < _size ; _countOffset++) {
674                        if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
675                            throw new PersistenceDatabaseUpdateException("batch insert failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
676                        }
677                    }
678                } else {
679                     _ps.executeBatch();
680                }
681                
682                _ps.clearBatch();
683            } catch (SQLException se) {
684                throw new PersistenceDatabaseException(se);
685            }
686            
687            _cacheValues.forEach((_cacheValue) -> {
688                CommentBlob _cacheEntity = new CommentBlob(_cacheValue, EntityPermission.READ_ONLY);
689                
690                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
691            });
692        }
693    }
694    
695    private boolean bindForStore(PreparedStatement _ps, CommentBlobValue _value)
696            throws SQLException {
697        boolean _hasBeenModified = _value.hasBeenModified();
698        
699        if(_hasBeenModified) {
700            CommentPK cmntb_cmnt_commentid = _value.getCommentPK();
701            if(cmntb_cmnt_commentid == null)
702                _ps.setNull(1, Types.BIGINT);
703            else
704                _ps.setLong(1, cmntb_cmnt_commentid.getEntityId());
705            
706            ByteArray cmntb_blob = _value.getBlob();
707            if(cmntb_blob == null)
708                _ps.setNull(2, Types.BLOB);
709            else
710                _ps.setBinaryStream(2, new ByteArrayInputStream(cmntb_blob.byteArrayValue()), cmntb_blob.length());
711            
712            Long cmntb_fromtime = _value.getFromTime();
713            if(cmntb_fromtime == null)
714                _ps.setNull(3, Types.BIGINT);
715            else
716                _ps.setLong(3, cmntb_fromtime);
717            
718            Long cmntb_thrutime = _value.getThruTime();
719            if(cmntb_thrutime == null)
720                _ps.setNull(4, Types.BIGINT);
721            else
722                _ps.setLong(4, cmntb_thrutime);
723            
724            _ps.setLong(5, _value.getPrimaryKey().getEntityId());
725            
726            _value.clearHasBeenModified();
727        }
728        
729        return _hasBeenModified;
730    }
731    
732    @Override
733    public void store(CommentBlob entity)
734            throws PersistenceDatabaseException {
735        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
736        
737        try {
738            CommentBlobValue _value = entity.getCommentBlobValue();
739            
740            if(bindForStore(_ps, _value)) {
741                if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
742                    int _count = _ps.executeUpdate();
743                    
744                    if(_count != 1) {
745                        throw new PersistenceDatabaseUpdateException("update failed, _count = " + _count);
746                    }
747                } else {
748                     _ps.executeUpdate();
749                }
750                
751                session.getValueCache().put(_value);
752            }
753        } catch (SQLException se) {
754            throw new PersistenceDatabaseException(se);
755        }
756    }
757    
758    @Override
759    public void store(Collection<CommentBlob> entities)
760            throws PersistenceDatabaseException {
761        if(entities.size() > 0) {
762            PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
763            int _modifiedEntities = 0;
764            
765            try {
766                for(CommentBlob entity : entities) {
767                    if(bindForStore(_ps, entity.getCommentBlobValue())) {
768                        _ps.addBatch();
769                        _modifiedEntities++;
770                    }
771                }
772                
773                if(_modifiedEntities != 0) {
774                    if(PersistenceDebugFlags.CheckEntityUpdateRowCount) {
775                        int[] _counts = _ps.executeBatch();
776                        
777                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
778                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
779                                throw new PersistenceDatabaseUpdateException("batch update failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
780                            }
781                        }
782                    } else {
783                         _ps.executeBatch();
784                    }
785                    
786                    _ps.clearBatch();
787                    
788                    entities.forEach((entity) -> {
789                        session.getValueCache().put(entity.getCommentBlobValue());
790                    });
791                }
792            } catch (SQLException se) {
793                throw new PersistenceDatabaseException(se);
794            }
795        }
796    }
797    
798    @Override
799    public void remove(CommentBlob entity)
800            throws PersistenceDatabaseException {
801        remove(entity.getPrimaryKey());
802    }
803    
804    @Override
805    public void remove(CommentBlobPK pk)
806            throws PersistenceDatabaseException {
807        PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
808        long _entityId = pk.getEntityId();
809        
810        try {
811            _ps.setLong(1, _entityId);
812            
813            if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
814                int _count = _ps.executeUpdate();
815                
816                if(_count != 1) {
817                    throw new PersistenceDatabaseUpdateException("remove failed, _count = " + _count);
818                }
819            } else {
820                 _ps.executeUpdate();
821            }
822            
823            session.getValueCache().remove(pk);
824        } catch (SQLException se) {
825            throw new PersistenceDatabaseException(se);
826        }
827        
828        session.removed(pk, false);
829    }
830    
831    @Override
832    public void remove(Collection<CommentBlobPK> pks)
833            throws PersistenceDatabaseException {
834        if(pks.size() > 0) {
835            PreparedStatement _ps = session.prepareStatement(SQL_DELETE);
836            int _modifiedEntities = 0;
837            
838            try {
839                for(CommentBlobPK pk : pks) {
840                    long _entityId = pk.getEntityId();
841                    
842                    _ps.setLong(1, _entityId);
843                    
844                    _ps.addBatch();
845                    _modifiedEntities++;
846                }
847                
848                if(_modifiedEntities != 0) {
849                    if(PersistenceDebugFlags.CheckEntityDeleteRowCount) {
850                        int[] _counts = _ps.executeBatch();
851                        
852                        for(int _countOffset = 0 ; _countOffset < _modifiedEntities  ; _countOffset++) {
853                            if(_counts[_countOffset] != 1 && _counts[_countOffset] != PreparedStatement.SUCCESS_NO_INFO) {
854                                throw new PersistenceDatabaseUpdateException("batch remove failed, _counts[" + _countOffset + "] = " + _counts[_countOffset]);
855                            }
856                        }
857                    } else {
858                        _ps.executeBatch();
859                    }
860                    
861                    _ps.clearBatch();
862                    
863                    pks.forEach((pk) -> {
864                        session.getValueCache().remove(pk);
865                    });
866                }
867            } catch (SQLException se) {
868                throw new PersistenceDatabaseException(se);
869            }
870            
871            pks.forEach((pk) -> {
872                session.removed(pk, true);
873            });
874        }
875    }
876    
877    @Override
878    public boolean validPK(CommentBlobPK pk)
879            throws PersistenceDatabaseException {
880        boolean valid = false;
881        PreparedStatement _ps = session.prepareStatement(SQL_VALID);
882        ResultSet _rs = null;
883        
884        try {
885            _ps.setLong(1, pk.getEntityId());
886            
887            _rs = _ps.executeQuery();
888            if(_rs.next()) {
889                long _count = _rs.getLong(1);
890                if(_rs.wasNull())
891                    _count = 0;
892                
893                if(_count == 1)
894                    valid = true;
895            }
896        } catch (SQLException se) {
897            throw new PersistenceDatabaseException(se);
898        } finally {
899            if(_rs != null) {
900                try {
901                    _rs.close();
902                } catch (SQLException se) {
903                    // do nothing
904                }
905            }
906        }
907        
908        return valid;
909    }
910    
911}