Java Code Examples for org.hibernate.dialect.Dialect.getAddForeignKeyConstraintString()

The following are Jave code examples for showing how to use getAddForeignKeyConstraintString() of the org.hibernate.dialect.Dialect class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: lams   File: ForeignKey.java   View Source Code Vote up 6 votes
public String sqlConstraintString(Dialect dialect, String constraintName, String defaultCatalog, String defaultSchema) {
	String[] cols = new String[ getColumnSpan() ];
	String[] refcols = new String[ getColumnSpan() ];
	int i=0;
	Iterator refiter = null;
	if(isReferenceToPrimaryKey() ) {
		refiter = referencedTable.getPrimaryKey().getColumnIterator();
	} 
	else {
		refiter = referencedColumns.iterator();
	}
	
	Iterator iter = getColumnIterator();
	while ( iter.hasNext() ) {
		cols[i] = ( (Column) iter.next() ).getQuotedName(dialect);
		refcols[i] = ( (Column) refiter.next() ).getQuotedName(dialect);
		i++;
	}
	String result = dialect.getAddForeignKeyConstraintString(
		constraintName, cols, referencedTable.getQualifiedName(dialect, defaultCatalog, defaultSchema), refcols, isReferenceToPrimaryKey()
	);
	return cascadeDeleteEnabled && dialect.supportsCascadeDelete() ? 
		result + " on delete cascade" : 
		result;
}
 
Example 2
Project: lams   File: ForeignKey.java   View Source Code Vote up 5 votes
public String sqlConstraintStringInAlterTable(Dialect dialect) {
	String[] columnNames = new String[ getColumnSpan() ];
	String[] targetColumnNames = new String[ getColumnSpan() ];
	int i=0;
	Iterator<Column> itTargetColumn = getTargetColumns().iterator();
	for ( Column column : getColumns() ) {
		if ( ! itTargetColumn.hasNext() ) {
			throw new MappingException( "More constraint columns that foreign key target columns." );
		}
		columnNames[i] = column.getColumnName().encloseInQuotesIfQuoted( dialect );
		targetColumnNames[i] = ( itTargetColumn.next() ).getColumnName().encloseInQuotesIfQuoted( dialect );
		i++;
	}
	if ( itTargetColumn.hasNext() ) {
		throw new MappingException( "More foreign key target columns than constraint columns." );
	}
	StringBuilder sb =
			new StringBuilder(
					dialect.getAddForeignKeyConstraintString(
							getName(),
							columnNames,
							targetTable.getQualifiedName( dialect ),
							targetColumnNames,
							this.targetColumns == null
					)
			);
	// TODO: If a dialect does not support cascade-delete, can it support other actions? (HHH-6428)
	// For now, assume not.
	if ( dialect.supportsCascadeDelete() ) {
		if ( deleteRule != ReferentialAction.NO_ACTION ) {
			sb.append( ON_DELETE ).append( deleteRule.getActionString() );
		}
		if ( updateRule != ReferentialAction.NO_ACTION ) {
			sb.append( ON_UPDATE ).append( updateRule.getActionString() );
		}
	}
	return sb.toString();
}