final ColumnInformation columnInfo = tableInfo.getColumn( Identifier.toIdentifier( column.getName(), column.isQuoted() ) ); .append( column.getQuotedName( dialect ) ) .append( ' ' ) .append( column.getSqlType( dialect, metadata ) ); String defaultValue = column.getDefaultValue(); if ( defaultValue != null ) { alter.append( " default " ).append( defaultValue ); if ( column.isNullable() ) { alter.append( dialect.getNullColumnString() ); if ( column.isUnique() ) { String keyName = Constraint.generateName( "UK_", this, column ); UniqueKey uk = getOrCreateUniqueKey( keyName ); uk.addColumn( column ); alter.append( dialect.getUniqueDelegate() if ( column.hasCheckConstraint() && dialect.supportsColumnCheck() ) { alter.append( " check(" ) .append( column.getCheckConstraint() ) .append( ")" ); String columnComment = column.getComment(); if ( columnComment != null ) { alter.append( dialect.getColumnComment( columnComment ) );
SimpleValue key = new DependantValue( buildingContext, join.getTable(), persistentClass.getIdentifier() ); Iterator mappedByColumns = otherSideProperty.getValue().getColumnIterator(); while ( mappedByColumns.hasNext() ) { Column column = (Column) mappedByColumns.next(); Column copy = new Column(); copy.setLength( column.getLength() ); copy.setScale( column.getScale() ); copy.setValue( key ); copy.setName( column.getQuotedName() ); copy.setNullable( column.isNullable() ); copy.setPrecision( column.getPrecision() ); copy.setUnique( column.isUnique() ); copy.setSqlType( column.getSqlType() ); copy.setCheckConstraint( column.getCheckConstraint() ); copy.setComment( column.getComment() ); copy.setDefaultValue( column.getDefaultValue() ); key.addColumn( copy ); persistentClass.addJoin( join ); return join;
private boolean propertyIteratorContainsColumn(Iterator propertyIterator, Column column) { for ( Iterator it = propertyIterator; it.hasNext(); ) { final Property property = (Property) it.next(); for ( Iterator<Selectable> selectableIterator = property.getColumnIterator(); selectableIterator.hasNext(); ) { final Selectable selectable = selectableIterator.next(); if ( column.equals( selectable ) ) { final Column iteratedColumn = (Column) selectable; if ( column.getValue().getTable().equals( iteratedColumn.getValue().getTable() ) ) { return true; } } } } return false; }
public String getSqlType(Dialect dialect, Mapping mapping) throws HibernateException { if ( sqlType == null ) { sqlType = dialect.getTypeName( getSqlTypeCode( mapping ), getLength(), getPrecision(), getScale() ); } return sqlType; }
private void applyComponentColumnSizeValueToJoinColumn(Column column, Ejb3JoinColumn joinColumn) { Column mappingColumn = joinColumn.getMappingColumn(); mappingColumn.setLength( column.getLength() ); mappingColumn.setPrecision( column.getPrecision() ); mappingColumn.setScale( column.getScale() ); }
/** * Shallow copy, the value is not copied */ @Override public Column clone() { Column copy = new Column(); copy.setLength( length ); copy.setScale( scale ); copy.setValue( value ); copy.setTypeIndex( typeIndex ); copy.setName( getQuotedName() ); copy.setNullable( nullable ); copy.setPrecision( precision ); copy.setUnique( unique ); copy.setSqlType( sqlType ); copy.setSqlTypeCode( sqlTypeCode ); copy.uniqueInteger = uniqueInteger; //usually useless copy.setCheckConstraint( checkConstraint ); copy.setComment( comment ); copy.setDefaultValue( defaultValue ); copy.setCustomRead( customRead ); copy.setCustomWrite( customWrite ); return copy; }
referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator(); Property referencedProperty = associatedClass.getRecursiveProperty( referencedPropertyName ); referencedEntityColumns = referencedProperty.getColumnIterator(); associatedClass.getTable().getName(), element.getColumnIterator(), referencedEntityColumns fromAndWhere = getFromAndWhereFormula( targetPropertyPersistentClass.getTable() .getQualifiedTableName() .toString(), element.getColumnIterator(), associatedClass.getIdentifier().getColumnIterator() ); while ( properties.hasNext() ) { Property current = (Property) properties.next(); Property newProperty = new Property(); newProperty.setCascade( current.getCascade() ); newProperty.setValueGenerationStrategy( current.getValueGenerationStrategy() ); newProperty.setInsertable( false ); String formulaString; if ( current instanceof Column ) { formulaString = ( (Column) current ).getQuotedName();
explicitDiscriminatorColumnName = column.getQuotedName( factory.getDialect() ); discriminatorAlias = column.getAlias( factory.getDialect(), persistentClass.getRootTable() ); String[] keyColReaders = new String[idColumnSpan]; String[] keyColReaderTemplates = new String[idColumnSpan]; Iterator cItr = key.getColumnIterator(); for ( int k = 0; k < idColumnSpan; k++ ) { Column column = (Column) cItr.next(); keyCols[k] = column.getQuotedName( factory.getDialect() ); keyColReaders[k] = column.getReadExpr( factory.getDialect() ); keyColReaderTemplates[k] = column.getTemplate( factory.getDialect(), factory.getSqlFunctionRegistry() ); keyCols[k] = column.getQuotedName( factory.getDialect() ); keyColReaders[k] = column.getReadExpr( factory.getDialect() ); keyColReaderTemplates[k] = column.getTemplate( factory.getDialect(), factory.getSqlFunctionRegistry() ); subclassTableNames.add( tableName ); String[] key = new String[idColumnSpan]; Iterator cItr = tab.getPrimaryKey().getColumnIterator(); for ( int k = 0; k < idColumnSpan; k++ ) { key[k] = ( (Column) cItr.next() ).getQuotedName( factory.getDialect() ); subclassTableNames.add( joinTableName ); String[] key = new String[idColumnSpan]; Iterator citer = joinTable.getPrimaryKey().getColumnIterator(); for ( int k = 0; k < idColumnSpan; k++ ) { key[k] = ( (Column) citer.next() ).getQuotedName( factory.getDialect() );
if ( hasPrimaryKey() && identityColumn ) { pkname = ( (Column) getPrimaryKey().getColumnIterator().next() ).getQuotedName( dialect ); buf.append( col.getQuotedName( dialect ) ) .append( ' ' ); if ( identityColumn && col.getQuotedName( dialect ).equals( pkname ) ) { if ( dialect.getIdentityColumnSupport().hasDataTypeInIdentityColumn() ) { buf.append( col.getSqlType( dialect, p ) ); .append( dialect.getIdentityColumnSupport().getIdentityColumnString( col.getSqlTypeCode( p ) ) ); buf.append( col.getSqlType( dialect, p ) ); String defaultValue = col.getDefaultValue(); if ( defaultValue != null ) { buf.append( " default " ).append( defaultValue ); if ( col.isNullable() ) { if ( col.isUnique() ) { if ( col.hasCheckConstraint() && dialect.supportsColumnCheck() ) { buf.append( " check (" ) .append( col.getCheckConstraint() ) .append( ")" ); String columnComment = col.getComment();
this.navigableRole = new NavigableRole( persistentClass.getEntityName() ); Iterator iter = persistentClass.getIdentifier().getColumnIterator(); int i = 0; while ( iter.hasNext() ) { Column col = (Column) iter.next(); rootTableKeyColumnNames[i] = col.getQuotedName( dialect ); rootTableKeyColumnReaders[i] = col.getReadExpr( dialect ); rootTableKeyColumnReaderTemplates[i] = col.getTemplate( dialect, factory.getSqlFunctionRegistry() ); identifierAliases[i] = col.getAlias( dialect, persistentClass.getRootTable() ); i++; versionColumnName = ( (Column) persistentClass.getVersion().getColumnIterator().next() ).getQuotedName( dialect ); colNames[k] = col.getQuotedName( dialect ); colReaderTemplates[k] = col.getTemplate( dialect, factory.getSqlFunctionRegistry() ); colWriters[k] = col.getWriteExpr(); String colName = col.getQuotedName( dialect ); readers[l] = col.getReadExpr( dialect ); String readerTemplate = col.getTemplate( dialect, factory.getSqlFunctionRegistry() ); readerTemplates[l] = readerTemplate; columnReaderTemplates.add( readerTemplate );
Column pkColumn = (Column) table.getPrimaryKey().getColumns().iterator().next(); pkColName = pkColumn.getQuotedName( dialect ); String colName = col.getQuotedName( dialect ); if ( dialect.getIdentityColumnSupport().hasDataTypeInIdentityColumn() ) { buf.append( col.getSqlType( dialect, metadata ) ); .append( dialect.getIdentityColumnSupport().getIdentityColumnString( col.getSqlTypeCode( metadata ) ) ); buf.append( col.getSqlType( dialect, metadata ) ); String defaultValue = col.getDefaultValue(); if ( defaultValue != null ) { buf.append( " default " ).append( defaultValue ); if ( col.isNullable() ) { buf.append( dialect.getNullColumnString() ); if ( col.isUnique() ) { if ( col.getCheckConstraint() != null && dialect.supportsColumnCheck() ) { buf.append( " check (" ) .append( col.getCheckConstraint() ) .append( ")" ); String columnComment = col.getComment();
/** * used for mappedBy cases */ public void linkValueUsingAColumnCopy(Column column, SimpleValue value) { initMappingColumn( //column.getName(), column.getQuotedName(), null, column.getLength(), column.getPrecision(), column.getScale(), getMappingColumn().isNullable(), column.getSqlType(), getMappingColumn().isUnique(), false //We do copy no strategy here ); linkWithValue( value ); }
@Override public void doTestWork(StandardServiceRegistry ssr) { MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) .addAnnotatedClass( E1.class ) .buildMetadata(); metadata.validate(); PersistentClass entityBinding = metadata.getEntityBinding( E1.class.getName() ); org.hibernate.mapping.Column idColumn = extractColumn( entityBinding.getIdentifier().getColumnIterator() ); assertTrue( isQuoted( idColumn.getSqlType(), ssr ) ); org.hibernate.mapping.Column otherColumn = extractColumn( entityBinding.getProperty( "other" ).getColumnIterator() ); assertTrue( isQuoted( otherColumn.getSqlType(), ssr ) ); } }
private void alignColumns(Table referencedTable) { final int referencedPkColumnSpan = referencedTable.getPrimaryKey().getColumnSpan(); if ( referencedPkColumnSpan != getColumnSpan() ) { StringBuilder sb = new StringBuilder(); sb.append( "Foreign key (" ).append( getName() ).append( ":" ) .append( getTable().getName() ) .append( " [" ); appendColumns( sb, getColumnIterator() ); sb.append( "])" ) .append( ") must have same number of columns as the referenced primary key (" ) .append( referencedTable.getName() ) .append( " [" ); appendColumns( sb, referencedTable.getPrimaryKey().getColumnIterator() ); sb.append( "])" ); throw new MappingException( sb.toString() ); } Iterator fkCols = getColumnIterator(); Iterator pkCols = referencedTable.getPrimaryKey().getColumnIterator(); while ( pkCols.hasNext() ) { ( (Column) fkCols.next() ).setLength( ( (Column) pkCols.next() ).getLength() ); } }
Iterator idColumnsIt = referencedEntity.getKey().getColumnIterator(); while ( idColumnsIt.hasNext() ) { idColumns.add( (Column) idColumnsIt.next() ); throw new MappingException( "Unable to find column with logical name: " + columns[0].getReferencedColumn() + " in " + referencedEntity.getTable() + " and its related " + "supertables and secondary tables" ); ( (PersistentClass) columnOwner ).getTable() : ( (Join) columnOwner ).getTable(); + logicalReferencedColumnName + " in " + matchingTable.getName() ); Column refCol = new Column( referencedColumnName ); boolean contains = idColumns.contains( refCol ); if ( !contains ) {
@Test public void testDefaultValue() throws Exception { Join join = (Join) metadata().getEntityBinding( Life.class.getName() ).getJoinClosureIterator().next(); assertEquals( "ExtendedLife", join.getTable().getName() ); org.hibernate.mapping.Column owner = new org.hibernate.mapping.Column(); owner.setName( "LIFE_ID" ); assertTrue( join.getTable().getPrimaryKey().containsColumn( owner ) ); Session s = openSession(); Transaction tx = s.beginTransaction(); Life life = new Life(); life.duration = 15; life.fullDescription = "Long long description"; s.persist( life ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Query q = s.createQuery( "from " + Life.class.getName() ); life = (Life) q.uniqueResult(); assertEquals( "Long long description", life.fullDescription ); tx.commit(); s.close(); }
protected void checkDefaultJoinTableAndAllColumnNames( Metadata metadata, Class<?> ownerEntityClass, String ownerCollectionPropertyName, String expectedCollectionTableName, String ownerForeignKeyNameExpected, String[] columnNames) { final org.hibernate.mapping.Collection collection = metadata.getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); final org.hibernate.mapping.Table table = collection.getCollectionTable(); assertEquals( expectedCollectionTableName, table.getName() ); // The default owner and inverse join columns can only be computed if they have PK with 1 column. assertEquals( 1, collection.getOwner().getKey().getColumnSpan() ); assertEquals( ownerForeignKeyNameExpected, collection.getKey().getColumnIterator().next().getText() ); int columnNumber = table.getColumnSpan(); for ( int i = 0; i < columnNumber; i++ ) { assertEquals( columnNames[i], table.getColumn( i + 1 ).getName()); } }
/** * Adds <code>column</code> element with the following attributes (unless empty): <code>name</code>, * <code>length</code>, <code>scale</code>, <code>precision</code>, <code>sql-type</code>, <code>read</code> * and <code>write</code>. * * @param anyMapping Parent element. * @param column Column descriptor. */ public static void addColumn(Element anyMapping, Column column) { addColumn( anyMapping, column.getName(), column.getLength(), column.getScale(), column.getPrecision(), column.getSqlType(), column.getCustomRead(), column.getCustomWrite(), column.isQuoted() ); }
table.setPrimaryKey( new PrimaryKey( table ) ); segmentColumnName, StringType.INSTANCE, database.getDialect().getTypeName( Types.VARCHAR, keySize, 0, 0 ) ); pkColumn.setNullable( false ); table.addColumn( pkColumn ); table.getPrimaryKey().addColumn( pkColumn ); valueColumnName + " from " + jdbcEnvironment.getDialect().appendLockHint( LockMode.PESSIMISTIC_WRITE, tableName ) + " where " + segmentColumnName + " = '" + segmentName + "'" + jdbcEnvironment.getDialect().getForUpdateString();
@Test public void testJoinTableIndex(){ PersistentClass entity = metadata().getEntityBinding( Importer.class.getName() ); Property property = entity.getProperty( "cars" ); Bag set = (Bag)property.getValue(); Table collectionTable = set.getCollectionTable(); Iterator<Index> itr = collectionTable.getIndexIterator(); assertTrue( itr.hasNext() ); Index index = itr.next(); assertFalse( itr.hasNext() ); assertTrue( "index name is not generated", StringHelper.isNotEmpty( index.getName() ) ); assertEquals( 1, index.getColumnSpan() ); Iterator<Column> columnIterator = index.getColumnIterator(); Column column = columnIterator.next(); assertEquals( "importers_id", column.getName() ); assertSame( collectionTable, index.getTable() ); }