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