From: <hib...@li...> - 2006-03-22 17:31:18
|
Author: ste...@jb... Date: 2006-03-22 12:31:13 -0500 (Wed, 22 Mar 2006) New Revision: 9674 Modified: trunk/Hibernate3/src/org/hibernate/id/SelectGenerator.java Log: HHH-1592 : SelectGenerator & natural-id Modified: trunk/Hibernate3/src/org/hibernate/id/SelectGenerator.java =================================================================== --- trunk/Hibernate3/src/org/hibernate/id/SelectGenerator.java 2006-03-22 14:57:59 UTC (rev 9673) +++ trunk/Hibernate3/src/org/hibernate/id/SelectGenerator.java 2006-03-22 17:31:13 UTC (rev 9674) @@ -34,29 +34,66 @@ } protected String getSQL(PostInsertIdentityPersister persister) { - return persister.getSelectByUniqueKeyString(uniqueKeyPropertyName); + if ( entityName == null ) { + entityName = persister.getEntityName(); + } + if ( uniqueKeyPropertyName == null ) { + 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" + ); + } + if ( persister.getPropertyInsertGeneration() [ naturalIdPropertyIndices[0] ] ) { + throw new IdentifierGenerationException( + "natural-id also defined as insert-generated; need to specify [key] " + + "in generator parameters" + ); + } + uniqueKeyPropertyName = persister.getPropertyNames() [ naturalIdPropertyIndices[0] ]; + } + return persister.getSelectByUniqueKeyString( uniqueKeyPropertyName ); } - protected void bindParameters(SessionImplementor session, PreparedStatement ps, Object object, PostInsertIdentityPersister persister) - throws SQLException { + protected void bindParameters( + SessionImplementor session, + PreparedStatement ps, + Object object, + PostInsertIdentityPersister persister) throws SQLException { Type uniqueKeyPropertyType = session.getFactory() - .getClassMetadata(entityName) - .getPropertyType(uniqueKeyPropertyName); - Object uniqueKeyValue = persister.getPropertyValue( object, uniqueKeyPropertyName, session.getEntityMode() ); + .getClassMetadata( entityName ) + .getPropertyType( uniqueKeyPropertyName ); + Object uniqueKeyValue = persister.getPropertyValue( + object, + uniqueKeyPropertyName, + session.getEntityMode() + ); uniqueKeyPropertyType.nullSafeSet( ps, uniqueKeyValue, 1, session ); } - protected Serializable getResult(SessionImplementor session, ResultSet rs, Object object, PostInsertIdentityPersister persister) - throws SQLException { + protected Serializable getResult( + SessionImplementor session, + ResultSet rs, + Object object, + PostInsertIdentityPersister persister) throws SQLException { if ( !rs.next() ) { - throw new IdentifierGenerationException( "the inserted row could not be located by the unique key: " + uniqueKeyPropertyName ); + throw new IdentifierGenerationException( + "the inserted row could not be located by the unique key: " + + uniqueKeyPropertyName + ); } - return (Serializable) idType.nullSafeGet(rs, persister.getRootTableKeyColumnNames(), session, object); + return ( Serializable ) idType.nullSafeGet( + rs, + persister.getRootTableKeyColumnNames(), + session, + object + ); } } - - - - - - |