1
//$Id: Interceptor.java 7883 2005-08-12 20:03:07Z oneovthafew $
4
import java.io.Serializable;
5
import java.util.Iterator;
7
import org.hibernate.type.Type;
10
* Allows user code to inspect and/or change property values.
12
* Inspection occurs before property values are written and after they are read
13
* from the database.<br>
15
* There might be a single instance of <tt>Interceptor</tt> for a <tt>SessionFactory</tt>, or a new instance
16
* might be specified for each <tt>Session</tt>. Whichever approach is used, the interceptor must be
17
* serializable if the <tt>Session</tt> is to be serializable. This means that <tt>SessionFactory</tt>-scoped
18
* interceptors should implement <tt>readResolve()</tt>.<br>
20
* The <tt>Session</tt> may not be invoked from a callback (nor may a callback cause a collection or proxy to
21
* be lazily initialized).<br>
23
* Instead of implementing this interface directly, it is usually better to extend <tt>EmptyInterceptor</tt>
24
* and override only the callback methods of interest.
26
* @see SessionFactory#openSession(Interceptor)
27
* @see org.hibernate.cfg.Configuration#setInterceptor(Interceptor)
28
* @see EmptyInterceptor
31
public interface Interceptor {
33
* Called just before an object is initialized. The interceptor may change the <tt>state</tt>, which will
34
* be propagated to the persistent object. Note that when this method is called, <tt>entity</tt> will be
35
* an empty uninitialized instance of the class.
37
* @return <tt>true</tt> if the user modified the <tt>state</tt> in any way.
39
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
41
* Called when an object is detected to be dirty, during a flush. The interceptor may modify the detected
42
* <tt>currentState</tt>, which will be propagated to both the database and the persistent object.
43
* Note that not all flushes end in actual synchronization with the database, in which case the
44
* new <tt>currentState</tt> will be propagated to the object, but not necessarily (immediately) to
45
* the database. It is strongly recommended that the interceptor <b>not</b> modify the <tt>previousState</tt>.
47
* @return <tt>true</tt> if the user modified the <tt>currentState</tt> in any way.
49
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException;
51
* Called before an object is saved. The interceptor may modify the <tt>state</tt>, which will be used for
52
* the SQL <tt>INSERT</tt> and propagated to the persistent object.
54
* @return <tt>true</tt> if the user modified the <tt>state</tt> in any way.
56
public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
58
* Called before an object is deleted. It is not recommended that the interceptor modify the <tt>state</tt>.
60
public void onDelete(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) throws CallbackException;
62
* Called before a collection is (re)created.
64
public void onCollectionRecreate(Object collection, Serializable key) throws CallbackException;
66
* Called before a collection is deleted.
68
public void onCollectionRemove(Object collection, Serializable key) throws CallbackException;
70
* Called before a collection is updated.
72
public void onCollectionUpdate(Object collection, Serializable key) throws CallbackException;
74
* Called before a flush
76
public void preFlush(Iterator entities) throws CallbackException;
78
* Called after a flush that actually ends in execution of the SQL statements required to synchronize
79
* in-memory state with the database.
81
public void postFlush(Iterator entities) throws CallbackException;
83
* Called to distinguish between transient and detached entities. The return value determines the
84
* state of the entity with respect to the current session.
86
* <li><tt>Boolean.TRUE</tt> - the entity is transient
87
* <li><tt>Boolean.FALSE</tt> - the entity is detached
88
* <li><tt>null</tt> - Hibernate uses the <tt>unsaved-value</tt> mapping and other heuristics to
89
* determine if the object is unsaved
91
* @param entity a transient or detached entity
92
* @return Boolean or <tt>null</tt> to choose default behaviour
94
public Boolean isTransient(Object entity);
96
* Called from <tt>flush()</tt>. The return value determines whether the entity is updated
98
* <li>an array of property indices - the entity is dirty
99
* <li>an empty array - the entity is not dirty
100
* <li><tt>null</tt> - use Hibernate's default dirty-checking algorithm
102
* @param entity a persistent entity
103
* @return array of dirty property indices or <tt>null</tt> to choose default behaviour
105
public int[] findDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState, String[] propertyNames, Type[] types);
107
* Instantiate the entity class. Return <tt>null</tt> to indicate that Hibernate should use
108
* the default constructor of the class. The identifier property of the returned instance
109
* should be initialized with the given identifier.
111
* @param entityName the name of the entity
112
* @param entityMode The type of entity instance to be returned.
113
* @param id the identifier of the new instance
114
* @return an instance of the class, or <tt>null</tt> to choose default behaviour
116
public Object instantiate(String entityName, EntityMode entityMode, Serializable id) throws CallbackException;
119
* Get the entity name for a persistent or transient instance
120
* @param object an entity instance
121
* @return the name of the entity
123
public String getEntityName(Object object) throws CallbackException;
126
* Get a fully loaded entity instance that is cached externally
127
* @param entityName the name of the entity
128
* @param id the instance identifier
129
* @return a fully initialized entity
130
* @throws CallbackException
132
public Object getEntity(String entityName, Serializable id) throws CallbackException;
135
* Called when a Hibernate transaction is begun via the Hibernate <tt>Transaction</tt>
136
* API. Will not be called if transactions are being controlled via some other
137
* mechanism (CMT, for example).
139
public void afterTransactionBegin(Transaction tx);
141
* Called before a transaction is committed (but not before rollback).
143
public void beforeTransactionCompletion(Transaction tx);
145
* Called after a transaction is committed or rolled back.
147
public void afterTransactionCompletion(Transaction tx);
150
* Called when sql string is being prepared.
151
* @param sql sql to be prepared
152
* @return original or modified sql
154
public String onPrepareStatement(String sql);