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