1 package org.hibernate.mapping; 3 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 7 import org.hibernate.MappingException; 8 import org.hibernate.type.EntityType; 9 import org.hibernate.type.ForeignKeyDirection; 10 import org.hibernate.type.SpecialOneToOneType; 11 import org.hibernate.type.Type; 12 import org.hibernate.type.TypeFactory; 13 14 18 public class OneToOne extends ToOne { 19 20 private boolean constrained; 21 private ForeignKeyDirection foreignKeyType; 22 private KeyValue identifier; 23 private String propertyName; 24 private String entityName; 25 26 public OneToOne(Table table, PersistentClass owner) throws MappingException { 27 super(table); 28 this.identifier = owner.getKey(); 29 this.entityName = owner.getEntityName(); 30 } 31 32 public String getPropertyName() { 33 return propertyName; 34 } 35 36 public void setPropertyName(String propertyName) { 37 this.propertyName = propertyName==null ? null : propertyName.intern(); 38 } 39 40 public String getEntityName() { 41 return entityName; 42 } 43 44 public void setEntityName(String propertyName) { 45 this.entityName = entityName==null ? null : entityName.intern(); 46 } 47 48 public Type getType() throws MappingException { 49 if ( getColumnIterator().hasNext() ) { 50 return new SpecialOneToOneType( 51 getReferencedEntityName(), 52 foreignKeyType, 53 referencedPropertyName, 54 isLazy(), 55 isUnwrapProxy(), 56 entityName, 57 propertyName 58 ); 59 } 60 else { 61 return TypeFactory.oneToOne( 62 getReferencedEntityName(), 63 foreignKeyType, 64 referencedPropertyName, 65 isLazy(), 66 isUnwrapProxy(), 67 isEmbedded(), 68 entityName, 69 propertyName 70 ); 71 } 72 } 73 74 public void createForeignKey() throws MappingException { 75 if ( constrained && referencedPropertyName==null) { 76 createForeignKeyOfEntity( ( (EntityType) getType() ).getAssociatedEntityName() ); 78 } 79 } 80 81 public java.util.List getConstraintColumns() { 82 ArrayList list = new ArrayList (); 83 Iterator iter = identifier.getColumnIterator(); 84 while ( iter.hasNext() ) list.add( iter.next() ); 85 return list; 86 } 87 91 public boolean isConstrained() { 92 return constrained; 93 } 94 95 99 public ForeignKeyDirection getForeignKeyType() { 100 return foreignKeyType; 101 } 102 103 107 public KeyValue getIdentifier() { 108 return identifier; 109 } 110 111 115 public void setConstrained(boolean constrained) { 116 this.constrained = constrained; 117 } 118 119 123 public void setForeignKeyType(ForeignKeyDirection foreignKeyType) { 124 this.foreignKeyType = foreignKeyType; 125 } 126 127 131 public void setIdentifier(KeyValue identifier) { 132 this.identifier = identifier; 133 } 134 135 public boolean isNullable() { 136 return !constrained; 137 } 138 139 public Object accept(ValueVisitor visitor) { 140 return visitor.accept(this); 141 } 142 143 } 144 | Popular Tags |