3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.api
.service
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
20 import org
.hibernate
.LockOptions
;
21 import org
.hibernate
.Session
;
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
.query
.Grouping
;
27 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
35 * @author a.kohlbecker
40 public interface IService
<T
extends ICdmBase
>{
42 // FIXME what does this method do?
46 * Obtain the specified lock mode on the given object t
48 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
50 public void lock(T t
, LockOptions lockOptions
);
53 * Refreshes a given object t using the specified lockmode
55 * All bean properties given in the <code>propertyPaths</code> parameter are recursively initialized.
57 * For detailed description and examples <b>please refer to:</b>
58 * {@link IBeanInitializer#initialize(Object, List)}
60 * NOTE: in the case of lockmodes that hit the database (e.g. LockMode.READ), you will need to re-initialize
61 * child propertiesto avoid a HibernateLazyInitializationException (even if the properties of the child
62 * were initialized prior to the refresh).
64 * NOTE: with hibernate 4 we changed parameter lockMode to lockOptions. LockOptions can be created from LockMode.
69 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
);
72 * Returns a count of all entities of type <T> optionally restricted
73 * to objects belonging to a class that that extends <T>
75 * @param clazz the class of entities to be counted (can be null to count all entities of type <T>)
76 * @return a count of entities
78 public int count(Class
<?
extends T
> clazz
);
81 * Delete an existing persistent object
83 * @param persistentObject the object to be deleted
84 * @return the unique identifier of the deleted entity
85 * @return deleteResult
87 public DeleteResult
delete(T persistentObject
) ;
92 * Returns true if an entity of type <T> with a unique identifier matching the
93 * identifier supplied exists in the database, or false if no such entity can be
95 * @param uuid the unique identifier of the entity required
96 * @return an entity of type <T> matching the uuid, or null if that entity does not exist
98 public boolean exists(UUID uuid
);
101 * Return a list of persisted entities that match the unique identifier
102 * set supplied as an argument
104 * @param uuidSet the set of unique identifiers of the entities required
105 * @return a list of entities of type <T>
107 public List
<T
> find(Set
<UUID
> uuidSet
);
110 * Return a persisted entity that matches the unique identifier
111 * supplied as an argument, or null if the entity does not exist
113 * @param uuid the unique identifier of the entity required
114 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
116 public T
find(UUID uuid
);
121 * Return a persisted entity that matches the unique identifier
122 * supplied as an argument, or null if the entity does not exist.
124 * The difference between this method and {@link #find(UUID) find} is
125 * that this method makes the hibernate read query with the
126 * {@link org.hibernate.FlushMode FlushMode} for the session set to 'MANUAL'
128 * <b>WARNING:</b>This method should <em>ONLY</em> be used when it is absolutely
129 * necessary and safe to ensure that the hibernate session is not flushed before a read
130 * query. A use case for this is the {@link eu.etaxonomy.cdm.api.cache.CdmCacher CdmCacher},
131 * (ticket #4276) where a call to {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID) load}
132 * the CDM Entity using the standard {@link #find(UUID) find} method results in recursion
133 * due to the fact that the {@link #find(UUID) find} method triggers a hibernate session
134 * flush which eventually could call {@link eu.etaxonomy.cdm.model.name.NonViralName#getNameCache getNameCache},
135 * which in turn (in the event that name cache is null) eventually calls the
136 * {@link eu.etaxonomy.cdm.api.cache.CdmCacher#load(UUID uuid) load} again.
137 * Apart from these kind of exceptional circumstances, the standard {@link #find(UUID) find}
138 * method should always be used to ensure that the persistence layer is always in sync with the
139 * underlying database.
142 * @return an entity of type <T>, or null if the entity does not exist or uuid is <code>null</code>
144 public T
findWithoutFlush(UUID uuid
);
147 * Return a persisted entity that matches the database identifier
148 * supplied as an argument, or null if the entity does not exist
150 * @param id the database identifier of the entity required
151 * @return an entity of type <T>, or null if the entity does not exist
153 public T
find(int id
);
156 * Returns a <code>List</code> of persisted entities that match the database identifiers.
157 * Returns an empty list if no identifier matches.
162 public List
<T
> findById(Set
<Integer
> idSet
); //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
165 // FIXME should we expose this method?
166 public Session
getSession();
169 * Returns a sublist of objects matching the grouping projections supplied using the groups parameter
171 * It would be nice to be able to return a pager, but for the moment hibernate doesn't
172 * seem to support this (HHH-3238 - impossible to get the rowcount for a criteria that has projections)
174 * @param clazz Restrict the query to objects of a certain class, or null for all objects of type T or subclasses
175 * @param limit the maximum number of entities returned (can be null to return
177 * @param start The (0-based) offset from the start of the recordset (can be null, equivalent of starting at the beginning of the recordset)
178 * @param groups The grouping objects representing a projection, plus an optional ordering on that projected property
179 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
180 * @return a list of arrays of objects, each matching the grouping objects supplied in the parameters.
182 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
);
185 * Returns a list of entities of type <T> optionally restricted
186 * to objects belonging to a class that that extends <T>
188 * @param type The type of entities to return (can be null to count all entities of type <T>)
189 * @param limit The maximum number of objects returned (can be null for all matching objects)
190 * @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)
192 * Supports path like <code>orderHints.propertyNames</code> which
193 * include *-to-one properties like createdBy.username or
194 * authorTeam.persistentTitleCache
195 * @param propertyPaths properties to be initialized
198 //TODO refactor to public <S extends T> List<T> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths);
199 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
202 * Finds the cdm entity specified by the <code>uuid</code> parameter and
203 * initializes all its *ToOne relations.
206 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
208 public T
load(UUID uuid
);
211 * Finds the cdm entity specified by the <code>uuid</code> parameter and
212 * recursively initializes all bean properties given in the
213 * <code>propertyPaths</code> parameter.
215 * For detailed description and examples <b>please refer to:</b>
216 * {@link IBeanInitializer#initialize(Object, List)}
219 * @return the cdm entity or <code>null</code> if not object with given uuid exists or uuid is <code>null</code>
221 public T
load(UUID uuid
, List
<String
> propertyPaths
);
224 * Copy the state of the given object onto the persistent object with the same identifier.
226 * @param transientObject the entity to be merged
227 * @return The unique identifier of the persisted entity
229 public T
merge(T transientObject
);
232 * Returns a paged list of entities of type <T> optionally restricted
233 * to objects belonging to a class that that extends <T>
235 * @param type The type of entities to return (can be null to count all entities of type <T>)
236 * @param pageSize The maximum number of objects returned (can be null for all matching objects)
237 * @param pageNumber The offset (in pageSize chunks) from the start of the result set (0 - based,
238 * can be null, equivalent of starting at the beginning of the recordset)
240 * Supports path like <code>orderHints.propertyNames</code> which
241 * include *-to-one properties like createdBy.username or
242 * authorTeam.persistentTitleCache
243 * @param propertyPaths properties to be initialized
244 * @return a pager of objects of type <T>
246 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);
249 * Re-read the state of the given instance from the underlying database.
251 * Hibernate claims that it is inadvisable to use refresh in long-running-sessions.
252 * I don't really see where we would get into a situation where problems as discussed
253 * this forum thread would apply for our scenario
255 * http://forum.hibernate.org/viewtopic.php?t=974544
257 * @param persistentObject the object to be refreshed
258 * @return the unique identifier
260 public UUID
refresh(T persistentObject
);
262 public List
<T
> rows(String tableName
, int limit
, int start
);
265 * Save a collection containing new entities (persists the entities)
266 * @param newInstances the new entities to be persisted
267 * @return A Map containing the new entities, keyed using the generated UUID's
270 public Map
<UUID
,T
> save(Collection
<T
> newInstances
);
273 * Save a new entity (persists the entity)
274 * @param newInstance the new entity to be persisted
275 * @return A generated UUID for the new persistent entity
277 public UUID
save(T newInstance
);
280 * Save a new entity or update the persistent state of an existing
281 * transient entity that has been persisted previously
283 * @param transientObject the entity to be persisted
284 * @return The unique identifier of the persisted entity
286 public UUID
saveOrUpdate(T transientObject
);
289 * Save new entities or update the persistent state of existing
290 * transient entities that have been persisted previously
292 * @param transientObjects the entities to be persisted
293 * @return The unique identifier of the persisted entity
295 public Map
<UUID
,T
> saveOrUpdate(Collection
<T
> transientObjects
);
298 * Update the persistent state of an existing transient entity
299 * that has been persisted previously
301 * @param transientObject the entity to be persisted
302 * @return The unique identifier of the persisted entity
304 public UUID
update(T transientObject
);
307 * Method that lists the objects matching the example provided.
308 * The includeProperties property is used to specify which properties of the example are used.
310 * If includeProperties is null or empty, then all literal properties are used (restrictions are
311 * applied as in the Hibernate Query-By-Example API call Example.create(object)).
313 * If includeProperties is not empty then only literal properties that are named in the set are used to
314 * create restrictions, *PLUS* any *ToOne related entities. Related entities are matched on ID, not by
315 * their internal literal values (e.g. the call is criteria.add(Restrictions.eq(property,relatedObject)), not
316 * criteria.createCriteria(property).add(Example.create(relatedObject)))
319 * @param includeProperties
320 * @param limit the maximum number of entities returned (can be null to return
322 * @param start The (0-based) offset from the start of the recordset
324 * Supports path like <code>orderHints.propertyNames</code> which
325 * include *-to-one properties like createdBy.username or
326 * @param propertyPaths paths initialized on the returned objects - only applied to the objects returned from the first grouping
327 * @return a list of matching objects
329 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
);