protected void initializeWithTableNames(Session session, Collection tableNames) { tableNameToClass = new Hashtable(tableNames.size()); tableNameToPkFieldNames = new Hashtable(tableNames.size()); // pkFieldVectors cached here to avoid calculating it more than once per class Hashtable classToPkFieldNames = new Hashtable(); // loop through the descriptors to fill out tableNameToClass and tableNameToPkFieldNames Iterator descriptors = session.getDescriptors().values().iterator(); while (descriptors.hasNext() && !tableNames.isEmpty()) { ClassDescriptor desc = (ClassDescriptor) descriptors.next(); // Create a Vector containing names of all tables mapped to the descriptor Vector descTableNames = desc.getTableNames(); // bypass descriptors with no tables if (descTableNames.isEmpty()) { continue; } // Remove schema names (if any) converting "SCHEMA_NAME.TABLE_NAME" to "TABLE_NAME" removePrefixFromDatabaseObjectNames(descTableNames); // handle inheritance: table name should be mapped to the base mapped class Class baseClass = desc.getJavaClass(); while (desc.isChildDescriptor()) { desc = session.getDescriptor(desc.getInheritancePolicy().getParentClass()); baseClass = desc.getJavaClass(); } Iterator it = tableNames.iterator(); while (it.hasNext()) { // for each tableName specified by the user String tableName = (String) it.next(); // verify whether the descriptor maps a table with the same name if (descTableNames.contains(tableName)) { // map the table name to the baseClass corresponding to the descriptor tableNameToClass.put(tableName, baseClass); // try to obtain cached pkFieldNames Vector corresponding to baseClass Vector pkFieldNames = (Vector) classToPkFieldNames.get(baseClass); if (pkFieldNames == null) { // Create a Vector containing names of all primary key fields pkFieldNames = desc.getPrimaryKeyFieldNames(); // Remove table name converting from "TABLE_NAME.FIELD_NAME" to "FIELD_NAME" removePrefixFromDatabaseObjectNames(pkFieldNames); // cache pkFieldNames Vector corresponding to baseClass classToPkFieldNames.put(baseClass, pkFieldNames); } // map the table name to the Vector of names of primary key fields. tableNameToPkFieldNames.put(tableName, pkFieldNames); // the table name is mapped - remove it from the list of table names to be mapped. it.remove(); } } } }
protected void onConnectAllSequences() { connectedSequences = new Vector(); boolean shouldUseTransaction = false; boolean shouldUsePreallocation = false; boolean shouldAcquireValueAfterInsert = false; Iterator descriptors = getOwnerSession().getDescriptors().values().iterator(); while (descriptors.hasNext()) { ClassDescriptor descriptor = (ClassDescriptor) descriptors.next(); // Find root sequence, because inheritance needs to be resolved here. // TODO: The way we initialize sequencing needs to be in line with descriptor init. ClassDescriptor parentDescriptor = descriptor; while (!parentDescriptor.usesSequenceNumbers() && parentDescriptor.isChildDescriptor()) { ClassDescriptor newDescriptor = getOwnerSession() .getDescriptor(parentDescriptor.getInheritancePolicy().getParentClass()); // Avoid issue with error cases of self parent, or null parent. if ((newDescriptor == null) || (newDescriptor == parentDescriptor)) { break; } parentDescriptor = newDescriptor; } if (!parentDescriptor.usesSequenceNumbers()) { continue; } String seqName = parentDescriptor.getSequenceNumberName(); Sequence sequence = getSequence(seqName); if (sequence == null) { sequence = new DefaultSequence(seqName); getOwnerSession().getDatasourcePlatform().addSequence(sequence); } // PERF: Initialize the sequence, this avoid having to look it up every time. descriptor.setSequence(sequence); if (connectedSequences.contains(sequence)) { continue; } try { if (sequence instanceof DefaultSequence && !connectedSequences.contains(getDefaultSequence())) { getDefaultSequence().onConnect(getOwnerSession().getDatasourcePlatform()); connectedSequences.add(0, getDefaultSequence()); shouldUseTransaction |= getDefaultSequence().shouldUseTransaction(); shouldUsePreallocation |= getDefaultSequence().shouldUsePreallocation(); shouldAcquireValueAfterInsert |= getDefaultSequence().shouldAcquireValueAfterInsert(); } sequence.onConnect(getOwnerSession().getDatasourcePlatform()); connectedSequences.addElement(sequence); shouldUseTransaction |= sequence.shouldUseTransaction(); shouldUsePreallocation |= sequence.shouldUsePreallocation(); shouldAcquireValueAfterInsert |= sequence.shouldAcquireValueAfterInsert(); } catch (RuntimeException ex) { // defaultSequence has to disconnect the last for (int i = connectedSequences.size() - 1; i >= 0; i--) { try { Sequence sequenceToDisconnect = (Sequence) connectedSequences.elementAt(i); sequenceToDisconnect.onDisconnect(getOwnerSession().getDatasourcePlatform()); } catch (RuntimeException ex2) { // ignore } } connectedSequences = null; throw ex; } } if (shouldAcquireValueAfterInsert && !shouldUsePreallocation) { whenShouldAcquireValueForAll = AFTER_INSERT; } else if (!shouldAcquireValueAfterInsert && shouldUsePreallocation) { whenShouldAcquireValueForAll = BEFORE_INSERT; } atLeastOneSequenceShouldUseTransaction = shouldUseTransaction; atLeastOneSequenceShouldUsePreallocation = shouldUsePreallocation; }