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