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