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