1
//$Id: Type.java 7793 2005-08-10 05:06:40Z oneovthafew $
2
package org.hibernate.type;
4
import java.io.Serializable;
5
import java.sql.PreparedStatement;
6
import java.sql.ResultSet;
7
import java.sql.SQLException;
10
import org.dom4j.Node;
11
import org.hibernate.EntityMode;
12
import org.hibernate.HibernateException;
13
import org.hibernate.MappingException;
14
import org.hibernate.engine.Mapping;
15
import org.hibernate.engine.SessionFactoryImplementor;
16
import org.hibernate.engine.SessionImplementor;
19
* Defines a mapping from a Java type to an JDBC datatype. This interface is intended to
20
* be implemented by applications that need custom types.<br>
22
* Implementors should usually be immutable and <b>must</b> certainly be threadsafe.
26
public interface Type extends Serializable {
29
* Return true if the implementation is castable to
30
* <tt>AssociationType</tt>. This does not necessarily imply that
31
* the type actually represents an association.
32
* @see AssociationType
35
public boolean isAssociationType();
37
* Is this type a collection type.
39
public boolean isCollectionType();
42
* Is this type a component type. If so, the implementation
43
* must be castable to <tt>AbstractComponentType</tt>. A component
44
* type may own collections or associations and hence must provide
45
* certain extra functionality.
46
* @see AbstractComponentType
49
public boolean isComponentType();
52
* Is this type an entity type?
55
public boolean isEntityType();
58
* Is this an "any" type.
60
* i.e. a reference to a persistent entity
61
* that is not modelled as a (foreign key) association.
63
public boolean isAnyType();
65
public boolean isXMLElement();
68
* Return the SQL type codes for the columns mapped by this type. The codes
69
* are defined on <tt>java.sql.Types</tt>.
71
* @return the typecodes
72
* @throws MappingException
74
public int[] sqlTypes(Mapping mapping) throws MappingException;
77
* How many columns are used to persist this type.
79
public int getColumnSpan(Mapping mapping) throws MappingException;
82
* The class returned by <tt>nullSafeGet()</tt> methods. This is used to
83
* establish the class of an array of this type.
87
public Class getReturnedClass();
90
* Compare two instances of the class mapped by this type for persistence
91
* "equality" - equality of persistent state - taking a shortcut for
98
* @throws HibernateException
100
public boolean isSame(Object x, Object y, EntityMode entityMode) throws HibernateException;
103
* Compare two instances of the class mapped by this type for persistence
104
* "equality" - equality of persistent state.
110
* @throws HibernateException
112
public boolean isEqual(Object x, Object y, EntityMode entityMode) throws HibernateException;
115
* Compare two instances of the class mapped by this type for persistence
116
* "equality" - equality of persistent state.
122
* @throws HibernateException
124
public boolean isEqual(Object x, Object y, EntityMode entityMode, SessionFactoryImplementor factory)
125
throws HibernateException;
128
* Get a hashcode, consistent with persistence "equality"
132
public int getHashCode(Object x, EntityMode entityMode) throws HibernateException;
135
* Get a hashcode, consistent with persistence "equality"
140
public int getHashCode(Object x, EntityMode entityMode, SessionFactoryImplementor factory)
141
throws HibernateException;
144
* compare two instances of the type
147
public int compare(Object x, Object y, EntityMode entityMode);
150
* Should the parent be considered dirty, given both the old and current field or
153
* @param old the old value
154
* @param current the current value
156
* @return true if the field is dirty
158
public boolean isDirty(Object old, Object current, SessionImplementor session)
159
throws HibernateException;
161
* Should the parent be considered dirty, given both the old and current field or
164
* @param old the old value
165
* @param current the current value
166
* @param checkable which columns are actually updatable
168
* @return true if the field is dirty
170
public boolean isDirty(Object old, Object current, boolean[] checkable, SessionImplementor session)
171
throws HibernateException;
174
* Has the parent object been modified, compared to the current database state?
175
* @param oldHydratedState the database state, in a "hydrated" form, with identifiers unresolved
176
* @param currentState the current state of the object
177
* @param checkable which columns are actually updatable
179
* @return true if the field has been modified
181
public boolean isModified(Object oldHydratedState, Object currentState, boolean[] checkable, SessionImplementor session)
182
throws HibernateException;
185
* Retrieve an instance of the mapped class from a JDBC resultset. Implementors
186
* should handle possibility of null values.
188
* @see Type#hydrate(ResultSet, String[], SessionImplementor, Object) alternative, 2-phase property initialization
190
* @param names the column names
192
* @param owner the parent entity
194
* @throws HibernateException
195
* @throws SQLException
197
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
198
throws HibernateException, SQLException;
201
* Retrieve an instance of the mapped class from a JDBC resultset. Implementations
202
* should handle possibility of null values. This method might be called if the
203
* type is known to be a single-column type.
206
* @param name the column name
208
* @param owner the parent entity
210
* @throws HibernateException
211
* @throws SQLException
213
public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner)
214
throws HibernateException, SQLException;
217
* Write an instance of the mapped class to a prepared statement, ignoring some columns.
218
* Implementors should handle possibility of null values. A multi-column type should be
219
* written to parameters starting from <tt>index</tt>.
221
* @param value the object to write
222
* @param index statement parameter index
223
* @param settable an array indicating which columns to ignore
226
* @throws HibernateException
227
* @throws SQLException
229
public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session)
230
throws HibernateException, SQLException;
233
* Write an instance of the mapped class to a prepared statement. Implementors
234
* should handle possibility of null values. A multi-column type should be written
235
* to parameters starting from <tt>index</tt>.
237
* @param value the object to write
238
* @param index statement parameter index
241
* @throws HibernateException
242
* @throws SQLException
244
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
245
throws HibernateException, SQLException;
248
* A representation of the value to be embedded in an XML element.
253
* @throws HibernateException
255
public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory)
256
throws HibernateException;
259
* A representation of the value to be embedded in a log file.
264
* @throws HibernateException
266
public String toLoggableString(Object value, SessionFactoryImplementor factory)
267
throws HibernateException;
270
* Parse the XML representation of an instance.
274
* @return an instance of the type
275
* @throws HibernateException
277
public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException;
280
* Returns the abbreviated name of the type.
282
* @return String the Hibernate type name
284
public String getName();
287
* Return a deep copy of the persistent state, stopping at entities and at
289
* @param value generally a collection element or entity field
292
* @return Object a copy
294
public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory)
295
throws HibernateException;
298
* Are objects of this type mutable. (With respect to the referencing object ...
299
* entities and collections are considered immutable because they manage their
300
* own internal state.)
304
public boolean isMutable();
307
* Return a cacheable "disassembled" representation of the object.
308
* @param value the value to cache
309
* @param session the session
310
* @param owner optional parent entity object (needed for collections)
311
* @return the disassembled, deep cloned state
313
public Serializable disassemble(Object value, SessionImplementor session, Object owner) throws HibernateException;
316
* Reconstruct the object from its cached "disassembled" state.
317
* @param cached the disassembled state from the cache
318
* @param session the session
319
* @param owner the parent entity object
320
* @return the the object
322
public Object assemble(Serializable cached, SessionImplementor session, Object owner)
323
throws HibernateException;
326
* Called before assembling a query result set from the query cache, to allow batch fetching
327
* of entities missing from the second-level cache.
329
public void beforeAssemble(Serializable cached, SessionImplementor session);
332
* Retrieve an instance of the mapped class, or the identifier of an entity or collection,
333
* from a JDBC resultset. This is useful for 2-phase property initialization - the second
334
* phase is a call to <tt>resolveIdentifier()</tt>.
336
* @see Type#resolve(Object, SessionImplementor, Object)
338
* @param names the column names
339
* @param session the session
340
* @param owner the parent entity
341
* @return Object an identifier or actual value
342
* @throws HibernateException
343
* @throws SQLException
345
public Object hydrate(ResultSet rs, String[] names, SessionImplementor session, Object owner)
346
throws HibernateException, SQLException;
349
* Map identifiers to entities or collections. This is the second phase of 2-phase property
352
* @see Type#hydrate(ResultSet, String[], SessionImplementor, Object)
353
* @param value an identifier or value returned by <tt>hydrate()</tt>
354
* @param owner the parent entity
355
* @param session the session
356
* @return the given value, or the value associated with the identifier
357
* @throws HibernateException
359
public Object resolve(Object value, SessionImplementor session, Object owner)
360
throws HibernateException;
363
* Given a hydrated, but unresolved value, return a value that may be used to
364
* reconstruct property-ref associations.
366
public Object semiResolve(Object value, SessionImplementor session, Object owner)
367
throws HibernateException;
370
* Get the type of a semi-resolved value.
372
public Type getSemiResolvedType(SessionFactoryImplementor factory);
375
* During merge, replace the existing (target) value in the entity we are merging to
376
* with a new (original) value from the detached entity we are merging. For immutable
377
* objects, or null values, it is safe to simply return the first parameter. For
378
* mutable objects, it is safe to return a copy of the first parameter. For objects
379
* with component values, it might make sense to recursively replace component values.
381
* @param original the value from the detached entity being merged
382
* @param target the value in the managed entity
383
* @return the value to be merged
385
public Object replace(
388
SessionImplementor session,
391
throws HibernateException;
394
* During merge, replace the existing (target) value in the entity we are merging to
395
* with a new (original) value from the detached entity we are merging. For immutable
396
* objects, or null values, it is safe to simply return the first parameter. For
397
* mutable objects, it is safe to return a copy of the first parameter. For objects
398
* with component values, it might make sense to recursively replace component values.
400
* @param original the value from the detached entity being merged
401
* @param target the value in the managed entity
402
* @return the value to be merged
404
public Object replace(
407
SessionImplementor session,
410
ForeignKeyDirection foreignKeyDirection)
411
throws HibernateException;
414
* Given an instance of the type, return an array of boolean, indicating
415
* which mapped columns would be null.
417
* @param value an instance of the type
419
public boolean[] toColumnNullness(Object value, Mapping mapping);