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