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