public OneToManyPersister( Collection collectionBinding, CollectionDataAccess cacheAccessStrategy, PersisterCreationContext creationContext) throws MappingException, CacheException { super( collectionBinding, cacheAccessStrategy, creationContext ); cascadeDeleteEnabled = collectionBinding.getKey().isCascadeDeleteEnabled() && creationContext.getSessionFactory().getDialect().supportsCascadeDelete(); keyIsNullable = collectionBinding.getKey().isNullable(); keyIsUpdateable = collectionBinding.getKey().isUpdateable(); }
/** * Returns whether the revision type column of the map-key is part of the collection table's primary key. * * NOTE: It is safe to call this method, even for non-map collection types as this method will always return * {@code false} for non-map collection types. * * @return {@code true} if the revision type should be part of the primary key, otherwise {@code false}. */ private boolean isKeyRevisionTypeInId() { if ( propertyValue instanceof org.hibernate.mapping.Map ) { final Type type = propertyValue.getKey().getType(); if ( !type.isComponentType() && !type.isAssociationType() ) { return ( type instanceof MaterializedClobType ) || ( type instanceof MaterializedNClobType ); } } return false; }
@SuppressWarnings({"unchecked"}) private String searchMappedByKey(PersistentClass referencedClass, Collection collectionValue) { final Iterator<Value> assocIdClassProps = referencedClass.getKeyClosureIterator(); while ( assocIdClassProps.hasNext() ) { final Value value = assocIdClassProps.next(); // make sure its a 'Component' because IdClass is registered as this type. if ( value instanceof Component ) { final Component component = (Component) value; final Iterator<Property> componentPropertyIterator = component.getPropertyIterator(); while ( componentPropertyIterator.hasNext() ) { final Property property = componentPropertyIterator.next(); final Iterator<Selectable> propertySelectables = property.getValue().getColumnIterator(); final Iterator<Selectable> collectionSelectables = collectionValue.getKey().getColumnIterator(); if ( Tools.iteratorsContentEqual( propertySelectables, collectionSelectables ) ) { return property.getName(); } } } } return null; }
@SuppressWarnings({"unchecked"}) private String searchMappedBy(PersistentClass referencedClass, Collection collectionValue) { final Iterator<Property> assocClassProps = referencedClass.getPropertyIterator(); while ( assocClassProps.hasNext() ) { final Property property = assocClassProps.next(); if ( Tools.iteratorsContentEqual( property.getValue().getColumnIterator(), collectionValue.getKey().getColumnIterator() ) ) { return property.getName(); } } // HHH-7625 // Support ToOne relations with mappedBy that point to an @IdClass key property. return searchMappedByKey( referencedClass, collectionValue ); }
public void validate(Mapping mapping) throws MappingException { assert getKey() != null : "Collection key not bound : " + getRole(); assert getElement() != null : "Collection element not bound : " + getRole(); if ( !getKey().isValid( mapping ) ) { throw new MappingException( "collection foreign key mapping has wrong number of columns: " + getRole() + " type: " + getKey().getType().getName() ); } if ( !getElement().isValid( mapping ) ) { throw new MappingException( "collection element mapping has wrong number of columns: " + getRole() + " type: " + getElement().getType().getName() ); } checkColumnDuplication(); }
private void checkColumnDuplication() throws MappingException { HashSet cols = new HashSet(); checkColumnDuplication( cols, getKey() ); if ( isIndexed() ) { checkColumnDuplication( cols, ( (IndexedCollection) this ).getIndex() ); } if ( isIdentified() ) { checkColumnDuplication( cols, ( (IdentifierCollection) this ).getIdentifier() ); } if ( !isOneToMany() ) { checkColumnDuplication( cols, getElement() ); } }
protected void validateCustomerIndustries(Metadata metadata) { final Collection collectionBinding = metadata.getCollectionBinding( Customer.class.getName() + ".industries" ); assertNotNull( collectionBinding ); validateCustomerIndustriesTableName( collectionBinding.getCollectionTable().getQuotedName() ); assertEquals( 1, collectionBinding.getKey().getColumnSpan() ); validateCustomerIndustriesKeyColumn( (Column) collectionBinding.getKey().getColumnIterator().next() ); assertEquals( 1, collectionBinding.getElement().getColumnSpan() ); validateCustomerIndustriesElementColumn( (Column) collectionBinding.getElement().getColumnIterator().next() ); }
protected void validateCustomerOrders(Metadata metadata) { final Collection collectionBinding = metadata.getCollectionBinding( Customer.class.getName() + ".orders" ); assertNotNull( collectionBinding ); validateCustomerOrdersTableName( collectionBinding.getCollectionTable().getQuotedName() ); assertEquals( 1, collectionBinding.getKey().getColumnSpan() ); validateCustomerOrdersKeyColumn( (Column) collectionBinding.getKey().getColumnIterator().next() ); assertEquals( 1, collectionBinding.getElement().getColumnSpan() ); validateCustomerOrdersElementColumn( (Column) collectionBinding.getElement().getColumnIterator().next() ); }
protected void validateCustomerRegisteredTrademarks(Metadata metadata) { final Collection collectionBinding = metadata.getCollectionBinding( Customer.class.getName() + ".registeredTrademarks" ); assertNotNull( collectionBinding ); validateCustomerRegisteredTrademarksTableName( collectionBinding.getCollectionTable().getQuotedName() ); assertEquals( 1, collectionBinding.getKey().getColumnSpan() ); validateCustomerRegisteredTrademarksKeyColumn( (Column) collectionBinding.getKey().getColumnIterator().next() ); assertEquals( 1, collectionBinding.getElement().getColumnSpan() ); validateCustomerRegisteredTrademarksElementColumn( (Column) collectionBinding.getElement() .getColumnIterator() .next() ); }
protected void validateCustomerAddresses(Metadata metadata) { final Collection collectionBinding = metadata.getCollectionBinding( Customer.class.getName() + ".addresses" ); assertNotNull( collectionBinding ); validateCustomerAddressesTableName( collectionBinding.getCollectionTable().getQuotedName() ); assertEquals( 1, collectionBinding.getKey().getColumnSpan() ); validateCustomerAddressesKeyColumn( (Column) collectionBinding.getKey().getColumnIterator().next() ); assertEquals( 3, collectionBinding.getElement().getColumnSpan() ); validateCustomerAddressesElementComponent( assertTyping( Component.class, collectionBinding.getElement() ) ); }
Iterator mappedByColumns; if ( property.getValue() instanceof Collection ) { mappedByColumns = ( (Collection) property.getValue() ).getKey().getColumnIterator();
protected void createBackReferences() { if ( collectionBinding.isOneToMany() && !collectionBinding.isInverse() && !collectionBinding.getKey().isNullable() ) { // for non-inverse one-to-many, with a not-null fk, add a backref! String entityName = ( (OneToMany) collectionBinding.getElement() ).getReferencedEntityName(); PersistentClass referenced = mappingDocument.getMetadataCollector().getEntityBinding( entityName ); Backref prop = new Backref(); prop.setName( '_' + collectionBinding.getOwnerEntityName() + "." + pluralAttributeSource.getName() + "Backref" ); prop.setUpdateable( false ); prop.setSelectable( false ); prop.setCollectionRole( collectionBinding.getRole() ); prop.setEntityName( collectionBinding.getOwner().getEntityName() ); prop.setValue( collectionBinding.getKey() ); referenced.addProperty( prop ); log.debugf( "Added virtual backref property [%s] : %s", prop.getName(), pluralAttributeSource.getAttributeRole().getFullPath() ); } }
public void doSecondPass(java.util.Map persistentClasses) throws MappingException { final boolean debugEnabled = LOG.isDebugEnabled(); if ( debugEnabled ) { LOG.debugf( "Second pass for collection: %s", collection.getRole() ); } secondPass( persistentClasses, localInheritedMetas ); // using local since the inheritedMetas at this point is not the correct map since it is always the empty map collection.createAllKeys(); if ( debugEnabled ) { String msg = "Mapped collection key: " + columns( collection.getKey() ); if ( collection.isIndexed() ) msg += ", index: " + columns( ( (IndexedCollection) collection ).getIndex() ); if ( collection.isOneToMany() ) { msg += ", one-to-many: " + ( (OneToMany) collection.getElement() ).getReferencedEntityName(); } else { msg += ", element: " + columns( collection.getElement() ); } LOG.debug( msg ); } }
protected void checkDefaultJoinColumnName( Class<?> ownerEntityClass, String ownerCollectionPropertyName, String ownerForeignKeyNameExpected) { final org.hibernate.mapping.Collection ownerCollection = metadata().getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); // The default owner join column can only be computed if it has a PK with 1 column. assertEquals ( 1, ownerCollection.getOwner().getKey().getColumnSpan() ); assertEquals( ownerForeignKeyNameExpected, ownerCollection.getKey().getColumnIterator().next().getText() ); boolean hasOwnerFK = false; for ( Iterator it=ownerCollection.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) { final ForeignKey fk = (ForeignKey) it.next(); assertSame( ownerCollection.getCollectionTable(), fk.getTable() ); if ( fk.getColumnSpan() > 1 ) { continue; } if ( fk.getColumn( 0 ).getText().equals( ownerForeignKeyNameExpected ) ) { assertSame( ownerCollection.getOwner().getTable(), fk.getReferencedTable() ); hasOwnerFK = true; } } assertTrue( hasOwnerFK ); }
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()); } }
bindCollectionSecondPass( collection, null, fkJoinColumns, cascadeDeleteEnabled, property, propertyHolder, buildingContext ); if ( !collection.isInverse() && !collection.getKey().isNullable() ) { prop.setCollectionRole( collection.getRole() ); prop.setEntityName( collection.getOwner().getEntityName() ); prop.setValue( collection.getKey() ); referenced.addProperty( prop );
@Override public void doSecondPass(Map persistentClasses) throws org.hibernate.MappingException { bindCollectionTable(); bindCollectionKey(); bindCollectionIdentifier(); bindCollectionIndex(); bindCollectionElement(); createBackReferences(); collectionBinding.createAllKeys(); if ( debugEnabled ) { log.debugf( "Mapped collection : " + getPluralAttributeSource().getAttributeRole().getFullPath() ); log.debugf( " + table -> " + getCollectionBinding().getTable().getName() ); log.debugf( " + key -> " + columns( getCollectionBinding().getKey() ) ); if ( getCollectionBinding().isIndexed() ) { log.debugf( " + index -> " + columns( ( (IndexedCollection) getCollectionBinding() ).getIndex() ) ); } if ( getCollectionBinding().isOneToMany() ) { log.debugf( " + one-to-many -> " + ( (OneToMany) getCollectionBinding().getElement() ).getReferencedEntityName() ); } else { log.debugf( " + element -> " + columns( getCollectionBinding().getElement() ) ); } } }
MetadataTools.getColumnNameIterator( propertyValue.getKey().getColumnIterator() ), referencingIdMapping );