public void doSecondPass(Map persistentClasses) throws MappingException { org.hibernate.mapping.OneToOne value = new org.hibernate.mapping.OneToOne( buildingContext, propertyHolder.getTable(), ); final String propertyName = inferredData.getPropertyName(); value.setPropertyName( propertyName ); String referencedEntityName = ToOneBinder.getReferenceEntityName( inferredData, targetEntity, buildingContext ); value.setReferencedEntityName( referencedEntityName ); AnnotationBinder.defineFetchingStrategy( value, inferredData.getProperty() ); value.setCascadeDeleteEnabled( cascadeOnDelete ); value.setConstrained( !optional ); final ForeignKeyDirection foreignKeyDirection = !BinderHelper.isEmptyAnnotationValue( mappedBy ) ? ForeignKeyDirection.TO_PARENT : ForeignKeyDirection.FROM_PARENT; value.setForeignKeyType(foreignKeyDirection); AnnotationBinder.bindForeignKeyNameAndDefinition( value, PersistentClass otherSide = (PersistentClass) persistentClasses.get( value.getReferencedEntityName() ); Property otherSideProperty; try { if ( otherSide == null ) { throw new MappingException( "Unable to find entity: " + value.getReferencedEntityName() ); "Unknown mappedBy in: " + StringHelper.qualify( ownerEntity, ownerProperty ) + ", referenced property unknown: "
public Type getType() throws MappingException { if ( getColumnIterator().hasNext() ) { return getMetadata().getTypeResolver().getTypeFactory().specialOneToOne( getReferencedEntityName(), foreignKeyType, referenceToPrimaryKey, referencedPropertyName, isLazy(), isUnwrapProxy(), entityName, propertyName, constrained ); } else { return getMetadata().getTypeResolver().getTypeFactory().oneToOne( getReferencedEntityName(), foreignKeyType, referenceToPrimaryKey, referencedPropertyName, isLazy(), isUnwrapProxy(), entityName, propertyName, constrained ); } }
final SingularAttributeSourceOneToOne oneToOneSource, final OneToOne oneToOneBinding) { oneToOneBinding.setPropertyName( oneToOneSource.getName() ); oneToOneBinding.setConstrained( true ); oneToOneBinding.setForeignKeyType( ForeignKeyDirection.FROM_PARENT ); 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() ); oneToOneBinding.setReferencedPropertyName( oneToOneSource.getReferencedEntityAttributeName() ); oneToOneBinding.setReferenceToPrimaryKey( false ); oneToOneBinding.setReferenceToPrimaryKey( true ); oneToOneBinding.setReferencedEntityName( oneToOneSource.getReferencedEntityName() ); oneToOneBinding.setForeignKeyName( oneToOneSource.getExplicitForeignKeyName() ); oneToOneBinding.setCascadeDeleteEnabled( oneToOneSource.isCascadeDeleteEnabled() );
); final String propertyRef = oneToOneBinding.getReferencedPropertyName(); if ( propertyRef != null ) { handlePropertyReference( sourceDocument, oneToOneBinding.getReferencedEntityName(), propertyRef, true, oneToOneBinding.createForeignKey();
public static void bindOneToOne(Element node, OneToOne oneToOne, 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, "proxy", 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 ) ); }
private static void bindOneToOne(final GrailsDomainClassProperty property, OneToOne oneToOne, String path, String sessionFactoryBeanName) { PropertyConfig config = getPropertyConfig(property); final GrailsDomainClassProperty otherSide = property.getOtherSide(); oneToOne.setConstrained(otherSide.isHasOne()); oneToOne.setForeignKeyType(oneToOne.isConstrained() ? ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT : ForeignKeyDirection.FOREIGN_KEY_TO_PARENT); oneToOne.setAlternateUniqueKey(true); if (config != null && config.getFetch() != null) { oneToOne.setFetchMode(config.getFetch()); } else { oneToOne.setFetchMode(FetchMode.DEFAULT); } oneToOne.setReferencedEntityName(otherSide.getDomainClass().getFullName()); oneToOne.setPropertyName(property.getName()); if (otherSide.isHasOne()) { PropertyConfig pc = getPropertyConfig(property); bindSimpleValue(property, oneToOne, path, pc, sessionFactoryBeanName); } else { oneToOne.setReferencedPropertyName(otherSide.getName()); } }
public Type getType() throws MappingException { if ( getColumnIterator().hasNext() ) { return new SpecialOneToOneType( getReferencedEntityName(), foreignKeyType, referencedPropertyName, isLazy(), isUnwrapProxy(), entityName, propertyName ); } else { return TypeFactory.oneToOne( getReferencedEntityName(), foreignKeyType, referencedPropertyName, isLazy(), isUnwrapProxy(), isEmbedded(), entityName, propertyName ); } }
private Property bindOneToOne(PersistentClass rc, Table targetTable, ForeignKey fk, Set<Column> processedColumns, boolean constrained, boolean inverseProperty) { OneToOne value = new OneToOne((MetadataImplementor)metadata, targetTable, rc); value.setReferencedEntityName(revengStrategy .tableToClassName(TableIdentifier.create(targetTable))); Column fkcolumn = (Column) columns.next(); checkColumn(fkcolumn); value.addColumn(fkcolumn); processedColumns.add(fkcolumn); value.setFetchMode(FetchMode.SELECT); value.setConstrained(constrained); value.setForeignKeyType( constrained ? ForeignKeyDirection.FROM_PARENT : ForeignKeyDirection.TO_PARENT );
sourceDocument, (SingularAttributeSourceOneToOne) attributeSource, new OneToOne( sourceDocument, component.getTable(), component.getOwner() ), component.getComponentClassName() );
private String getOneToOneMappedBy(Metadata md, OneToOne oneToOne) { String mappedBy; Iterator<Selectable> joinColumnsIt = oneToOne.getColumnIterator(); Set<Selectable> joinColumns = new HashSet<Selectable>(); while ( joinColumnsIt.hasNext() ) { joinColumns.add( joinColumnsIt.next() ); PersistentClass pc = md.getEntityBinding(oneToOne.getReferencedEntityName()); String referencedPropertyName = oneToOne.getReferencedPropertyName(); if ( referencedPropertyName != null ) return referencedPropertyName;
@Test public void testOneToOneSingleColumnBiDirectional() { PersistentClass person = metadata.getEntityBinding("Person"); Property addressProperty = person.getProperty("addressPerson"); Assert.assertNotNull(addressProperty); Assert.assertTrue(addressProperty.getValue() instanceof OneToOne); OneToOne oto = (OneToOne) addressProperty.getValue(); Assert.assertEquals(oto.getColumnSpan(),1); Assert.assertEquals("Person", oto.getEntityName()); Assert.assertEquals("AddressPerson", oto.getReferencedEntityName()); Assert.assertEquals(2, person.getPropertyClosureSpan()); Assert.assertEquals("personId", person.getIdentifierProperty().getName()); Assert.assertFalse(oto.isConstrained()); PersistentClass addressPerson = metadata.getEntityBinding("AddressPerson"); Property personProperty = addressPerson.getProperty("person"); Assert.assertNotNull(personProperty); Assert.assertTrue(personProperty.getValue() instanceof OneToOne); oto = (OneToOne) personProperty.getValue(); Assert.assertTrue(oto.isConstrained()); Assert.assertEquals(oto.getColumnSpan(),1); Assert.assertEquals("AddressPerson", oto.getEntityName()); Assert.assertEquals("Person", oto.getReferencedEntityName()); Assert.assertEquals(2, addressPerson.getPropertyClosureSpan()); Assert.assertEquals("addressId", addressPerson.getIdentifierProperty().getName()); }
String entityName) { final OneToOne propertyValue = (OneToOne) value; final String owningReferencePropertyName = propertyValue.getReferencedPropertyName(); final String referencedEntityName = propertyValue.getReferencedEntityName();
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 ); }
private Property getConstrainedOneToOne(RootClass rc) { Iterator<?> propertyClosureIterator = rc.getPropertyClosureIterator(); while (propertyClosureIterator.hasNext()) { Property property = (Property) propertyClosureIterator.next(); if(property.getValue() instanceof OneToOne) { OneToOne oto = (OneToOne) property.getValue(); if(oto.isConstrained()) { return property; } } } return null; }
@Override public JavaTypeMapping getJavaTypeMapping() { final PersistentClass referencedPersistentClass = getMetadataBuildingContext() .getMetadataCollector() .getEntityBinding( getReferencedEntityName() ); if ( referenceToPrimaryKey || referencedPropertyName == null ) { return referencedPersistentClass.getIdentifier().getJavaTypeMapping(); } else { return referencedPersistentClass.getReferencedProperty( getReferencedPropertyName() ) .getValue() .getJavaTypeMapping(); } } }
public ForeignKey createForeignKey() throws MappingException { if ( constrained ) { this.foreignKey = createForeignKeyOfEntity( getReferencedEntityName() ); if ( this.referencedPropertyName == null ) { foreignKey.disableCreation(); } } return foreignKey; }
if ( oneToOne.getReferencedPropertyName() != null ) { toOneRelationMetadataGenerator.addOneToOneNotOwning( propertyAuditingData,
@Test public void testProperCallbacks() { final MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( serviceRegistry ) .buildMetadata(); final Table tbl = new Table(); final RootClass rootClass = new RootClass( metadataBuildingContext ); ValueVisitor vv = new ValueVisitorValidator(); MetadataBuildingContextTestingImpl metadataBuildingContext = new MetadataBuildingContextTestingImpl(); new Any( metadataBuildingContext, tbl ).accept( vv ); new Array( metadataBuildingContext, rootClass ).accept( vv ); new Bag( metadataBuildingContext, rootClass ).accept( vv ); new Component( metadataBuildingContext, rootClass ).accept( vv ); new DependantValue( metadataBuildingContext, tbl, null ).accept( vv ); new IdentifierBag( metadataBuildingContext, rootClass ).accept( vv ); new List( metadataBuildingContext, rootClass ).accept( vv ); new ManyToOne( metadataBuildingContext, tbl ).accept( vv ); new Map( metadataBuildingContext, rootClass ).accept( vv ); new OneToMany( metadataBuildingContext, rootClass ).accept( vv ); new OneToOne( metadataBuildingContext, tbl, rootClass ).accept( vv ); new PrimitiveArray( metadataBuildingContext, rootClass ).accept( vv ); new Set( metadataBuildingContext, rootClass ).accept( vv ); new SimpleValue( metadataBuildingContext ).accept( vv ); }
public boolean isSharedPkBasedOneToOne(OneToOne oneToOne){ Iterator<Selectable> joinColumnsIt = oneToOne.getColumnIterator(); Set<Selectable> joinColumns = new HashSet<Selectable>(); while ( joinColumnsIt.hasNext() ) { joinColumns.add( joinColumnsIt.next() ); } if ( joinColumns.size() == 0 ) return false; Iterator<?> idColumnsIt = getIdentifierProperty().getColumnIterator(); while ( idColumnsIt.hasNext() ) { if (!joinColumns.contains(idColumnsIt.next()) ) return false; } return true; }
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 ) ); 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" ); } } }