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