/** * Checks if the collection element is of {@link ComponentType} type. */ private boolean isEmbeddableElementType() { return propertyValue.getElement().getType() instanceof ComponentType; }
private PersistentClass getReferenceCollectionClass(Collection collectionValue) { PersistentClass referencedClass = null; if ( collectionValue.getElement() instanceof OneToMany ) { final OneToMany oneToManyValue = (OneToMany) collectionValue.getElement(); referencedClass = oneToManyValue.getAssociatedClass(); } else if ( collectionValue.getElement() instanceof ManyToOne ) { // Case for bi-directional relation with @JoinTable on the owning @ManyToOne side. final ManyToOne manyToOneValue = (ManyToOne) collectionValue.getElement(); referencedClass = manyToOneValue.getMetadata().getEntityBinding( manyToOneValue.getReferencedEntityName() ); } return referencedClass; }
public Value getHibernateValue() { return ( (Collection) getPropertyMapping().getValue() ).getElement(); }
public static String getReferencedEntityName(Value value) { if ( value instanceof ToOne ) { return ( (ToOne) value ).getReferencedEntityName(); } else if ( value instanceof OneToMany ) { return ( (OneToMany) value ).getReferencedEntityName(); } else if ( value instanceof Collection ) { return getReferencedEntityName( ( (Collection) value ).getElement() ); } return null; }
private static void checkFilterConditions(Collection collValue) { //for now it can't happen, but sometime soon... if ( ( collValue.getFilters().size() != 0 || StringHelper.isNotEmpty( collValue.getWhere() ) ) && collValue.getFetchMode() == FetchMode.JOIN && !( collValue.getElement() instanceof SimpleValue ) && //SimpleValue (CollectionOfElements) are always SELECT but it does not matter collValue.getElement().getFetchMode() != FetchMode.JOIN ) { throw new MappingException( "@ManyToMany or @CollectionOfElements defining filter or where without join fetching " + "not valid within collection using join fetching[" + collValue.getRole() + "]" ); } }
private String getMiddleTableName(Collection value, String entityName) { // We check how Hibernate maps the collection. if ( value.getElement() instanceof OneToMany && !value.isInverse() ) { // This must be a @JoinColumn+@OneToMany mapping. Generating the table name, as Hibernate doesn't use a // middle table for mapping this relation. return StringTools.getLastComponent( entityName ) + "_" + StringTools.getLastComponent( MappingTools.getReferencedEntityName( value.getElement() ) ); } // Hibernate uses a middle table for mapping this relation, so we get it's name directly. return value.getCollectionTable().getName(); }
/** * Returns whether the collection is a map-type and that the map element is defined as a Clob/NClob type. * * @return {@code true} if the element is a Clob/NClob type, otherwise {@code false}. */ private boolean isLobMapElementType() { if ( propertyValue instanceof org.hibernate.mapping.Map ) { final Type type = propertyValue.getElement().getType(); // we're only interested in basic types if ( !type.isComponentType() && !type.isAssociationType() ) { return ( type instanceof MaterializedClobType ) || ( type instanceof MaterializedNClobType ); } } return false; }
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() ); } // } }
public CascadeStyle getCascadeStyle() throws MappingException { Type type = value.getType(); if ( type.isComponentType() ) { return getCompositeCascadeStyle( (CompositeType) type, cascade ); } else if ( type.isCollectionType() ) { return getCollectionCascadeStyle( ( (Collection) value ).getElement().getType(), cascade ); } else { return getCascadeStyle( cascade ); } }
void addCollection() { final Type type = propertyValue.getType(); final Value value = propertyValue.getElement(); final boolean oneToManyAttachedType = type instanceof BagType || type instanceof SetType || type instanceof MapType || type instanceof ListType; final boolean inverseOneToMany = (value instanceof OneToMany) && (propertyValue.isInverse()); final boolean owningManyToOneWithJoinTableBidirectional = (value instanceof ManyToOne) && (propertyAuditingData.getRelationMappedBy() != null); final boolean fakeOneToManyBidirectional = (value instanceof OneToMany) && (propertyAuditingData.getAuditMappedBy() != null); if ( oneToManyAttachedType && (inverseOneToMany || fakeOneToManyBidirectional || owningManyToOneWithJoinTableBidirectional) ) { // A one-to-many relation mapped using @ManyToOne and @OneToMany(mappedBy="...") addOneToManyAttached( fakeOneToManyBidirectional ); } else { // All other kinds of relations require a middle (join) table. addWithMiddleTable(); } }
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(); }
@Override protected void afterMetadataBuilt(Metadata metadata) { Collection children = metadata.getCollectionBinding( Parent.class.getName() + ".children" ); Component childComponents = ( Component ) children.getElement(); Formula f = ( Formula ) childComponents.getProperty( "bioLength" ).getValue().getColumnIterator().next(); SQLFunction lengthFunction = metadata.getDatabase().getJdbcEnvironment().getDialect().getFunctions().get( "length" ); if ( lengthFunction != null ) { ArrayList args = new ArrayList(); args.add( "bio" ); f.setFormula( lengthFunction.render( StandardBasicTypes.INTEGER, args, null ) ); } }
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() ); } }
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 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() ) ); }
@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() ) ); } } }
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() ); } }