org.hibernate.mapping.OneToOne Java Examples

The following examples show how to use org.hibernate.mapping.OneToOne. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ValueVisitorTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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);
	
		
	}
 
Example #2
Source File: ToOneFkSecondPass.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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()
			);
		}
		manyToOne.setPropertyName( path );
		BinderHelper.createSyntheticPropertyReference( columns, ref, null, manyToOne, false, buildingContext );
		TableBinder.bindFk( ref, null, columns, manyToOne, unique, buildingContext );
		/*
		 * 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() );
	}
}
 
Example #3
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Property createOneToOneAttribute(
		MappingDocument sourceDocument,
		SingularAttributeSourceOneToOne oneToOneSource,
		OneToOne oneToOneBinding,
		String containingClassName) {
	bindOneToOne( sourceDocument, oneToOneSource, oneToOneBinding );

	prepareValueTypeViaReflection(
			sourceDocument,
			oneToOneBinding,
			containingClassName,
			oneToOneSource.getName(),
			oneToOneSource.getAttributeRole()
	);

	final String propertyRef = oneToOneBinding.getReferencedPropertyName();
	if ( propertyRef != null ) {
		handlePropertyReference(
				sourceDocument,
				oneToOneBinding.getReferencedEntityName(),
				propertyRef,
				true,
				"<one-to-one name=\"" + oneToOneSource.getName() + "\"/>"
		);
	}

	oneToOneBinding.createForeignKey();

	Property prop = new Property();
	prop.setValue( oneToOneBinding );
	bindProperty(
			sourceDocument,
			oneToOneSource,
			prop
	);

	return prop;
}
 
Example #4
Source File: RelationalObjectBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void bindFormulas(
		MappingDocument sourceDocument,
		List<DerivedValueSource> formulaSources,
		OneToOne oneToOneBinding) {
	for ( DerivedValueSource formulaSource : formulaSources ) {
		oneToOneBinding.addFormula( new Formula( formulaSource.getExpression() ) );
	}
}
 
Example #5
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
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());
    }
}
 
Example #6
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public void bindOneToOne(
		final MappingDocument sourceDocument,
		final SingularAttributeSourceOneToOne oneToOneSource,
		final OneToOne oneToOneBinding) {
	oneToOneBinding.setPropertyName( oneToOneSource.getName() );

	relationalObjectBinder.bindFormulas(
			sourceDocument,
			oneToOneSource.getFormulaSources(),
			oneToOneBinding
	);


	if ( oneToOneSource.isConstrained() ) {
		if ( oneToOneSource.getCascadeStyleName() != null
				&& oneToOneSource.getCascadeStyleName().contains( "delete-orphan" ) ) {
			throw new MappingException(
					String.format(
							Locale.ENGLISH,
							"one-to-one attribute [%s] cannot specify orphan delete cascading as it is constrained",
							oneToOneSource.getAttributeRole().getFullPath()
					),
					sourceDocument.getOrigin()
			);
		}
		oneToOneBinding.setConstrained( true );
		oneToOneBinding.setForeignKeyType( ForeignKeyDirection.FROM_PARENT );
	}
	else {
		oneToOneBinding.setForeignKeyType( ForeignKeyDirection.TO_PARENT );
	}

	oneToOneBinding.setLazy( oneToOneSource.getFetchCharacteristics().getFetchTiming() == FetchTiming.DELAYED );
	oneToOneBinding.setFetchMode(
			oneToOneSource.getFetchCharacteristics().getFetchStyle() == FetchStyle.SELECT
					? FetchMode.SELECT
					: FetchMode.JOIN
	);
	oneToOneBinding.setUnwrapProxy( oneToOneSource.getFetchCharacteristics().isUnwrapProxies() );


	if ( StringHelper.isNotEmpty( oneToOneSource.getReferencedEntityAttributeName() ) ) {
		oneToOneBinding.setReferencedPropertyName( oneToOneSource.getReferencedEntityAttributeName() );
		oneToOneBinding.setReferenceToPrimaryKey( false );
	}
	else {
		oneToOneBinding.setReferenceToPrimaryKey( true );
	}

	// todo : probably need some reflection here if null
	oneToOneBinding.setReferencedEntityName( oneToOneSource.getReferencedEntityName() );

	if ( oneToOneSource.isEmbedXml() == Boolean.TRUE ) {
		DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfEmbedXmlSupport();
	}

	if ( StringHelper.isNotEmpty( oneToOneSource.getExplicitForeignKeyName() ) ) {
		oneToOneBinding.setForeignKeyName( oneToOneSource.getExplicitForeignKeyName() );
	}

	oneToOneBinding.setCascadeDeleteEnabled( oneToOneSource.isCascadeDeleteEnabled() );
}
 
Example #8
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void bindAllCompositeAttributes(
		MappingDocument sourceDocument,
		EmbeddableSource embeddableSource,
		Component component) {

	for ( AttributeSource attributeSource : embeddableSource.attributeSources() ) {
		Property attribute = null;

		if ( SingularAttributeSourceBasic.class.isInstance( attributeSource ) ) {
			attribute = createBasicAttribute(
					sourceDocument,
					(SingularAttributeSourceBasic) attributeSource,
					new SimpleValue( sourceDocument, component.getTable() ),
					component.getComponentClassName()
			);
		}
		else if ( SingularAttributeSourceEmbedded.class.isInstance( attributeSource ) ) {
			attribute = createEmbeddedAttribute(
					sourceDocument,
					(SingularAttributeSourceEmbedded) attributeSource,
					new Component( sourceDocument, component ),
					component.getComponentClassName()
			);
		}
		else if ( SingularAttributeSourceManyToOne.class.isInstance( attributeSource ) ) {
			attribute = createManyToOneAttribute(
					sourceDocument,
					(SingularAttributeSourceManyToOne) attributeSource,
					new ManyToOne( sourceDocument, component.getTable() ),
					component.getComponentClassName()
			);
		}
		else if ( SingularAttributeSourceOneToOne.class.isInstance( attributeSource ) ) {
			attribute = createOneToOneAttribute(
					sourceDocument,
					(SingularAttributeSourceOneToOne) attributeSource,
					new OneToOne( sourceDocument, component.getTable(), component.getOwner() ),
					component.getComponentClassName()
			);
		}
		else if ( SingularAttributeSourceAny.class.isInstance( attributeSource ) ) {
			attribute = createAnyAssociationAttribute(
					sourceDocument,
					(SingularAttributeSourceAny) attributeSource,
					new Any( sourceDocument, component.getTable() ),
					component.getComponentClassName()
			);
		}
		else if ( PluralAttributeSource.class.isInstance( attributeSource ) ) {
			attribute = createPluralAttribute(
					sourceDocument,
					(PluralAttributeSource) attributeSource,
					component.getOwner()
			);
		}
		else {
			throw new AssertionFailure(
					String.format(
							Locale.ENGLISH,
							"Unexpected AttributeSource sub-type [%s] as part of composite [%s]",
							attributeSource.getClass().getName(),
							attributeSource.getAttributeRole().getFullPath()
					)

			);
		}

		component.addProperty( attribute );
	}
}
 
Example #9
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
private boolean isHasOne(Association association) {
    return association instanceof org.grails.datastore.mapping.model.types.OneToOne && ((org.grails.datastore.mapping.model.types.OneToOne)association).isForeignKeyInChild();
}
 
Example #10
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example #11
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
protected void bindOneToOneInternal(org.grails.datastore.mapping.model.types.OneToOne property, OneToOne oneToOne, String path) {
    //no-op, for subclasses to extend
}
 
Example #12
Source File: GrailsDomainBinder.java    From gorm-hibernate5 with Apache License 2.0 4 votes vote down vote up
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);
}
 
Example #13
Source File: ValueVisitorTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Object accept(OneToOne oto) {
	return validate(OneToOne.class, oto);
}
 
Example #14
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 3 votes vote down vote up
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 );
}