public void init(DBDescriptor dbDescriptor, SqlUtils sqlUtils) { if (!dbDescriptor.supportsSequences()) { throw new InternalException( "Database Type '" + dbDescriptor.getClass().getName() + "' does not support sequences"); } this.dbDescriptor = dbDescriptor; this.sqlUtils = sqlUtils; }
private List getNextSequenceImpl(TypeDescriptor typeDescriptor, Session session) { Field[] pkFields = typeDescriptor.getPkFields(); Assert.condition( 1 == pkFields.length, "Automatic PK values are only supported for types with a single PK field."); String createPKStmt = dbDescriptor.getCreatePKStatement( sqlUtils.getSchemaName(), typeDescriptor.getPkSequence(), this.sequenceBatchSize); Field field = pkFields[0]; if (session.isUsingPreparedStatements(typeDescriptor.getType())) { PreparedStatement pkStatement = null; try { Connection connection = session.getConnection(); PreparedStatement result; try { result = connection.prepareStatement(createPKStmt); } catch (SQLException x) { throw new InternalException(x); } pkStatement = result; ResultSet pkQuery = pkStatement.executeQuery(); List newIds = new LinkedList(); while (pkQuery.next()) { newIds.add( DmlManager.getJavaValue( field.getType(), typeDescriptor.getPersistentField(field).getLength(), pkQuery, 1, true, false)); } return newIds; } catch (SQLException e) { throw new InternalException(e); } finally { QueryUtils.closeStatement(pkStatement); } } else { Statement pkStmt = null; try { Connection connection = session.getConnection(); pkStmt = connection.createStatement(); ResultSet pkQuery = pkStmt.executeQuery(createPKStmt); List newIds = new LinkedList(); while (pkQuery.next()) { newIds.add( DmlManager.getJavaValue( field.getType(), typeDescriptor.getPersistentField(field).getLength(), pkQuery, 1, true, false)); } return newIds; } catch (SQLException e) { throw new InternalException(e); } finally { QueryUtils.closeStatement(pkStmt); } } }