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