KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > OneToOneType


1 //$Id: OneToOneType.java,v 1.22 2005/07/19 18:17:14 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.hibernate.HibernateException;
10 import org.hibernate.MappingException;
11 import org.hibernate.engine.EntityKey;
12 import org.hibernate.engine.Mapping;
13 import org.hibernate.engine.SessionImplementor;
14 import org.hibernate.persister.entity.EntityPersister;
15 import org.hibernate.util.ArrayHelper;
16
17 /**
18  * A one-to-one association to an entity
19  * @author Gavin King
20  */

21 public class OneToOneType extends EntityType {
22
23     private final ForeignKeyDirection foreignKeyType;
24     private final String JavaDoc propertyName;
25     private final String JavaDoc entityName;
26     
27     public String JavaDoc getPropertyName() {
28         return propertyName;
29     }
30     
31     public boolean isNull(Object JavaDoc owner, SessionImplementor session) {
32         
33         if ( propertyName != null ) {
34             
35             EntityPersister ownerPersister = session.getFactory()
36                     .getEntityPersister(entityName);
37             Serializable JavaDoc id = session.getContextEntityIdentifier(owner);
38
39             EntityKey entityKey = new EntityKey( id, ownerPersister, session.getEntityMode() );
40             
41             return session.getPersistenceContext()
42                     .isPropertyNull( entityKey, getPropertyName() );
43             
44         }
45         else {
46             return false;
47         }
48
49     }
50
51     public int getColumnSpan(Mapping session) throws MappingException {
52         return 0;
53     }
54
55     public int[] sqlTypes(Mapping session) throws MappingException {
56         return ArrayHelper.EMPTY_INT_ARRAY;
57     }
58
59     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
60         return ArrayHelper.EMPTY_BOOLEAN_ARRAY;
61     }
62
63     public OneToOneType(
64             String JavaDoc referencedEntityName,
65             ForeignKeyDirection foreignKeyType,
66             String JavaDoc uniqueKeyPropertyName,
67             boolean lazy,
68             boolean unwrapProxy,
69             boolean isEmbeddedInXML,
70             String JavaDoc entityName,
71             String JavaDoc propertyName
72     ) {
73         super(
74                 referencedEntityName,
75                 uniqueKeyPropertyName,
76                 !lazy,
77                 isEmbeddedInXML,
78                 unwrapProxy
79             );
80         this.foreignKeyType = foreignKeyType;
81         this.propertyName = propertyName;
82         this.entityName = entityName;
83     }
84
85     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, boolean[] settable, SessionImplementor session) {
86         //nothing to do
87
}
88
89     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session) {
90         //nothing to do
91
}
92
93     public boolean isOneToOne() {
94         return true;
95     }
96
97     public boolean isDirty(Object JavaDoc old, Object JavaDoc current, SessionImplementor session) {
98         return false;
99     }
100
101     public boolean isModified(Object JavaDoc old, Object JavaDoc current, SessionImplementor session) {
102         return false;
103     }
104
105     public ForeignKeyDirection getForeignKeyDirection() {
106         return foreignKeyType;
107     }
108
109     public Object JavaDoc hydrate(
110         ResultSet JavaDoc rs,
111         String JavaDoc[] names,
112         SessionImplementor session,
113         Object JavaDoc owner)
114     throws HibernateException, SQLException JavaDoc {
115
116         return session.getContextEntityIdentifier(owner);
117     }
118
119     protected boolean isNullable() {
120         return foreignKeyType==ForeignKeyDirection.FOREIGN_KEY_TO_PARENT;
121     }
122
123     public boolean useLHSPrimaryKey() {
124         return true;
125     }
126
127     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
128     throws HibernateException {
129         return null;
130     }
131
132     public Object JavaDoc assemble(Serializable JavaDoc oid, SessionImplementor session, Object JavaDoc owner)
133     throws HibernateException {
134         //this should be a call to resolve(), not resolveIdentifier(),
135
//'cos it might be a property-ref, and we did not cache the
136
//referenced value
137
return resolve( session.getContextEntityIdentifier(owner), session, owner );
138     }
139     
140     public boolean isAlwaysDirtyChecked() {
141         return false;
142         // because of how assemble/disassemble is implemented
143
// and because one-to-one is never dirty
144
//TODO: this is kinda inconsistent with CollectionType
145
}
146     
147 }
148
149
Popular Tags