package org.hibernate.mapping;
import java.util.ArrayList;
import java.util.Iterator;
import org.hibernate.MappingException;
import org.hibernate.cfg.Mappings;
import org.hibernate.type.EntityType;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;
public class OneToOne extends ToOne {
private boolean constrained;
private ForeignKeyDirection foreignKeyType;
private KeyValue identifier;
private String propertyName;
private String entityName;
public OneToOne(Mappings mappings, Table table, PersistentClass owner) throws MappingException {
super( mappings, 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 getMappings().getTypeResolver().getTypeFactory().specialOneToOne(
getReferencedEntityName(),
foreignKeyType,
referenceToPrimaryKey,
referencedPropertyName,
isLazy(),
isUnwrapProxy(),
entityName,
propertyName
);
}
else {
return getMappings().getTypeResolver().getTypeFactory().oneToOne(
getReferencedEntityName(),
foreignKeyType,
referenceToPrimaryKey,
referencedPropertyName,
isLazy(),
isUnwrapProxy(),
entityName,
propertyName
);
}
}
public void createForeignKey() throws MappingException {
if ( constrained && referencedPropertyName==null) {
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;
}
public boolean isConstrained() {
return constrained;
}
public ForeignKeyDirection getForeignKeyType() {
return foreignKeyType;
}
public KeyValue getIdentifier() {
return identifier;
}
public void setConstrained(boolean constrained) {
this.constrained = constrained;
}
public void setForeignKeyType(ForeignKeyDirection foreignKeyType) {
this.foreignKeyType = foreignKeyType;
}
public void setIdentifier(KeyValue identifier) {
this.identifier = identifier;
}
public boolean isNullable() {
return !constrained;
}
public Object accept(ValueVisitor visitor) {
return visitor.accept(this);
}
}