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