Refine search
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() ); } }
@SuppressWarnings("unchecked") private String searchMappedBy(Iterator<Property> properties, Table collectionTable) { while ( properties.hasNext() ) { final Property property = properties.next(); if ( property.getValue() instanceof Collection ) { // The equality is intentional. We want to find a collection property with the same collection table. //noinspection ObjectEquality if ( ( (Collection) property.getValue() ).getCollectionTable() == collectionTable ) { return property.getName(); } } else if ( property.getValue() instanceof Component ) { // HHH-12240 // Should we find an embeddable, we should traverse it as well to see if the collection table // happens to be an attribute inside the embeddable rather than directly on the entity. final Component component = (Component) property.getValue(); final String mappedBy = searchMappedBy( component.getPropertyIterator(), collectionTable ); if ( mappedBy != null ) { return property.getName() + "_" + mappedBy; } } } return null; }
public void createAllKeys() throws MappingException { createForeignKeys(); if ( !isInverse() ) { createPrimaryKey(); } }
public Component(MetadataBuildingContext metadata, Collection collection) throws MappingException { this( metadata, collection.getCollectionTable(), collection.getOwner() ); }
private void createForeignKeys() throws MappingException { // if ( !isInverse() ) { // for inverse collections, let the "other end" handle it if ( referencedPropertyName == null ) { getElement().createForeignKey(); key.createForeignKeyOfEntity( getOwner().getEntityName() ); } // } }
PersistentClass targetPropertyPersistentClass, MetadataBuildingContext buildingContext) { Value element = collection.getElement(); String fromAndWhere = null; if ( !( element instanceof OneToMany ) ) { referencedPropertyName = collection.getReferencedPropertyName(); referencedEntityColumns = associatedClass.getIdentifier().getColumnIterator(); Property referencedProperty = associatedClass.getRecursiveProperty( referencedPropertyName ); referencedEntityColumns = referencedProperty.getColumnIterator(); associatedClass.getTable().getName(), element.getColumnIterator(), referencedEntityColumns ); if ( value instanceof ManyToOne ) { ManyToOne sourceManyToOne = (ManyToOne) sourceValue; ManyToOne targetManyToOne = new ManyToOne( getBuildingContext(), collection.getCollectionTable() ); targetManyToOne.setFetchMode( FetchMode.DEFAULT ); targetManyToOne.setLazy( true ); targetValue = new SimpleValue( getBuildingContext(), collection.getCollectionTable() ); targetValue.copyTypeFrom( sourceValue );
this.cacheAccessStrategy = cacheAccessStrategy; if ( factory.getSessionFactoryOptions().isStructuredCacheEntriesEnabled() ) { cacheEntryStructure = collectionBinding.isMap() ? StructuredMapCacheEntry.INSTANCE : StructuredCollectionCacheEntry.INSTANCE; collectionType = collectionBinding.getCollectionType(); navigableRole = new NavigableRole( collectionBinding.getRole() ); entityName = collectionBinding.getOwnerEntityName(); ownerPersister = factory.getEntityPersister( entityName ); queryLoaderName = collectionBinding.getLoaderName(); isMutable = collectionBinding.isMutable(); mappedByProperty = collectionBinding.getMappedByProperty(); Table table = collectionBinding.getCollectionTable(); fetchMode = collectionBinding.getElement().getFetchMode(); elementType = collectionBinding.getElement().getType(); isPrimitiveArray = collectionBinding.isPrimitiveArray(); isArray = collectionBinding.isArray(); subselectLoadable = collectionBinding.isSubselectLoadable(); int spacesSize = 1 + collectionBinding.getSynchronizedTables().size(); spaces = new String[spacesSize]; spaces[0] = qualifiedTableName; Iterator iter = collectionBinding.getSynchronizedTables().iterator(); for ( int i = 1; i < spacesSize; i++ ) { spaces[i] = (String) iter.next();
LOG.debugf( "Retrieving property %s.%s", associatedClass.getEntityName(), mappedByProperty ); final Property property = associatedClass.getRecursiveProperty( columns[0].getMappedBy() ); Iterator mappedByColumns; if ( property.getValue() instanceof Collection ) { Collection collection = ( (Collection) property.getValue() ); Value element = collection.getElement(); if ( element == null ) { throw new AnnotationException( "Illegal use of mappedBy on both sides of the relationship: " + associatedClass.getEntityName() + "." + mappedByProperty ); mappedByColumns = element.getColumnIterator(); mappedByColumns = property.getValue().getColumnIterator(); idColumns = referencedEntity.getKey().getColumnIterator(); idColumns = referencedEntity.getIdentifier().getColumnIterator(); Collection collection = (Collection) referencedEntity.getRecursiveProperty( propertyName ) .getValue(); referencedPropertyName = collection.getReferencedPropertyName(); if ( Ejb3JoinColumn.NO_REFERENCE == fkEnum ) {
collection.setRole(path); collection.setInverse( "true".equals( inverseNode.getValue() ) ); collection.setMutable( !"false".equals( mutableNode.getValue() ) ); collection.setOptimisticLocked( olNode == null || "true".equals( olNode.getValue() ) ); collection.setOrderBy( orderNode.getValue() ); collection.setWhere( whereNode.getValue() ); collection.setBatchSize( Integer.parseInt( batchNode.getValue() ) ); collection.setNodeName( nodeName ); String embed = node.attributeValue( "embed-xml" ); collection.setEmbedded( embed==null || "true".equals(embed) ); if ( persisterNode != null ) { try { collection.setCollectionPersisterClass( ReflectHelper.classForName( persisterNode .getValue() ) ); TypeDef typeDef = mappings.getTypeDef( typeName ); if ( typeDef != null ) { collection.setTypeName( typeDef.getTypeClass() ); collection.setTypeParameters( typeDef.getParameters() ); collection.setTypeName( typeName );
org.hibernate.mapping.OneToMany oneToMany = new org.hibernate.mapping.OneToMany( buildingContext, collection.getOwner() ); collection.setElement( oneToMany ); oneToMany.setReferencedEntityName( collectionType.getName() ); oneToMany.setIgnoreNotFound( ignoreNotFound ); jpaOrderBy.value(), associatedClass, collection.getRole() ); if ( StringHelper.isNotEmpty( orderByFragment ) ) { collection.setOrderBy( orderByFragment ); column.setPersistentClass( associatedClass, joins, inheritanceStatePerClass ); column.setJoins( joins ); collection.setCollectionTable( column.getTable() ); LOG.debugf( "Mapping collection: %s -> %s", collection.getRole(), collection.getCollectionTable().getName() ); if ( !collection.isInverse() && !collection.getKey().isNullable() ) { prop.setUpdateable( false ); prop.setSelectable( false ); prop.setCollectionRole( collection.getRole() ); prop.setEntityName( collection.getOwner().getEntityName() ); prop.setValue( collection.getKey() ); referenced.addProperty( prop );
ManyToAny anyAnn = property.getAnnotation( ManyToAny.class ); if ( LOG.isDebugEnabled() ) { String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); if ( isCollectionOfEntities && unique ) { LOG.debugf("Binding a OneToMany: %s through an association table", path); String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); throw new AnnotationException( "Use of @OneToMany or @ManyToMany targeting an unmapped class: " + path + "[" + collType + "]" String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); throw new AnnotationException( "@JoinTable is mandatory when @ManyToAny is used: " + path JoinTable joinTableAnn = parentPropertyHolder.getJoinTable( property ); if ( joinTableAnn != null && joinTableAnn.inverseJoinColumns().length > 0 ) { String path = collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName(); throw new AnnotationException( "Use of @JoinTable.inverseJoinColumns targeting an unmapped class: " + path + "[" + collType + "]" throw new AnnotationException( "Collection of elements must not have mappedBy or association reference an unmapped entity: " + collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName() "mappedBy reference an unknown target entity property: " + collType + "." + joinColumns[0].getMappedBy() + " in " + collValue.getOwnerEntityName() + "." + joinColumns[0].getPropertyName()
collection.setRole( StringHelper.qualify( className, path ) ); collection.setInverse( "true".equals( inverseNode.getValue() ) ); collection.setOptimisticLocked( olNode == null || "true".equals( olNode.getValue() ) ); collection.setOrderBy( orderNode.getValue() ); collection.setWhere( whereNode.getValue() ); collection.setBatchSize( Integer.parseInt( batchNode.getValue() ) ); collection.setNodeName( nodeName ); String embed = node.attributeValue( "embed-xml" ); collection.setEmbedded( embed==null || "true".equals(embed) ); if ( persisterNode != null ) { try { collection.setCollectionPersisterClass( ReflectHelper.classForName( persisterNode .getValue() ) ); if ( typeNode != null ) collection.setTypeName( typeNode.getValue() ); collection.setSubselectLoadable(true); collection.getOwner().setSubselectLoadableCollections(true); OneToMany oneToMany = new OneToMany( collection.getOwner() ); collection.setElement( oneToMany ); bindOneToMany( oneToManyNode, oneToMany, mappings );
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() ); }
private void bindCollectionMetadata(MappingDocument mappingDocument, PluralAttributeSource source, Collection binding) { binding.setRole( source.getAttributeRole().getFullPath() ); binding.setInverse( source.isInverse() ); binding.setMutable( source.isMutable() ); binding.setOptimisticLocked( source.isIncludedInOptimisticLocking() ); binding.setCollectionPersisterClass( mappingDocument.getBootstrapContext().getClassLoaderAccess().classForName( mappingDocument.qualifyClassName( source.getCustomPersisterClassName() ) binding.setTypeName( typeName ); binding.setTypeParameters( typeParameters ); binding.setLazy( true ); binding.setExtraLazy( source.getFetchCharacteristics().isExtraLazy() ); binding.setLazy( false ); binding.setFetchMode( FetchMode.SELECT ); break; binding.setFetchMode( FetchMode.JOIN ); break; binding.setFetchMode( FetchMode.SELECT ); binding.setBatchSize( source.getFetchCharacteristics().getBatchSize() ); break; binding.setFetchMode( FetchMode.SELECT ); binding.setSubselectLoadable( true );
final String mappedBy = columns[0].getMappedBy(); if ( StringHelper.isNotEmpty( mappedBy ) ) { final Property property = referencedEntity.getRecursiveProperty( mappedBy ); Iterator mappedByColumns; if ( property.getValue() instanceof Collection ) { mappedByColumns = ( (Collection) property.getValue() ).getKey().getColumnIterator(); Iterator joinsIt = referencedEntity.getJoinIterator(); KeyValue key = null; while ( joinsIt.hasNext() ) { if ( key == null ) key = property.getPersistentClass().getIdentifier(); mappedByColumns = key.getColumnIterator();
String inverseForeignKeyNameExpected) { final org.hibernate.mapping.Collection collection = metadata().getCollectionBinding( ownerEntityClass.getName() + '.' + ownerCollectionPropertyName ); final org.hibernate.mapping.Table table = collection.getCollectionTable(); assertEquals( expectedCollectionTableName, table.getName() ); assertEquals ( 1, ownerCollection.getOwner().getKey().getColumnSpan() ); assertEquals( ownerForeignKeyNameExpected, ownerCollection.getKey().getColumnIterator().next().getText() ); final EntityType associatedEntityType = (EntityType) ownerCollection.getElement().getType(); final PersistentClass associatedPersistentClass = metadata().getEntityBinding( associatedEntityType.getAssociatedEntityName() ); assertEquals( 1, associatedPersistentClass.getKey().getColumnSpan() ); if ( inverseCollectionPropertyName != null ) { final org.hibernate.mapping.Collection inverseCollection = metadata().getCollectionBinding( associatedPersistentClass.getEntityName() + '.' + inverseCollectionPropertyName ); assertEquals( inverseForeignKeyNameExpected, inverseCollection.getKey().getColumnIterator().next().getText() ); for ( Iterator it=ownerCollection.getCollectionTable().getForeignKeyIterator(); it.hasNext(); ) { final ForeignKey fk = (ForeignKey) it.next(); assertSame( ownerCollection.getCollectionTable(), fk.getTable() ); if ( fk.getColumnSpan() > 1 ) { continue; assertSame( ownerCollection.getOwner().getTable(), fk.getReferencedTable() ); hasOwnerFK = true;
); if ( propRef != null ) { collValue.setReferencedPropertyName( propRef ); buildingContext.getMetadataCollector().addPropertyReference( collValue.getOwnerEntityName(), propRef ); String propRef = collValue.getReferencedPropertyName(); if ( propRef == null ) { keyVal = collValue.getOwner().getIdentifier(); keyVal = (KeyValue) collValue.getOwner() .getReferencedProperty( propRef ) .getValue(); DependantValue key = new DependantValue( buildingContext, collValue.getCollectionTable(), keyVal ); key.setTypeName( null ); Ejb3Column.checkPropertyConsistency( joinColumns, collValue.getOwnerEntityName() ); key.setNullable( joinColumns.length == 0 || joinColumns[0].isNullable() ); key.setUpdateable( joinColumns.length == 0 || joinColumns[0].isUpdatable() ); key.setCascadeDeleteEnabled( cascadeDeleteEnabled ); collValue.setKey( key ); if ( property != null ) { final ForeignKey fk = property.getAnnotation( ForeignKey.class );
/** * Creates the DependentValue object that forms a primary key reference for the collection. * * @param mappings * @param property The grails property * @param collection The collection object * @param persistentClasses * @return The DependantValue (key) */ private static DependantValue createPrimaryKeyValue(Mappings mappings, @SuppressWarnings("unused") GrailsDomainClassProperty property, Collection collection, @SuppressWarnings("unused") Map<?, ?> persistentClasses) { KeyValue keyValue; DependantValue key; String propertyRef = collection.getReferencedPropertyName(); // this is to support mapping by a property if (propertyRef == null) { keyValue = collection.getOwner().getIdentifier(); } else { keyValue = (KeyValue) collection.getOwner().getProperty(propertyRef).getValue(); } if (LOG.isDebugEnabled()) LOG.debug("[GrailsDomainBinder] creating dependant key value to table [" + keyValue.getTable().getName() + "]"); key = new DependantValue(mappings, collection.getCollectionTable(), keyValue); key.setTypeName(null); // make nullable and non-updateable key.setNullable(true); key.setUpdateable(false); return key; }
collection.setRole(qualify(property.getOwner().getName(), propertyName)); collection.setFetchMode(FetchMode.JOIN); collection.setFetchMode(pc.getFetchMode()); collection.setFetchMode(FetchMode.DEFAULT); collection.setOrphanDelete(pc.getCascade().equals(CASCADE_ALL_DELETE_ORPHAN)); OneToMany oneToMany = new OneToMany(mappings, collection.getOwner()); collection.setElement(oneToMany); bindOneToMany((org.grails.datastore.mapping.model.types.OneToMany) property, oneToMany, mappings); } else { bindCollectionTable(property, mappings, collection, owner.getTable(), sessionFactoryBeanName); collection.setInverse(true); collection.setBatchSize(pc.getBatchSize());
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 ); }