KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > mapping > OneToOne


1 //$Id: OneToOne.java,v 1.14 2005/06/20 20:32:35 oneovthafew Exp $
2
package org.hibernate.mapping;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.Iterator JavaDoc;
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 /**
15  * A one-to-one association mapping
16  * @author Gavin King
17  */

18 public class OneToOne extends ToOne {
19
20     private boolean constrained;
21     private ForeignKeyDirection foreignKeyType;
22     private KeyValue identifier;
23     private String JavaDoc propertyName;
24     private String JavaDoc 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 JavaDoc getPropertyName() {
33         return propertyName;
34     }
35
36     public void setPropertyName(String JavaDoc propertyName) {
37         this.propertyName = propertyName==null ? null : propertyName.intern();
38     }
39     
40     public String JavaDoc getEntityName() {
41         return entityName;
42     }
43
44     public void setEntityName(String JavaDoc 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             //TODO: handle the case of a foreign key to something other than the pk
77
createForeignKeyOfEntity( ( (EntityType) getType() ).getAssociatedEntityName() );
78         }
79     }
80
81     public java.util.List JavaDoc getConstraintColumns() {
82         ArrayList JavaDoc list = new ArrayList JavaDoc();
83         Iterator JavaDoc iter = identifier.getColumnIterator();
84         while ( iter.hasNext() ) list.add( iter.next() );
85         return list;
86     }
87     /**
88      * Returns the constrained.
89      * @return boolean
90      */

91     public boolean isConstrained() {
92         return constrained;
93     }
94
95     /**
96      * Returns the foreignKeyType.
97      * @return AssociationType.ForeignKeyType
98      */

99     public ForeignKeyDirection getForeignKeyType() {
100         return foreignKeyType;
101     }
102
103     /**
104      * Returns the identifier.
105      * @return Value
106      */

107     public KeyValue getIdentifier() {
108         return identifier;
109     }
110
111     /**
112      * Sets the constrained.
113      * @param constrained The constrained to set
114      */

115     public void setConstrained(boolean constrained) {
116         this.constrained = constrained;
117     }
118
119     /**
120      * Sets the foreignKeyType.
121      * @param foreignKeyType The foreignKeyType to set
122      */

123     public void setForeignKeyType(ForeignKeyDirection foreignKeyType) {
124         this.foreignKeyType = foreignKeyType;
125     }
126
127     /**
128      * Sets the identifier.
129      * @param identifier The identifier to set
130      */

131     public void setIdentifier(KeyValue identifier) {
132         this.identifier = identifier;
133     }
134
135     public boolean isNullable() {
136         return !constrained;
137     }
138
139     public Object JavaDoc accept(ValueVisitor visitor) {
140         return visitor.accept(this);
141     }
142     
143 }
144
Popular Tags