2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.util
.Collection
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.hibernate
.LockOptions
;
20 import org
.hibernate
.Session
;
21 import org
.hibernate
.event
.spi
.MergeEvent
;
23 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
24 import eu
.etaxonomy
.cdm
.model
.common
.ICdmBase
;
25 import eu
.etaxonomy
.cdm
.persistence
.dao
.initializer
.IBeanInitializer
;
26 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
27 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.PostMergeEntityListener
;
28 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
29 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
37 * @author a.kohlbecker
42 public interface IService
<T
extends ICdmBase
>{
44 // FIXME what does this method do?
48 * Obtain the specified lock mode on the given object t
50 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
52 public void lock(T t
, LockOptions lockOptions
);
55 * Refreshes a given object t using the specified lockmode
57 * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
59 * For detailed description and examples <b>please refer to:</b>
60 * {@link IBeanInitializer#initialize(Object, List)}
62 * NOTE: in the case of lockmodes that hit the database (e.g. LockMode.READ), you will need to re-initialize
63 * child propertiesto avoid a HibernateLazyInitializationException (even if the properties of the child
64 * were initialized prior to the refresh).
66 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
71 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
);
74 * Returns a count of all entities of type <T> optionally restricted
75 * to objects belonging to a class that that extends <T>
77 * @param clazz the class of entities to be counted (can be null to count all entities of type <T>)
78 * @return a count of entities
80 public int count(Class
<?
extends T
> clazz
);
83 * Delete an existing persistent object
85 * @param persistentObject the object to be deleted
86 * @return the unique identifier of the deleted entity
87 * @return deleteResult
89 public DeleteResult
delete(UUID persistentObjectUUID
) ;
94 * Returns true if an entity of type <T> with a unique identifier matching the
95 * identifier supplied exists in the database, or false if no such entity can be
97 * @param uuid the unique identifier of the entity required
98 * @return an entity of type <T> matching the uuid, or null if that entity does not exist
100 public boolean exists(UUID uuid
);
103 * Return a list of persisted entities that match the unique identifier
104 * set supplied as an argument
106 * @param uuidSet the set of unique identifiers of the entities required
107 * @return a list of entities of type <T>
109 public List
<T
> find(Set
<UUID
> uuidSet
);
112 * Return a persisted entity that matches the unique identifier
113 * supplied as an argument, or null if the entity does not exist
115 * @param uuid the unique identifier of the entity required
116 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
118 public T
find(UUID uuid
);
123 * Return a persisted entity that matches the unique identifier
124 * supplied as an argument, or null if the entity does not exist.
126 * The difference between this method and {@link #find(UUID) find} is
127 * that this method makes the hibernate read query with the
128 * {@link org.hibernate.FlushMode FlushMode} for the session set to 'MANUAL'
130 * <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely
131 * necessary and safe to ensure that the hibernate session is not flushed before a read
132 * query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacher CdmCacher},
133 * (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID) load}
134 * the CDM Entity using the standard {@link #find(UUID) find} method results in recursion
135 * due to the fact that the {@link #find(UUID) find} method triggers a hibernate session
136 * flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache},
137 * which in turn (in the event that name cache is null) eventually calls the
138 * {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID uuid) load} again.
139 * Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find}
140 * method should always be used to ensure that the persistence layer is always in sync with the
141 * underlying database.
144 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
146 public T
findWithoutFlush(UUID uuid
);
149 * Return a persisted entity that matches the database identifier
150 * supplied as an argument, or null if the entity does not exist
152 * @param id the database identifier of the entity required
153 * @return an entity of type <T>, or null if the entity does not exist
155 public T
find(int id
);
158 * Returns a <code>List</code> of persisted entities that match the database identifiers.
159 * Returns an empty list if no identifier matches.
163 * @deprecated use {@link #loadByIds(Set, List)} instead
166 public List
<T
> findById(Set
<Integer
> idSet
); //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
169 // FIXME should we expose this method?
170 public Session
getSession();
173 * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
175 * It would be nice to be able to return a pager, but for the moment hibernate doesn't
176 * seem to support this (HHH-3238 - impossible to get the rowcount for a criteria that has projections)
178 * @param clazz Restrict the query to objects of a certain class, or null for all objects of type T or subclasses
179 * @param limit the maximum number of entities returned (can be null to return
181 * @param start The (0-based) offset from the start of the recordset (can be null, equivalent of starting at the beginning of the recordset)
182 * @param groups The grouping objects representing a projection, plus an optional ordering on that projected property
183 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
184 * @return a list of arrays of objects, each matching the grouping objects supplied in the parameters.
186 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
);
189 * Returns a list of entities of type <T> optionally restricted
190 * to objects belonging to a class that that extends <T>
192 * @param type The type of entities to return (can be null to count all entities of type <T>)
193 * @param limit The maximum number of objects returned (can be null for all matching objects)
194 * @param start The offset from the start of the result set (0 - based, can be null - equivalent of starting at the beginning of the recordset)
196 * Supports path like <code>orderHints.propertyNames</code> which
197 * include *-to-one properties like createdBy.username or
198 * authorTeam.persistentTitleCache
199 * @param propertyPaths properties to be initialized
202 //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
203 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
206 * Finds the cdm entity specified by the <code>uuid</code> parameter and
207 * initializes all its *ToOne relations.
210 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
212 public T
load(UUID uuid
);
215 * Return a persisted entity that matches the database identifier
216 * supplied as an argument, or null if the entity does not exist
218 * @param id the database identifier of the entity required
219 * @param propertyPaths
222 public T
load(int id
, List
<String
> propertyPaths
);
225 * Finds the cdm entity specified by the <code>uuid</code> parameter and
226 * recursively initializes all bean properties given in the
227 * <code>propertyPaths</code> parameter.
229 * For detailed description and examples <b>please refer to:</b>
230 * {@link IBeanInitializer#initialize(Object, List)}
233 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
235 public T
load(UUID uuid
, List
<String
> propertyPaths
);
239 * Finds the cdm entities specified by the <code>uuids</code>,
240 * recursively initializes all bean properties given in the
241 * <code>propertyPaths</code> parameter and returns the initialised
244 * For detailed description and examples <b>please refer to:</b>
245 * {@link IBeanInitializer#initialize(Object, List)}
247 * @param propertyPaths
250 public List
<T
> load(List
<UUID
> uuids
, List
<String
> propertyPaths
);
253 * Copy the state of the given object onto the persistent object with the same identifier.
255 * @param transientObject the entity to be merged
256 * @return The unique identifier of the persisted entity
258 public T
merge(T transientObject
);
261 * Returns a paged list of entities of type <T> optionally restricted
262 * to objects belonging to a class that that extends <T>
264 * @param type The type of entities to return (can be null to count all entities of type <T>)
265 * @param pageSize The maximum number of objects returned (can be null for all matching objects)
266 * @param pageNumber The offset (in pageSize chunks) from the start of the result set (0 - based,
267 * can be null, equivalent of starting at the beginning of the recordset)
269 * Supports path like <code>orderHints.propertyNames</code> which
270 * include *-to-one properties like createdBy.username or
271 * authorTeam.persistentTitleCache
272 * @param propertyPaths properties to be initialized
273 * @return a pager of objects of type <T>
275 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
278 * Re-read the state of the given instance from the underlying database.
280 * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
281 * I don't really see where we would get into a situation where problems as discussed
282 * this forum thread would apply for our scenario
284 * http://forum.hibernate.org/viewtopic.php?t=974544
286 * @param persistentObject the object to be refreshed
287 * @return the unique identifier
289 public UUID
refresh(T persistentObject
);
291 public List
<T
> rows(String tableName
, int limit
, int start
);
294 * Save a collection containing new entities (persists the entities)
295 * @param newInstances the new entities to be persisted
296 * @return A Map containing the new entities, keyed using the generated UUID's
299 public Map
<UUID
,T
> save(Collection
<T
> newInstances
);
302 * Save a new entity (persists the entity)
303 * @param newInstance the new entity to be persisted
304 * @return A generated UUID for the new persistent entity
306 public T
save(T newInstance
);
309 * Save a new entity or update the persistent state of an existing
310 * transient entity that has been persisted previously
312 * @param transientObject the entity to be persisted
313 * @return The unique identifier of the persisted entity
315 public UUID
saveOrUpdate(T transientObject
);
318 * Save new entities or update the persistent state of existing
319 * transient entities that have been persisted previously
321 * @param transientObjects the entities to be persisted
322 * @return The unique identifier of the persisted entity
324 public Map
<UUID
,T
> saveOrUpdate(Collection
<T
> transientObjects
);
327 * Update the persistent state of an existing transient entity
328 * that has been persisted previously
330 * @param transientObject the entity to be persisted
331 * @return The unique identifier of the persisted entity
333 public UUID
update(T transientObject
);
336 * Simply calls the load method.
337 * Required specifically for the editor to allow load calls which
338 * can also update the session cache.
343 public T
loadWithUpdate(UUID uuid
);
346 * Method that lists the objects matching the example provided.
347 * The includeProperties property is used to specify which properties of the example are used.
349 * If includeProperties is null or empty, then all literal properties are used (restrictions are
350 * applied as in the Hibernate Query-By-Example API call Example.create(object)).
352 * If includeProperties is not empty then only literal properties that are named in the set are used to
353 * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
354 * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
355 * criteria.createCriteria(property).add(Example.create(relatedObject)))
358 * @param includeProperties
359 * @param limit the maximum number of entities returned (can be null to return
361 * @param start The (0-based) offset from the start of the recordset
363 * Supports path like <code>orderHints.propertyNames</code> which
364 * include *-to-one properties like createdBy.username or
365 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
366 * @return a list of matching objects
368 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
370 public DeleteResult
delete(T persistentObject
);
373 * Deletes a collection of persistent objects correponding to the
374 * given list of uuids. The result will have status as ok if even one
375 * of the deletes is successful, else error.
377 * @param persistentObjectUUIDs uuids of persistent objects to delete
378 * @return DeleteResult object
380 public DeleteResult
delete(Collection
<UUID
> persistentObjectUUIDs
);
383 * Merges a list of detached objects and returns the new
384 * list of merged objects
386 * @param detachedObjects
387 * @return a list of merged objects
389 public List
<T
> merge(List
<T
> detachedObjects
);
392 * Loads a batch of entities referenced by their ids.
395 * @param propertyPaths
398 List
<T
> loadByIds(List
<Integer
> idSet
, List
<String
> propertyPaths
);
401 * This method allows for the possibility of returning the input transient
402 * entities instead of the merged persistent entity
404 * WARNING : This method should never be used when the objective of the merge
405 * is to attach to an existing session which is the standard use case.
406 * This method should only be used in the
407 * case of an external call which does not use hibernate sessions and is
408 * only interested in the entity as a POJO. Apart from the session information
409 * the only other difference between the transient and persisted object is in the case
410 * of new objects (id=0) where hibernate sets the id after commit. This id is copied
411 * over to the transient entity in {@link PostMergeEntityListener#onMerge(MergeEvent,Map)}
412 * making the two objects identical and allowing the transient object to be used further
415 * @param detachedObjects
416 * @param returnTransientEntity
419 public List
<MergeResult
<T
>> merge(List
<T
> detachedObjects
, boolean returnTransientEntity
);
422 * This method allows for the possibility of returning the input transient
423 * entity instead of the merged persistent entity
425 * WARNING : This method should never be used when the objective of the merge
426 * is to attach to an existing session which is the standard use case.
427 * This method should only be used in the case of an external call which does
428 * not use hibernate sessions and is only interested in the entity as a POJO.
429 * This method returns the root merged transient entity as well as all newly merged
430 * persistent entities within the return object.
433 * @param returnTransientEntity
436 public MergeResult
<T
> merge(T newInstance
, boolean returnTransientEntity
);