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