KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: ManyToOneType.java,v 1.27 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 import java.util.Arrays JavaDoc;
9
10 import org.hibernate.AssertionFailure;
11 import org.hibernate.HibernateException;
12 import org.hibernate.MappingException;
13 import org.hibernate.engine.EntityKey;
14 import org.hibernate.engine.ForeignKeys;
15 import org.hibernate.engine.Mapping;
16 import org.hibernate.engine.SessionImplementor;
17 import org.hibernate.persister.entity.EntityPersister;
18
19 /**
20  * A many-to-one association to an entity
21  * @author Gavin King
22  */

23 public class ManyToOneType extends EntityType {
24     
25     private final boolean ignoreNotFound;
26     
27     protected boolean isNullable() {
28         return ignoreNotFound;
29     }
30
31     public boolean isAlwaysDirtyChecked() {
32         return ignoreNotFound;
33     }
34     
35     public int getColumnSpan(Mapping mapping) throws MappingException {
36         return getIdentifierOrUniqueKeyType(mapping).getColumnSpan(mapping);
37     }
38
39     public int[] sqlTypes(Mapping mapping) throws MappingException {
40         return getIdentifierOrUniqueKeyType(mapping).sqlTypes(mapping);
41     }
42
43     public ManyToOneType(String JavaDoc className) {
44         this(className, false);
45     }
46     
47     public ManyToOneType(String JavaDoc className, boolean lazy) {
48         super(className, null, !lazy, true, false);
49         this.ignoreNotFound = false;
50     }
51
52     public ManyToOneType(
53             String JavaDoc className,
54             String JavaDoc uniqueKeyPropertyName,
55             boolean lazy,
56             boolean unwrapProxy,
57             boolean isEmbeddedInXML,
58             boolean ignoreNotFound
59     ) {
60         super(className, uniqueKeyPropertyName, !lazy, isEmbeddedInXML, unwrapProxy);
61         this.ignoreNotFound = ignoreNotFound;
62     }
63
64     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, boolean[] settable, SessionImplementor session)
65     throws HibernateException, SQLException JavaDoc {
66         getIdentifierOrUniqueKeyType( session.getFactory() )
67                 .nullSafeSet(st, getIdentifier(value, session), index, settable, session);
68     }
69
70     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session)
71     throws HibernateException, SQLException JavaDoc {
72         getIdentifierOrUniqueKeyType( session.getFactory() )
73                 .nullSafeSet(st, getIdentifier(value, session), index, session);
74     }
75
76     public boolean isOneToOne() {
77         return false;
78     }
79
80     public ForeignKeyDirection getForeignKeyDirection() {
81         return ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT;
82     }
83
84     public Object JavaDoc hydrate(ResultSet JavaDoc rs, String JavaDoc[] names, SessionImplementor session, Object JavaDoc owner)
85     throws HibernateException, SQLException JavaDoc {
86         
87         //return the (fully resolved) identifier value, but do not resolve
88
//to the actual referenced entity instance
89

90         Serializable JavaDoc id = (Serializable JavaDoc) getIdentifierOrUniqueKeyType( session.getFactory() )
91                 .nullSafeGet(rs, names, session, null); //note that the owner of the association is not really the owner of the id!
92

93         if (id!=null) scheduleBatchLoad(id, session);
94         
95         return id;
96     }
97
98     /**
99      * Register the entity as batch loadable, if enabled
100      */

101     private void scheduleBatchLoad(Serializable JavaDoc id, SessionImplementor session)
102     throws MappingException {
103         
104         if (uniqueKeyPropertyName==null) { //cannot batch fetch by unique key
105

106             EntityPersister persister = session.getFactory()
107                     .getEntityPersister( getAssociatedEntityName() );
108             
109             EntityKey entityKey = new EntityKey( id, persister, session.getEntityMode() );
110             session.getPersistenceContext()
111                     .getBatchFetchQueue()
112                     .addBatchLoadableEntityKey( entityKey );
113         }
114     }
115     
116     public boolean useLHSPrimaryKey() {
117         return false;
118     }
119
120     public boolean isModified(Object JavaDoc old, Object JavaDoc current, SessionImplementor session)
121     throws HibernateException {
122
123         if (current==null) return old!=null;
124         if (old==null) return current!=null;
125         //the ids are fully resolved, so compare them with isDirty(), not isModified()
126
return getIdentifierOrUniqueKeyType( session.getFactory() )
127                 .isDirty( old, getIdentifier(current, session), session );
128     }
129
130     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
131     throws HibernateException {
132
133         if ( isNotEmbedded(session) ) {
134             return getIdentifierType(session).disassemble(value, session, owner);
135         }
136         
137         if (value==null) {
138             return null;
139         }
140         else {
141             // cache the actual id of the object, not the value of the
142
// property-ref, which might not be initialized
143
Object JavaDoc id = ForeignKeys.getEntityIdentifierIfNotUnsaved(
144                     getAssociatedEntityName(),
145                     value,
146                     session
147                 );
148             if (id==null) {
149                 throw new AssertionFailure(
150                         "cannot cache a reference to an object with a null id: " +
151                         getAssociatedEntityName()
152                     );
153             }
154             return getIdentifierType(session).disassemble(id, session, owner);
155         }
156     }
157
158     public Object JavaDoc assemble(Serializable JavaDoc oid, SessionImplementor session, Object JavaDoc owner)
159     throws HibernateException {
160         
161         //TODO: currently broken for unique-key references (does not detect
162
// change to unique key property of the associated object)
163

164         Serializable JavaDoc id = (Serializable JavaDoc) getIdentifierType(session)
165                 .assemble(oid, session, null); //the owner of the association is not the owner of the id
166

167         if ( isNotEmbedded(session) ) return id;
168         
169         if (id==null) {
170             return null;
171         }
172         else {
173             return resolveIdentifier(id, session);
174         }
175     }
176
177     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
178         boolean[] result = new boolean[ getColumnSpan(mapping) ];
179         if (value!=null) Arrays.fill(result, true);
180         return result;
181     }
182     
183 }
184
Popular Tags