Value value,
Collection collection,
String targetPropertyName,
PersistentClass associatedClass,
Mappings mappings) {
Value element = collection.getElement();
String fromAndWhere = null;
if ( !( element instanceof OneToMany ) ) {
String referencedPropertyName = null;
if ( element instanceof ToOne ) {
referencedPropertyName = ( (ToOne) element ).getReferencedPropertyName();
}
else if ( element instanceof DependantValue ) {
//TODO this never happen I think
if ( propertyName != null ) {
referencedPropertyName = collection.getReferencedPropertyName();
}
else {
throw new AnnotationException( "SecondaryTable JoinColumn cannot reference a non primary key" );
}
}
Iterator referencedEntityColumns;
if ( referencedPropertyName == null ) {
referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator();
}
else {
Property referencedProperty = associatedClass.getRecursiveProperty( referencedPropertyName );
referencedEntityColumns = referencedProperty.getColumnIterator();
}
String alias = "$alias$";
StringBuilder fromAndWhereSb = new StringBuilder( " from " )
.append( associatedClass.getTable().getName() )
//.append(" as ") //Oracle doesn't support it in subqueries
.append( " " )
.append( alias ).append( " where " );
Iterator collectionTableColumns = element.getColumnIterator();
while ( collectionTableColumns.hasNext() ) {
Column colColumn = (Column) collectionTableColumns.next();
Column refColumn = (Column) referencedEntityColumns.next();
fromAndWhereSb.append( alias ).append( '.' ).append( refColumn.getQuotedName() )
.append( '=' ).append( colColumn.getQuotedName() ).append( " and " );