private SelectGeneratorDelegate( PostInsertIdentityPersister persister, Dialect dialect, String suppliedUniqueKeyPropertyName) { super(persister); this.persister = persister; this.dialect = dialect; this.uniqueKeyPropertyName = determineNameOfPropertyToUse(persister, suppliedUniqueKeyPropertyName); idSelectString = persister.getSelectByUniqueKeyString(uniqueKeyPropertyName); uniqueKeyType = persister.getPropertyType(uniqueKeyPropertyName); idType = persister.getIdentifierType(); }
protected Serializable getResult(SessionImplementor session, ResultSet rs, Object entity) throws SQLException { if (!rs.next()) { throw new IdentifierGenerationException( "the inserted row could not be located by the unique key: " + uniqueKeyPropertyName); } return (Serializable) idType.nullSafeGet(rs, persister.getRootTableKeyColumnNames(), session, entity); }
private static String determineNameOfPropertyToUse( PostInsertIdentityPersister persister, String supplied) { if (supplied != null) { return supplied; } int[] naturalIdPropertyIndices = persister.getNaturalIdentifierProperties(); if (naturalIdPropertyIndices == null) { throw new IdentifierGenerationException( "no natural-id property defined; need to specify [key] in " + "generator parameters"); } if (naturalIdPropertyIndices.length > 1) { throw new IdentifierGenerationException( "select generator does not currently support composite " + "natural-id properties; need to specify [key] in generator parameters"); } ValueInclusion inclusion = persister.getPropertyInsertGenerationInclusions()[naturalIdPropertyIndices[0]]; if (inclusion != ValueInclusion.NONE) { throw new IdentifierGenerationException( "natural-id also defined as insert-generated; need to specify [key] " + "in generator parameters"); } return persister.getPropertyNames()[naturalIdPropertyIndices[0]]; }
protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object entity) throws SQLException { Object uniqueKeyValue = persister.getPropertyValue(entity, uniqueKeyPropertyName); uniqueKeyType.nullSafeSet(ps, uniqueKeyValue, 1, session); }