/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.mapping;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

import org.hibernate.MappingException;
import org.hibernate.boot.spi.MetadataBuildingContext;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.type.EntityType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;

A one-to-one association mapping
Author:Gavin King
/** * A one-to-one association mapping * @author Gavin King */
public class OneToOne extends ToOne { private boolean constrained; private ForeignKeyDirection foreignKeyType; private KeyValue identifier; private String propertyName; private String entityName;
Deprecated:Use OneToOne(MetadataBuildingContext, Table, PersistentClass) instead.
/** * @deprecated Use {@link OneToOne#OneToOne(MetadataBuildingContext, Table, PersistentClass)} instead. */
@Deprecated public OneToOne(MetadataImplementor metadata, Table table, PersistentClass owner) throws MappingException { super( metadata, table ); this.identifier = owner.getKey(); this.entityName = owner.getEntityName(); } public OneToOne(MetadataBuildingContext buildingContext, Table table, PersistentClass owner) throws MappingException { super( buildingContext, table ); this.identifier = owner.getKey(); this.entityName = owner.getEntityName(); } public String getPropertyName() { return propertyName; } public void setPropertyName(String propertyName) { this.propertyName = propertyName==null ? null : propertyName.intern(); } public String getEntityName() { return entityName; } public void setEntityName(String propertyName) { this.entityName = entityName==null ? null : entityName.intern(); } public Type getType() throws MappingException { if ( getColumnIterator().hasNext() ) { return getMetadata().getTypeResolver().getTypeFactory().specialOneToOne( getReferencedEntityName(), foreignKeyType, referenceToPrimaryKey, referencedPropertyName, isLazy(), isUnwrapProxy(), entityName, propertyName ); } else { return getMetadata().getTypeResolver().getTypeFactory().oneToOne( getReferencedEntityName(), foreignKeyType, referenceToPrimaryKey, referencedPropertyName, isLazy(), isUnwrapProxy(), entityName, propertyName ); } } public void createForeignKey() throws MappingException { if ( constrained && referencedPropertyName==null) { //TODO: handle the case of a foreign key to something other than the pk createForeignKeyOfEntity( ( (EntityType) getType() ).getAssociatedEntityName() ); } } public java.util.List getConstraintColumns() { ArrayList list = new ArrayList(); Iterator iter = identifier.getColumnIterator(); while ( iter.hasNext() ) { list.add( iter.next() ); } return list; }
Returns the constrained.
Returns:boolean
/** * Returns the constrained. * @return boolean */
public boolean isConstrained() { return constrained; }
Returns the foreignKeyType.
Returns:AssociationType.ForeignKeyType
/** * Returns the foreignKeyType. * @return AssociationType.ForeignKeyType */
public ForeignKeyDirection getForeignKeyType() { return foreignKeyType; }
Returns the identifier.
Returns:Value
/** * Returns the identifier. * @return Value */
public KeyValue getIdentifier() { return identifier; }
Sets the constrained.
Params:
  • constrained – The constrained to set
/** * Sets the constrained. * @param constrained The constrained to set */
public void setConstrained(boolean constrained) { this.constrained = constrained; }
Sets the foreignKeyType.
Params:
  • foreignKeyType – The foreignKeyType to set
/** * Sets the foreignKeyType. * @param foreignKeyType The foreignKeyType to set */
public void setForeignKeyType(ForeignKeyDirection foreignKeyType) { this.foreignKeyType = foreignKeyType; }
Sets the identifier.
Params:
  • identifier – The identifier to set
/** * Sets the identifier. * @param identifier The identifier to set */
public void setIdentifier(KeyValue identifier) { this.identifier = identifier; } public boolean isNullable() { return !constrained; } public Object accept(ValueVisitor visitor) { return visitor.accept(this); } @Override public boolean isSame(ToOne other) { return other instanceof OneToOne && isSame( (OneToOne) other ); } public boolean isSame(OneToOne other) { return super.isSame( other ) && Objects.equals( foreignKeyType, other.foreignKeyType ) && isSame( identifier, other.identifier ) && Objects.equals( propertyName, other.propertyName ) && Objects.equals( entityName, other.entityName ) && constrained == other.constrained; } }