@Override
  public AssociationKey getAssociationKey() {
    final AssociationType type = getType();

    if (type.isAnyType()) {
      return new AssociationKey(
          JoinHelper.getLHSTableName(type, attributeNumber(), (OuterJoinLoadable) getSource()),
          JoinHelper.getLHSColumnNames(
              type, attributeNumber(), 0, (OuterJoinLoadable) getSource(), sessionFactory()));
    }

    final Joinable joinable = type.getAssociatedJoinable(sessionFactory());

    if (type.getForeignKeyDirection() == ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT) {
      final String lhsTableName;
      final String[] lhsColumnNames;

      if (joinable.isCollection()) {
        final QueryableCollection collectionPersister = (QueryableCollection) joinable;
        lhsTableName = collectionPersister.getTableName();
        lhsColumnNames = collectionPersister.getElementColumnNames();
      } else {
        final OuterJoinLoadable entityPersister = (OuterJoinLoadable) source();
        lhsTableName = getLHSTableName(type, attributeNumber(), entityPersister);
        lhsColumnNames =
            getLHSColumnNames(type, attributeNumber(), entityPersister, sessionFactory());
      }
      return new AssociationKey(lhsTableName, lhsColumnNames);
    } else {
      return new AssociationKey(joinable.getTableName(), getRHSColumnNames(type, sessionFactory()));
    }
  }
Example #2
0
 /** Used to detect circularities in the joined graph, note that this method is side-effecty */
 protected boolean isDuplicateAssociation(
     final String lhsTable, final String[] lhsColumnNames, final AssociationType type) {
   final String foreignKeyTable;
   final String[] foreignKeyColumns;
   if (type.getForeignKeyDirection() == ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT) {
     foreignKeyTable = lhsTable;
     foreignKeyColumns = lhsColumnNames;
   } else {
     foreignKeyTable = type.getAssociatedJoinable(getFactory()).getTableName();
     foreignKeyColumns = JoinHelper.getRHSColumnNames(type, getFactory());
   }
   return isDuplicateAssociation(foreignKeyTable, foreignKeyColumns);
 }