public void doSecondPass(java.util.Map persistentClasses) throws MappingException { if ( value instanceof ManyToOne ) { ManyToOne manyToOne = (ManyToOne) value; PersistentClass ref = (PersistentClass) persistentClasses.get( manyToOne.getReferencedEntityName() ); if ( ref == null ) { throw new AnnotationException( "@OneToOne or @ManyToOne on " + StringHelper.qualify( entityClassName, path ) + " references an unknown entity: " + manyToOne.getReferencedEntityName() ); } BinderHelper.createSyntheticPropertyReference( columns, ref, null, manyToOne, false, mappings ); TableBinder.bindFk( ref, null, columns, manyToOne, unique, mappings ); /* * HbmMetadataSourceProcessorImpl does this only when property-ref != null, but IMO, it makes sense event if it is null */ if ( !manyToOne.isIgnoreNotFound() ) manyToOne.createPropertyRefConstraints( persistentClasses ); } else if ( value instanceof OneToOne ) { value.createForeignKey(); } else { throw new AssertionFailure( "FkSecondPass for a wrong value type: " + value.getClass().getName() ); } }
public void testProperCallbacks() { ValueVisitor vv = new ValueVisitorValidator(); new Any(new Table()).accept(vv); new Array(new RootClass()).accept(vv); new Bag(new RootClass()).accept(vv); new Component(new RootClass()).accept(vv); new DependantValue(null,null).accept(vv); new IdentifierBag(null).accept(vv); new List(null).accept(vv); new ManyToOne(null).accept(vv); new Map(null).accept(vv); new OneToMany(null).accept(vv); new OneToOne(null, new RootClass() ).accept(vv); new PrimitiveArray(null).accept(vv); new Set(null).accept(vv); new SimpleValue().accept(vv); }
protected void bindOneToOne(final org.grails.datastore.mapping.model.types.OneToOne property, OneToOne oneToOne, String path, String sessionFactoryBeanName) { PropertyConfig config = getPropertyConfig(property); final Association otherSide = property.getInverseSide(); final boolean hasOne = isHasOne(otherSide); oneToOne.setConstrained(hasOne); oneToOne.setForeignKeyType(oneToOne.isConstrained() ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT); oneToOne.setAlternateUniqueKey(true); if (config != null && config.getFetchMode() != null) { oneToOne.setFetchMode(config.getFetchMode()); } else { oneToOne.setFetchMode(FetchMode.DEFAULT); } oneToOne.setReferencedEntityName(otherSide.getOwner().getName()); oneToOne.setPropertyName(property.getName()); oneToOne.setReferenceToPrimaryKey(false); bindOneToOneInternal(property, oneToOne, path); if (hasOne) { PropertyConfig pc = getPropertyConfig(property); bindSimpleValue(property, oneToOne, path, pc, sessionFactoryBeanName); } else { oneToOne.setReferencedPropertyName(otherSide.getName()); } }
protected String getAssociationDescription(Association grailsProperty) { String assType = "unknown"; if (grailsProperty instanceof ManyToMany) { assType = "many-to-many"; } else if (grailsProperty instanceof org.grails.datastore.mapping.model.types.OneToMany) { assType = "one-to-many"; } else if (grailsProperty instanceof org.grails.datastore.mapping.model.types.OneToOne) { assType = "one-to-one"; } else if (grailsProperty instanceof org.grails.datastore.mapping.model.types.ManyToOne) { assType = "many-to-one"; } else if (grailsProperty.isEmbedded()) { assType = "embedded"; } return assType; }
public static void bindOneToOne(Element node, OneToOne oneToOne, String path, boolean isNullable, Mappings mappings) throws MappingException { bindColumns( node, oneToOne, isNullable, false, null, mappings ); Attribute constrNode = node.attribute( "constrained" ); boolean constrained = constrNode != null && constrNode.getValue().equals( "true" ); oneToOne.setConstrained( constrained ); oneToOne.setForeignKeyType( constrained ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT ); initOuterJoinFetchSetting( node, oneToOne ); initLaziness( node, oneToOne, mappings, true ); String embed = node.attributeValue( "embed-xml" ); // sometimes embed is set to the default value when not specified in the mapping, // so can't seem to determine if an attribute was explicitly set; // log a warning if embed has a value different from the default. if ( !StringHelper.isEmpty( embed ) && !"true".equals( embed ) ) { LOG.embedXmlAttributesNoLongerSupported(); } oneToOne.setEmbedded( "true".equals( embed ) ); Attribute fkNode = node.attribute( "foreign-key" ); if ( fkNode != null ) oneToOne.setForeignKeyName( fkNode.getValue() ); Attribute ukName = node.attribute( "property-ref" ); if ( ukName != null ) oneToOne.setReferencedPropertyName( ukName.getValue() ); oneToOne.setReferenceToPrimaryKey( oneToOne.getReferencedPropertyName() == null ); oneToOne.setPropertyName( node.attributeValue( "name" ) ); oneToOne.setReferencedEntityName( getEntityName( node, mappings ) ); String cascade = node.attributeValue( "cascade" ); if ( cascade != null && cascade.indexOf( "delete-orphan" ) >= 0 ) { if ( oneToOne.isConstrained() ) { throw new MappingException( "one-to-one attribute [" + path + "] does not support orphan delete as it is constrained" ); } } }
private boolean isHasOne(Association association) { return association instanceof org.grails.datastore.mapping.model.types.OneToOne && ((org.grails.datastore.mapping.model.types.OneToOne)association).isForeignKeyInChild(); }
/** * Binds a many-to-one relationship to the * */ @SuppressWarnings("unchecked") protected void bindManyToOne(Association property, ManyToOne manyToOne, String path, InFlightMetadataCollector mappings, String sessionFactoryBeanName) { NamingStrategy namingStrategy = getNamingStrategy(sessionFactoryBeanName); bindManyToOneValues(property, manyToOne); PersistentEntity refDomainClass = property instanceof ManyToMany ? property.getOwner() : property.getAssociatedEntity(); Mapping mapping = getMapping(refDomainClass); boolean isComposite = hasCompositeIdentifier(mapping); if (isComposite) { CompositeIdentity ci = (CompositeIdentity) mapping.getIdentity(); bindCompositeIdentifierToManyToOne(property, manyToOne, ci, refDomainClass, path, sessionFactoryBeanName); } else { if (property.isCircular() && (property instanceof ManyToMany)) { PropertyConfig pc = getPropertyConfig(property); if (pc.getColumns().isEmpty()) { mapping.getColumns().put(property.getName(), pc); } if (!hasJoinKeyMapping(pc) ) { JoinTable jt = new JoinTable(); final ColumnConfig columnConfig = new ColumnConfig(); columnConfig.setName(namingStrategy.propertyToColumnName(property.getName()) + UNDERSCORE + FOREIGN_KEY_SUFFIX); jt.setKey(columnConfig); pc.setJoinTable(jt); } bindSimpleValue(property, manyToOne, path, pc, sessionFactoryBeanName); } else { // bind column bindSimpleValue(property, null, manyToOne, path, mappings, sessionFactoryBeanName); } } PropertyConfig config = getPropertyConfig(property); if ((property instanceof org.grails.datastore.mapping.model.types.OneToOne) && !isComposite) { manyToOne.setAlternateUniqueKey(true); Column c = getColumnForSimpleValue(manyToOne); if (config != null && !config.isUniqueWithinGroup()) { c.setUnique(config.isUnique()); } else if (property.isBidirectional() && isHasOne(property.getInverseSide())) { c.setUnique(true); } } }
protected void bindOneToOneInternal(org.grails.datastore.mapping.model.types.OneToOne property, OneToOne oneToOne, String path) { //no-op, for subclasses to extend }
protected void setCascadeBehaviour(PersistentProperty grailsProperty, Property prop) { String cascadeStrategy = "none"; // set to cascade all for the moment PersistentEntity domainClass = grailsProperty.getOwner(); PropertyConfig config = getPropertyConfig(grailsProperty); if (config != null && config.getCascade() != null) { cascadeStrategy = config.getCascade(); } else if (grailsProperty instanceof Association) { Association association = (Association) grailsProperty; PersistentEntity referenced = association.getAssociatedEntity(); if (isHasOne(association)) { cascadeStrategy = CASCADE_ALL; } else if (association instanceof org.grails.datastore.mapping.model.types.OneToOne) { if (referenced != null && association.isOwningSide()) { cascadeStrategy = CASCADE_ALL; } else { cascadeStrategy = CASCADE_SAVE_UPDATE; } } else if (association instanceof org.grails.datastore.mapping.model.types.OneToMany) { if (referenced != null && association.isOwningSide()) { cascadeStrategy = CASCADE_ALL; } else { cascadeStrategy = CASCADE_SAVE_UPDATE; } } else if (grailsProperty instanceof ManyToMany) { if ((referenced != null && referenced.isOwningEntity(domainClass)) || association.isCircular()) { cascadeStrategy = CASCADE_SAVE_UPDATE; } } else if (grailsProperty instanceof org.grails.datastore.mapping.model.types.ManyToOne) { if (referenced != null && referenced.isOwningEntity(domainClass) && !isCircularAssociation(grailsProperty)) { cascadeStrategy = CASCADE_ALL; } else if(isCompositeIdProperty((Mapping) domainClass.getMapping().getMappedForm(), grailsProperty)) { cascadeStrategy = CASCADE_ALL; } else { cascadeStrategy = CASCADE_NONE; } } else if (grailsProperty instanceof Basic) { cascadeStrategy = CASCADE_ALL; } else if (Map.class.isAssignableFrom(grailsProperty.getType())) { referenced = association.getAssociatedEntity(); if (referenced != null && referenced.isOwningEntity(domainClass)) { cascadeStrategy = CASCADE_ALL; } else { cascadeStrategy = CASCADE_SAVE_UPDATE; } } logCascadeMapping(association, cascadeStrategy, referenced); } prop.setCascade(cascadeStrategy); }
public Object accept(OneToOne oto) { return validate(OneToOne.class, oto); }
public static void bindOneToOne(Element node, OneToOne oneToOne, String path, boolean isNullable, Mappings mappings) throws MappingException { bindColumns( node, oneToOne, isNullable, false, null, mappings ); Attribute constrNode = node.attribute( "constrained" ); boolean constrained = constrNode != null && constrNode.getValue().equals( "true" ); oneToOne.setConstrained( constrained ); oneToOne.setForeignKeyType( constrained ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT ); initOuterJoinFetchSetting( node, oneToOne ); initLaziness( node, oneToOne, mappings, true ); oneToOne.setEmbedded( "true".equals( node.attributeValue( "embed-xml" ) ) ); Attribute fkNode = node.attribute( "foreign-key" ); if ( fkNode != null ) oneToOne.setForeignKeyName( fkNode.getValue() ); Attribute ukName = node.attribute( "property-ref" ); if ( ukName != null ) oneToOne.setReferencedPropertyName( ukName.getValue() ); oneToOne.setPropertyName( node.attributeValue( "name" ) ); oneToOne.setReferencedEntityName( getEntityName( node, mappings ) ); validateCascade( node, path ); }