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 * OrderRoleFactory.java
021 */
022
023package com.echothree.model.data.order.server.factory;
024
025import com.echothree.model.data.order.common.pk.OrderPK;
026import com.echothree.model.data.party.common.pk.PartyPK;
027import com.echothree.model.data.order.common.pk.OrderRoleTypePK;
028
029import com.echothree.model.data.order.server.entity.Order;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.model.data.order.server.entity.OrderRoleType;
032
033import com.echothree.model.data.order.common.OrderRoleConstants;
034import com.echothree.model.data.order.common.pk.OrderRolePK;
035import com.echothree.model.data.order.server.value.OrderRoleValue;
036import com.echothree.model.data.order.server.entity.OrderRole;
037import com.echothree.util.common.exception.PersistenceDatabaseException;
038import com.echothree.util.common.exception.PersistenceDatabaseUpdateException;
039import com.echothree.util.common.exception.PersistenceNotNullException;
040import com.echothree.util.server.persistence.BaseFactory;
041import com.echothree.util.server.persistence.EntityIdGenerator;
042import com.echothree.util.server.persistence.EntityPermission;
043import com.echothree.util.server.persistence.PersistenceDebugFlags;
044import com.echothree.util.server.persistence.Session;
045import java.sql.PreparedStatement;
046import java.sql.ResultSet;
047import java.sql.SQLException;
048import java.sql.Types;
049import java.io.ByteArrayInputStream;
050import java.io.StringReader;
051import java.util.ArrayList;
052import java.util.Collection;
053import java.util.HashSet;
054import java.util.List;
055import java.util.Map;
056import java.util.Set;
057import javax.enterprise.context.ApplicationScoped;
058import javax.enterprise.inject.spi.CDI;
059import javax.inject.Inject;
060import org.jooq.ResultQuery;
061import org.slf4j.Logger;
062import org.slf4j.LoggerFactory;
063
064@ApplicationScoped
065public class OrderRoleFactory
066        implements BaseFactory<OrderRolePK, OrderRole> {
067    
068    @Inject
069    Session session;
070    
071    //private static final Logger log = LoggerFactory.getLogger(OrderRoleFactory.class);
072    
073    final private static String SQL_SELECT_READ_ONLY = "SELECT ordr_orderroleid, ordr_ord_orderid, ordr_par_partyid, ordr_ordrtyp_orderroletypeid, ordr_fromtime, ordr_thrutime FROM orderroles WHERE ordr_orderroleid = ?";
074    final private static String SQL_SELECT_READ_WRITE = "SELECT ordr_orderroleid, ordr_ord_orderid, ordr_par_partyid, ordr_ordrtyp_orderroletypeid, ordr_fromtime, ordr_thrutime FROM orderroles WHERE ordr_orderroleid = ? FOR UPDATE";
075    final private static String SQL_INSERT = "INSERT INTO orderroles (ordr_orderroleid, ordr_ord_orderid, ordr_par_partyid, ordr_ordrtyp_orderroletypeid, ordr_fromtime, ordr_thrutime) VALUES (?, ?, ?, ?, ?, ?)";
076    final private static String SQL_UPDATE = "UPDATE orderroles SET ordr_ord_orderid = ?, ordr_par_partyid = ?, ordr_ordrtyp_orderroletypeid = ?, ordr_fromtime = ?, ordr_thrutime = ? WHERE ordr_orderroleid = ?";
077    final private static String SQL_DELETE = "DELETE FROM orderroles WHERE ordr_orderroleid = ?";
078    final private static String SQL_VALID = "SELECT COUNT(*) FROM orderroles WHERE ordr_orderroleid = ?";
079    
080    final public static String ORDR_ORDERROLEID = "ordr_orderroleid";
081    final public static String ORDR_ORD_ORDERID = "ordr_ord_orderid";
082    final public static String ORDR_PAR_PARTYID = "ordr_par_partyid";
083    final public static String ORDR_ORDRTYP_ORDERROLETYPEID = "ordr_ordrtyp_orderroletypeid";
084    final public static String ORDR_FROMTIME = "ordr_fromtime";
085    final public static String ORDR_THRUTIME = "ordr_thrutime";
086    
087    final private static EntityIdGenerator entityIdGenerator = new EntityIdGenerator(OrderRoleConstants.COMPONENT_VENDOR_NAME, OrderRoleConstants.ENTITY_TYPE_NAME);
088    
089    /** Creates a new instance of OrderRoleFactory */
090    protected OrderRoleFactory() {
091        super();
092    }
093    
094    public static OrderRoleFactory getInstance() {
095        return CDI.current().select(OrderRoleFactory.class).get();
096    }
097    
098    @Override
099    public String getComponentVendorName() {
100        return OrderRoleConstants.COMPONENT_VENDOR_NAME;
101    }
102    
103    @Override
104    public String getEntityTypeName() {
105        return OrderRoleConstants.ENTITY_TYPE_NAME;
106    }
107    
108    public PreparedStatement prepareStatement(String query) {
109        return session.prepareStatement(query);
110    }
111    
112    public OrderRolePK getNextPK() {
113        return new OrderRolePK(entityIdGenerator.getNextEntityId());
114    }
115    
116    public Set<OrderRolePK> getPKsFromResultSetAsSet(ResultSet rs)
117            throws PersistenceDatabaseException {
118        Set<OrderRolePK> _result = new HashSet<>();
119        
120        try {
121            while(rs.next()) {
122                _result.add(getPKFromResultSet(rs));
123            }
124        } catch (SQLException se) {
125            throw new PersistenceDatabaseException(se);
126        }
127        
128        return _result;
129    }
130    
131    public java.util.List<OrderRolePK> getPKsFromResultSetAsList(ResultSet rs)
132            throws PersistenceDatabaseException {
133        java.util.List<OrderRolePK> _result = new ArrayList<>();
134        
135        try {
136            while(rs.next()) {
137                _result.add(getPKFromResultSet(rs));
138            }
139        } catch (SQLException se) {
140            throw new PersistenceDatabaseException(se);
141        }
142        
143        return _result;
144    }
145    
146    public OrderRolePK getPKFromResultSet(ResultSet rs)
147            throws PersistenceDatabaseException {
148        OrderRolePK _result;
149        
150        try {
151            long ordr_orderroleid = rs.getLong(ORDR_ORDERROLEID);
152            Long _entityId = rs.wasNull() ? null : ordr_orderroleid;
153            
154            _result = new OrderRolePK(_entityId);
155        } catch (SQLException se) {
156            throw new PersistenceDatabaseException(se);
157        }
158        
159        return _result;
160    }
161    
162    public java.util.List<OrderRoleValue> getValuesFromPKs(Collection<OrderRolePK> pks)
163            throws PersistenceDatabaseException {
164        java.util.List<OrderRoleValue> _values = new ArrayList<>(pks.size());
165        
166        for(OrderRolePK _pk: pks) {
167            _values.add(getValueFromPK(_pk));
168        }
169        
170        return _values;
171    }
172    
173    public OrderRoleValue getValueFromPK(OrderRolePK pk)
174            throws PersistenceDatabaseException {
175        OrderRoleValue _value;
176        
177        // See if we already have the entity in the session cache
178        OrderRole _entity = (OrderRole)session.getEntity(pk);
179        if(_entity == null)
180            _value = getEntityFromPK(EntityPermission.READ_ONLY, pk).getOrderRoleValue();
181        else
182            _value = _entity.getOrderRoleValue();
183        
184        return _value;
185    }
186    
187    public java.util.List<OrderRoleValue> getValuesFromResultSet(ResultSet rs)
188            throws PersistenceDatabaseException {
189        java.util.List<OrderRoleValue> _result = new ArrayList<>();
190        
191        try {
192            while(rs.next()) {
193                _result.add(getValueFromResultSet(rs));
194            }
195        } catch (SQLException se) {
196            throw new PersistenceDatabaseException(se);
197        }
198        
199        return _result;
200    }
201    
202    public OrderRoleValue getValueFromResultSet(ResultSet rs)
203            throws PersistenceDatabaseException {
204        OrderRoleValue _value;
205        
206        try {
207            Long ordr_orderroleid = rs.getLong(ORDR_ORDERROLEID);
208            OrderRolePK _pk = new OrderRolePK(ordr_orderroleid);
209            
210            // See if we already have the entity in the session cache
211            OrderRole _entity = (OrderRole)session.getEntity(_pk);
212            
213            if(_entity == null) {
214                Long ordr_ord_orderid = rs.getLong(ORDR_ORD_ORDERID);
215                if(rs.wasNull())
216                    ordr_ord_orderid = null;
217                
218                Long ordr_par_partyid = rs.getLong(ORDR_PAR_PARTYID);
219                if(rs.wasNull())
220                    ordr_par_partyid = null;
221                
222                Long ordr_ordrtyp_orderroletypeid = rs.getLong(ORDR_ORDRTYP_ORDERROLETYPEID);
223                if(rs.wasNull())
224                    ordr_ordrtyp_orderroletypeid = null;
225                
226                Long ordr_fromtime = rs.getLong(ORDR_FROMTIME);
227                if(rs.wasNull())
228                    ordr_fromtime = null;
229                
230                Long ordr_thrutime = rs.getLong(ORDR_THRUTIME);
231                if(rs.wasNull())
232                    ordr_thrutime = null;
233                
234                _value = new OrderRoleValue(_pk, new OrderPK(ordr_ord_orderid), new PartyPK(ordr_par_partyid), new OrderRoleTypePK(ordr_ordrtyp_orderroletypeid), ordr_fromtime, ordr_thrutime);
235            } else
236                _value = _entity.getOrderRoleValue();
237        } catch (SQLException se) {
238            throw new PersistenceDatabaseException(se);
239        }
240        
241        return _value;
242    }
243    
244    public java.util.List<OrderRole> getEntitiesFromPKs(EntityPermission entityPermission, Collection<OrderRolePK> pks)
245            throws PersistenceDatabaseException {
246        java.util.List<OrderRole> _entities = new ArrayList<>(pks.size());
247        
248        for(OrderRolePK _pk: pks) {
249            _entities.add(getEntityFromPK(entityPermission, _pk));
250        }
251        
252        return _entities;
253    }
254    
255    public OrderRole getEntityFromValue(EntityPermission entityPermission, OrderRoleValue value) {
256        return getEntityFromPK(entityPermission, value.getPrimaryKey());
257    }
258    
259    public OrderRole getEntityFromCache(OrderRolePK pk) {
260        OrderRoleValue _value = (OrderRoleValue)session.getValueCache().get(pk);
261    
262        return _value == null ? null : new OrderRole(_value, EntityPermission.READ_ONLY);
263    }
264    
265    public OrderRole getEntityFromPK(EntityPermission entityPermission, OrderRolePK pk)
266            throws PersistenceDatabaseException {
267        OrderRole _entity;
268        
269        // See if we already have the entity in the session cache
270        _entity = (OrderRole)session.getEntity(pk);
271        if(_entity != null) {
272            // If the requested permission is READ_WRITE, and the cached permission is
273            // READ_ONLY, then pretend that the cached object wasn't found, and create
274            // a new entity that is READ_WRITE.
275            if(entityPermission.equals(EntityPermission.READ_WRITE)) {
276                if(_entity.getEntityPermission().equals(EntityPermission.READ_ONLY))
277                    _entity = null;
278            }
279        }
280        
281        if(_entity == null && entityPermission.equals(EntityPermission.READ_ONLY)) {
282            _entity = getEntityFromCache(pk);
283        }
284        
285        if(_entity == null) {
286            PreparedStatement _ps = session.prepareStatement(entityPermission.equals(EntityPermission.READ_ONLY)? SQL_SELECT_READ_ONLY: SQL_SELECT_READ_WRITE);
287            long _entityId = pk.getEntityId();
288            ResultSet _rs = null;
289            
290            try {
291                _ps.setLong(1, _entityId);
292                _rs = _ps.executeQuery();
293                if(_rs.next()) {
294                    _entity = getEntityFromResultSet(entityPermission, _rs);
295                }
296            } catch (SQLException se) {
297                throw new PersistenceDatabaseException(se);
298            } finally {
299                if(_rs != null) {
300                    try {
301                        _rs.close();
302                    } catch (SQLException se) {
303                        // do nothing
304                    }
305                }
306            }
307        }
308        
309        return _entity;
310    }
311    
312    public Set<OrderRolePK> getPKsFromQueryAsSet(PreparedStatement ps, final Object... params)
313            throws PersistenceDatabaseException {
314        Set<OrderRolePK> _pks;
315        ResultSet _rs = null;
316        
317        try {
318            if(params.length != 0) {
319                Session.setQueryParams(ps, params);
320            }
321            
322            _rs = ps.executeQuery();
323            _pks = getPKsFromResultSetAsSet(_rs);
324            _rs.close();
325        } catch (SQLException se) {
326            throw new PersistenceDatabaseException(se);
327        } finally {
328            if(_rs != null) {
329                try {
330                    _rs.close();
331                } catch (SQLException se) {
332                    // do nothing
333                }
334            }
335        }
336        
337        return _pks;
338    }
339    
340    public java.util.List<OrderRolePK> getPKsFromQueryAsList(PreparedStatement ps, final Object... params)
341            throws PersistenceDatabaseException {
342        java.util.List<OrderRolePK> _pks;
343        ResultSet _rs = null;
344        
345        try {
346            if(params.length != 0) {
347                Session.setQueryParams(ps, params);
348            }
349            
350            _rs = ps.executeQuery();
351            _pks = getPKsFromResultSetAsList(_rs);
352            _rs.close();
353        } catch (SQLException se) {
354            throw new PersistenceDatabaseException(se);
355        } finally {
356            if(_rs != null) {
357                try {
358                    _rs.close();
359                } catch (SQLException se) {
360                    // do nothing
361                }
362            }
363        }
364        
365        return _pks;
366    }
367    
368    public OrderRolePK getPKFromQuery(PreparedStatement ps, final Object... params)
369            throws PersistenceDatabaseException {
370        OrderRolePK _pk = null;
371        ResultSet _rs = null;
372        
373        try {
374            if(params.length != 0) {
375                Session.setQueryParams(ps, params);
376            }
377            
378            _rs = ps.executeQuery();
379            if(_rs.next()) {
380                _pk = getPKFromResultSet(_rs);
381            }
382            _rs.close();
383        } catch (SQLException se) {
384            throw new PersistenceDatabaseException(se);
385        } finally {
386            if(_rs != null) {
387                try {
388                    _rs.close();
389                } catch (SQLException se) {
390                    // do nothing
391                }
392            }
393        }
394        
395        return _pk;
396    }
397    
398    public java.util.List<OrderRole> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
399            throws PersistenceDatabaseException {
400        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
401        
402        return getEntitiesFromQuery(entityPermission, ps, params);
403    }
404    
405    public java.util.List<OrderRole> getEntitiesFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
406            throws PersistenceDatabaseException {
407        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
408        
409        return getEntitiesFromQuery(entityPermission, ps);
410    }
411    
412    public java.util.List<OrderRole> getEntitiesFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
413            throws PersistenceDatabaseException {
414        PreparedStatement ps = session.prepareStatement(query.getSQL());
415        
416        return getEntitiesFromQuery(entityPermission, ps, query.getBindValues().toArray());
417    }
418    
419    public java.util.List<OrderRole> getEntitiesFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
420            throws PersistenceDatabaseException {
421        java.util.List<OrderRole> _entities;
422        ResultSet _rs = null;
423        
424        try {
425            if(params.length != 0) {
426                Session.setQueryParams(ps, params);
427            }
428            
429            _rs = ps.executeQuery();
430            _entities = getEntitiesFromResultSet(entityPermission, _rs);
431            _rs.close();
432        } catch (SQLException se) {
433            throw new PersistenceDatabaseException(se);
434        } finally {
435            if(_rs != null) {
436                try {
437                    _rs.close();
438                } catch (SQLException se) {
439                    // do nothing
440                }
441            }
442        }
443        
444        return _entities;
445    }
446    
447    public OrderRole getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap, final Object... params)
448            throws PersistenceDatabaseException {
449        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
450        
451        return getEntityFromQuery(entityPermission, ps, params);
452    }
453    
454    public OrderRole getEntityFromQuery(EntityPermission entityPermission, Map<EntityPermission, String>queryMap)
455            throws PersistenceDatabaseException {
456        PreparedStatement ps = session.prepareStatement(queryMap.get(entityPermission));
457        
458        return getEntityFromQuery(entityPermission, ps);
459    }
460    
461    public OrderRole getEntityFromQuery(EntityPermission entityPermission, ResultQuery<?> query)
462            throws PersistenceDatabaseException {
463        PreparedStatement ps = session.prepareStatement(query.getSQL());
464        
465        return getEntityFromQuery(entityPermission, ps, query.getBindValues().toArray());
466    }
467    
468    public OrderRole getEntityFromQuery(EntityPermission entityPermission, PreparedStatement ps, final Object... params)
469            throws PersistenceDatabaseException {
470        OrderRole _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<OrderRole> getEntitiesFromResultSet(EntityPermission entityPermission, ResultSet rs)
499            throws PersistenceDatabaseException {
500        java.util.List<OrderRole> _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 OrderRole getEntityFromResultSet(EntityPermission entityPermission, ResultSet rs)
514            throws PersistenceDatabaseException {
515        OrderRole _entity;
516        
517        try {
518            Long ordr_orderroleid = rs.getLong(ORDR_ORDERROLEID);
519            OrderRolePK _pk = new OrderRolePK(ordr_orderroleid);
520            
521            // See if we already have the entity in the session cache
522            _entity = (OrderRole)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 ordr_ord_orderid = rs.getLong(ORDR_ORD_ORDERID);
540                if(rs.wasNull())
541                    ordr_ord_orderid = null;
542                
543                Long ordr_par_partyid = rs.getLong(ORDR_PAR_PARTYID);
544                if(rs.wasNull())
545                    ordr_par_partyid = null;
546                
547                Long ordr_ordrtyp_orderroletypeid = rs.getLong(ORDR_ORDRTYP_ORDERROLETYPEID);
548                if(rs.wasNull())
549                    ordr_ordrtyp_orderroletypeid = null;
550                
551                Long ordr_fromtime = rs.getLong(ORDR_FROMTIME);
552                if(rs.wasNull())
553                    ordr_fromtime = null;
554                
555                Long ordr_thrutime = rs.getLong(ORDR_THRUTIME);
556                if(rs.wasNull())
557                    ordr_thrutime = null;
558                
559                OrderRoleValue _value = new OrderRoleValue(_pk, ordr_ord_orderid == null? null: new OrderPK(ordr_ord_orderid), ordr_par_partyid == null? null: new PartyPK(ordr_par_partyid), ordr_ordrtyp_orderroletypeid == null? null: new OrderRoleTypePK(ordr_ordrtyp_orderroletypeid), ordr_fromtime, ordr_thrutime);
560                _entity = new OrderRole(_value, entityPermission);
561            }
562            
563            if(!foundInSessionCache) {
564                if(entityPermission.equals(EntityPermission.READ_ONLY)) {
565                    session.putReadOnlyEntity(_pk, _entity);
566                    session.getValueCache().put(_entity.getOrderRoleValue());
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 OrderRole create(Order order, Party party, OrderRoleType orderRoleType, Long fromTime, Long thruTime)
579            throws PersistenceDatabaseException, PersistenceNotNullException {
580        return create(order == null ? null : order.getPrimaryKey(), party == null ? null : party.getPrimaryKey(), orderRoleType == null ? null : orderRoleType.getPrimaryKey(), fromTime, thruTime);
581    }
582    
583    private void bindForCreate(PreparedStatement _ps, OrderRoleValue _value)
584            throws SQLException {
585        _ps.setLong(1, _value.getEntityId());
586        
587        OrderPK ordr_ord_orderid = _value.getOrderPK();
588        if(ordr_ord_orderid == null)
589            _ps.setNull(2, Types.BIGINT);
590        else
591            _ps.setLong(2, ordr_ord_orderid.getEntityId());
592            
593        PartyPK ordr_par_partyid = _value.getPartyPK();
594        if(ordr_par_partyid == null)
595            _ps.setNull(3, Types.BIGINT);
596        else
597            _ps.setLong(3, ordr_par_partyid.getEntityId());
598            
599        OrderRoleTypePK ordr_ordrtyp_orderroletypeid = _value.getOrderRoleTypePK();
600        if(ordr_ordrtyp_orderroletypeid == null)
601            _ps.setNull(4, Types.BIGINT);
602        else
603            _ps.setLong(4, ordr_ordrtyp_orderroletypeid.getEntityId());
604            
605        Long ordr_fromtime = _value.getFromTime();
606        if(ordr_fromtime == null)
607            _ps.setNull(5, Types.BIGINT);
608        else
609            _ps.setLong(5, ordr_fromtime);
610            
611        Long ordr_thrutime = _value.getThruTime();
612        if(ordr_thrutime == null)
613            _ps.setNull(6, Types.BIGINT);
614        else
615            _ps.setLong(6, ordr_thrutime);
616            
617    }
618    
619    public OrderRole create(OrderPK orderPK, PartyPK partyPK, OrderRoleTypePK orderRoleTypePK, Long fromTime, Long thruTime)
620            throws PersistenceDatabaseException, PersistenceNotNullException {
621        OrderRolePK _pk = getNextPK();
622        OrderRoleValue _value = new OrderRoleValue(_pk, orderPK, partyPK, orderRoleTypePK, 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        OrderRole _entity = new OrderRole(_value, EntityPermission.READ_ONLY);
645        session.putReadOnlyEntity(_pk, _entity);
646        
647        return _entity;
648    }
649    
650    public void create(Collection<OrderRoleValue> _values)
651            throws PersistenceDatabaseException, PersistenceNotNullException {
652        int _size = _values.size();
653        
654        if(_size > 0) {
655            PreparedStatement _ps = session.prepareStatement(SQL_INSERT);
656            List<OrderRoleValue> _cacheValues = new ArrayList<>(_size);
657            
658            try {
659                for(OrderRoleValue _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                OrderRole _cacheEntity = new OrderRole(_cacheValue, EntityPermission.READ_ONLY);
687                
688                session.putReadOnlyEntity(_cacheValue.getPrimaryKey(), _cacheEntity);
689            });
690        }
691    }
692    
693    private boolean bindForStore(PreparedStatement _ps, OrderRoleValue _value)
694            throws SQLException {
695        boolean _hasBeenModified = _value.hasBeenModified();
696        
697        if(_hasBeenModified) {
698            OrderPK ordr_ord_orderid = _value.getOrderPK();
699            if(ordr_ord_orderid == null)
700                _ps.setNull(1, Types.BIGINT);
701            else
702                _ps.setLong(1, ordr_ord_orderid.getEntityId());
703            
704            PartyPK ordr_par_partyid = _value.getPartyPK();
705            if(ordr_par_partyid == null)
706                _ps.setNull(2, Types.BIGINT);
707            else
708                _ps.setLong(2, ordr_par_partyid.getEntityId());
709            
710            OrderRoleTypePK ordr_ordrtyp_orderroletypeid = _value.getOrderRoleTypePK();
711            if(ordr_ordrtyp_orderroletypeid == null)
712                _ps.setNull(3, Types.BIGINT);
713            else
714                _ps.setLong(3, ordr_ordrtyp_orderroletypeid.getEntityId());
715            
716            Long ordr_fromtime = _value.getFromTime();
717            if(ordr_fromtime == null)
718                _ps.setNull(4, Types.BIGINT);
719            else
720                _ps.setLong(4, ordr_fromtime);
721            
722            Long ordr_thrutime = _value.getThruTime();
723            if(ordr_thrutime == null)
724                _ps.setNull(5, Types.BIGINT);
725            else
726                _ps.setLong(5, ordr_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(OrderRole entity)
738            throws PersistenceDatabaseException {
739        PreparedStatement _ps = session.prepareStatement(SQL_UPDATE);
740        
741        try {
742            OrderRoleValue _value = entity.getOrderRoleValue();
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<OrderRole> 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(OrderRole entity : entities) {
771                    if(bindForStore(_ps, entity.getOrderRoleValue())) {
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.getOrderRoleValue());
794                    });
795                }
796            } catch (SQLException se) {
797                throw new PersistenceDatabaseException(se);
798            }
799        }
800    }
801    
802    @Override
803    public void remove(OrderRole entity)
804            throws PersistenceDatabaseException {
805        remove(entity.getPrimaryKey());
806    }
807    
808    @Override
809    public void remove(OrderRolePK 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<OrderRolePK> 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(OrderRolePK 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(OrderRolePK 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}